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 ...
随机推荐
- 开始安装 ASP.NET (4.0.30319.18408)。 出现了错误: 0x8007b799 必须具有此计算机的管理员权限才能运行此工具
在Visual Studio命令提示符安装ASP.NET .出现了错误: 0x8007b799 必须具有此计算机的管理员权限才能运行此工具:如下图: 解决方案如下: 1.打开“C:\Windows\S ...
- SQL SERVER中查询参数为空(null)时默认查询所有的实现
最近在项目中碰到一个比较有意思的问题,网上查找了一些方法,在这里总结分享一下. 我们经常会碰到这样的场景:需要查询数据,有一些查询条件,但是查询的时候,我们希望在某个条件为空的时候,则不筛选这个条件, ...
- Silverlight Color的颜色值
1.MainPage.xaml <UserControl xmlns:SysManage="clr-namespace:Application" x:Class=" ...
- ListView的几种形式
一. ArrayAdapter ListView listView = (ListView) findViewById(R.id.list_view);//ListView的参数为id listVie ...
- Microsoft Visual C++ Runtime error解决方法
1: 当出现下图时提示Microsoft Visual C++ Runtime error 2:此时不要关闭该对话框,然后打开任务管理器(Ctrl+Shift+Esc)如下图: 找到Microsoft ...
- delphi的几个特别关键字 object absolute
1.object关键字相当于C++中的struct, record定义个结构体只能定义数据,而object可以定义方法,默认都是public的. 代码示例如下: TTest = record na ...
- DevExpress BarManager 部分用法
1.创建一个BarManager会默认产生三个菜单.BarManager右键ShowDesignTime enhancements会显示[add]按钮,可对菜单进行编辑. 2.其中比较有用的属性: 选 ...
- 04-树5 Root of AVL Tree
平衡二叉树 LL RR LR RL 注意画图理解法 An AVL tree is a self-balancing binary search tree. In an AVL tree, the he ...
- sql server 查询数据库所有的表名+字段
SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='Account' SELECT (case when a.colorder= ...
- 使用 Bumblebee 控制 NVIDIA 双显卡
简介 Nvidia的双显卡切换技术叫Optimus(擎天柱),可惜只能在win7.vista下实现.Linux下没有对应的技术,当然苹果也没有.这导致独立显卡一直在启用,显卡发热升温,风扇狂转,却没有 ...