SPOJ 057 Supernumbers in a permutation
原题链接:http://www.spoj.com/problems/SUPPER/
这道题n<=200000,那么确定为nlogn的算法,再定位到求LIS的O(nlogn)的算法。
对于每个a[i],求出其向左能延伸的元素个数L[i]和向右能延伸的元素个数R[i],所有位置L[i]+R[i]为最大值的元素排序输出即可。
心得:
1.求LIS的O(nlogn)算法能解决子区间的LIS问题,所以经常出现在题目中,要灵活运用。
2.lower_bound函数有cmp函数参数可以选择升序查找(less<int>())或降序查找(greater<int>())。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std; #define N 100005 int a[N], L[N], R[N], n, m, d[N];
vector<int> vt; int main()
{
for(int cas = ; cas <= ; cas++)
{
scanf("%d", &n);
for(int i = ; i < n; i++)
scanf("%d", &a[i]);
vt.clear();
for(int i = ; i < n; i++)
{
int j = lower_bound(vt.begin(), vt.end(), a[i], less<int>()) - vt.begin();
if(j == vt.size())
vt.push_back(a[i]);
else
vt[j] = min(vt[j], a[i]);
L[i] = j;
}
vt.clear();
for(int i = n - ; i >= ; i--)
{
int j = lower_bound(vt.begin(), vt.end(), a[i], greater<int>()) - vt.begin();
if(j == vt.size())
vt.push_back(a[i]);
else
vt[j] = max(vt[j], a[i]);
R[i] = j;
}
m = ;
for(int i = ; i < n; i++)
if(L[i] + R[i] > m) m = L[i] + R[i];
int k();
for(int i = ; i < n; i++)
{
if(L[i]+R[i] == m)
{
d[k++] = a[i];
}
}
sort(d, d+k);
printf("%d\n", k);
for(int i = ; i < k; i++)
{
if(i != ) putchar(' ');
printf("%d", d[i]);
}
putchar('\n');
}
return ;
}
SPOJ 057 Supernumbers in a permutation的更多相关文章
- SPOJ - PERMJUMP Permutation Jumping
Discription John likes playing the game Permutation Jumping. First he writes down a permutation A of ...
- bzoj1318[spoj 744] Longest Permutation
题意 给出一个长度为n的,所有元素大小在[1,n]的整数数列,要求选出一个尽量长的区间使得区间内所有元素组成一个1到区间长度k的排列,输出k的最大值 n<=1e5 分析 不会做,好菜啊.jpg ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- yii 计划任务
Yii框架自动生成的Web应用骨架的目录里面有连个脚步文件,yiic和yiic.bat. yiic是Unix/Linux平台用的,yiic.bat是windows平台用的.如果要查看脚本的帮助可以进入 ...
- PHP 把GBK编码转换为UTF8
//把GBK编码转换为UTF8 $name="勿以善小而不为"; $name=iconv("GBK", "UTF-8", $name);
- [.ashx檔?泛型处理例程?]基础入门#2....FileUpload上传前,预览图片(两种作法--ashx与JavaScript)
原文出處 http://www.dotblogs.com.tw/mis2000lab/archive/2013/08/20/ashx_beginner_02_fileupload_picture_p ...
- 【转】javascript性能优化-repaint和reflow
repaint(重绘) ,repaint发生更改时,元素的外观被改变,且在没有改变布局的情况下发生,如改变outline,visibility,background color,不会影响到dom结构渲 ...
- android在程序中打开另一个程序
在开发android应用的时候,在一些情况下要有前置条件,比如这边所说的要启动时要确保别的应用程序服务已经打开 或者在操作中启动别的应用等. 先来一段google上的代码: 1. 已知包名和类名的情 ...
- PropertyGrid 控件使用方法
编写一个对象,后面传递给 PropertyGrid 来显示: using System; using System.Collections.Generic; using System.Linq; us ...
- python实现决策树
1.决策树的简介 http://www.cnblogs.com/lufangtao/archive/2013/05/30/3103588.html 2.决策是实现的伪代码 “读入训练数据” “找出每个 ...
- 第十章 管理类型(In .net4.5) 之 使用反射
1. 概述 一个.net程序不仅包含代码和数据,还包含 元数据. 本章介绍如何应用attributes以及如何使用反射来获取它,还有如何使用CodeDom和expression trees来实现在运行 ...
- 解决 MVC 用户上线下线状态问题
以前工作项目中就有一个微博类功能,其中也出现了用户在线和离线的问题. 但是因为初入程序猿 使用的是 Session _end 上个事件. Session _end 这个事件不需要怎么解释吧 就是在se ...
- python xml包使用记录
<?xml version="1.0" encoding="utf-8" ?> <request> <functionID> ...