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 ...
随机推荐
- 常规轮询请求,客户端用Ajax调webservice的方法
服务端发布webservice,下图方框中的一定要有 客户端代码 <script type="text/javascript"> $(document).ready(f ...
- 前端构建工具grunt
简单配置grunt 配置gulp还是grunt都是在node的环境下安装的,所以在这之前保证你的node环境已经安装好了! -------------------------------------- ...
- asp.net读取excel文件多种方法
asp.net读取excel文件的三种方法示例,包括采用OleDB读取Excel文件.引用的com组件读取Excel文件.用文件流读取. 方法一:采用OleDB读取Excel文件 把Excel文件 ...
- JS中cookie的基本使用
cookie是本身是HTML中ducument中的一个属性,可以用来保存一些简单的数据信息,比如用户名.密码等,提高一些网站的用户体验度.下面就来简单的说说cookie,它有下面几个特性: 1.有过期 ...
- 1)Java JDK和JRE
>JRE : Java Runtime Enviroment Java的运行环境.面向Java程序的使用者,而不是开发者.如果你仅下载并安装了JRE,那么你的系统只能运行Java程序(不能 ...
- android开源代码
Android开源项目--分类汇总 转自:https://github.com/Trinea/android-open-project Android开源项目第一篇——个性化控件(View)篇 包括L ...
- Windows API 的数据类型与 Delphi 数据类型对照表
Windows 数据类型 Delphi 数据类型 描述 LPSTR PAnsiChar 字符串指针 LPCSTR PAnsiChar 字符串指针 DWORD LongWord 整数 BOOL Long ...
- MongoDB工具简要说明
[mongodb@hadoop1 bin]$ pwd /usr/local/mongodb/bin [mongodb@hadoop1 bin]$ ls -l total 207696 -rwxr-xr ...
- 十天学会单片机Day4串行口通信
并行与串行基本通信方式 1.并行通信方式 通常是将数据字节的各位用多条数据线同时进行传送. 并行通信控制简单.传输速度快:由于传输线较多,长距离传送时成本高且接收方的各位同时接收存在困难. 2.串行通 ...
- linux命令行下的ftp 多文件下载和目录下载(转)
目标ftp服务器是一个非标准端口的ftp 1.通过shell登录 #ftp //shell下输入ftp命令,进入到ftp提示符 >open IP PORT //IP ,PORT对 ...