原题链接: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的更多相关文章

  1. SPOJ - PERMJUMP Permutation Jumping

    Discription John likes playing the game Permutation Jumping. First he writes down a permutation A of ...

  2. bzoj1318[spoj 744] Longest Permutation

    题意 给出一个长度为n的,所有元素大小在[1,n]的整数数列,要求选出一个尽量长的区间使得区间内所有元素组成一个1到区间长度k的排列,输出k的最大值 n<=1e5 分析 不会做,好菜啊.jpg ...

  3. 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 ...

  4. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  6. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  7. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. 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 ...

随机推荐

  1. 未能正确加载“Microsoft.VisualStudio.Implementation.EditorPackage”包

    未能正确加载“Microsoft.VisualStudio.Implementation.EditorPackage”包 未处理ImportCardinalityMismatchException 未 ...

  2. [leetcode]_Maximum Depth of Binary Tree

    第三道树的题目,我还是不会,我擦,怎么递归算法还是不能很好理解.看来还得好好研究下递归算法. 题目:求一棵树的最大深度. 思路:递归地求取左子树最大深度 和 右子树最大深度,返回较大值即为 整棵树的 ...

  3. freefilesync 7 使用

    官方下载地址:http://www.freefilesync.org/download.php 1.打开FreeFileSync 设置左右的文件夹,设置过滤规则,设置同步方式(双向.单向),执行同步 ...

  4. synchronized的重入

    /** * synchronized的重入 * */ public class SyncDubbo1 { public synchronized void method1(){ System.out. ...

  5. AngularJs记录学习04

    <html> <head> <title>Angular JS Views</title> <script src="js/Angula ...

  6. WPF学习03:Element Binding

    元素绑定是数据绑定的一个子集,关于数据绑定是什么,园子里有很多文章都说得非常好,在此不予详细说明. WPF实现了完善的数据绑定机制,使得元素绑定有简易的实现步骤. 本文的元素指的是WPF中的可视控件. ...

  7. Android WebView代理设置方法(API10~21适用)

    最近碰到个需求需要在APP中加入代理,HttpClient的代理好解决,但是WebView碰到些问题,然后找到个API10~API21都通用的类,需要用的同学自己看吧,使用方法,直接调用类方法setP ...

  8. EMVTag系列13《脱机PIN》

    DGI8010用于个人化借记贷记交易中使用的脱机PIN.数据强制要求加密.制卡数据传输过程中,此DGI采用DEK加密保护. 数据分组标识 '8010'的数据内容       要求            ...

  9. MIFARE系列1《MIFARE简介》

    随着社会的发展,智能卡在很多领域得到了广泛的应用.特别是非接触卡,由于使用方便以及功能强大的特点,在管理.公交.工作证.身份识别等领域得到了快速的普及和推广. 非接触卡已经逐步发展成为一个独立的跨学科 ...

  10. .NET开源工作流RoadFlow-流程设计-流程步骤设置-数据设置

    数据设置是控制在流程处理过程中,当前步骤的数据显示与编辑状态,控制当前步骤哪些字段为只读,隐藏或可编辑.需要配合表单设计器使用.