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 ...
随机推荐
- VS2010在非IE浏览器下调试Silverlight程序
以Chrome为例: 第一步:在程序中设置断点. 第二步:右键点击web应用程序的起始页(.html或.aspx文件),选择"浏览方式",选中Chrome或其它非IE浏览器,点&q ...
- 取消界面的title
在setContentView(R.layout.activity_main)方法上面添加代码(继承Activity的写法): requestWindowFeature(Window.FEATURE_ ...
- 通过messenger实现activity与service的相互通信
布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...
- Android中内容观察者的使用---- ContentObserver类详解
详解:http://blog.csdn.net/qinjuning/article/details/7047607
- Dapper多表查询(列名重复,类字段重复)映射方案
1. 一个主名,一个别名,设计时候属性和字段命名不同. 这样主名和别名都可以用的,在主名与别人重复时候用别名(别名可以设计的明确一点长一点,比如类名和字段结合) 2. 或者找一个字段多的直接继承出一个 ...
- c语言结构体保存并输出学生信息
最近在学习数据结构,巩固下c语言. #include<stdio.h> /*定义结构体student并设置别名stud*/ /*typedef struct student{ int nu ...
- Python开发【第一篇】Python基础之生成器和迭代器
生成器和迭代器 1.生成器 一个函数调用时返回一个迭代器,那这个函数就叫做生成器(generator):如果函数中包含yield语法,那这个函数就会变成生成器: def func(): yield 1 ...
- [转]命令行 Subversion 入门
http://omyyal.iteye.com/blog/1762831 命令行 Subversion 入门 如果您参与的项目正在使用 Subversion 进行版本控制,您将需要使用 Subvers ...
- Requests:Python HTTP Module学习笔记(一)(转)
Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...
- C基础 数据序列化简单使用和讨论
前言 C中对序列化讨论少, 因为很多传输的内容都有自己解析的轮子. 对于序列化本质是统一编码, 统一解码的方式. 本文探讨是一种简单的序列化方案. 保证不同使用端都能解析出正确结果. 在文章一开始, ...