uva 10534 Wavio Sequence
2015-09-09 18:17
316 查看
题意:
找一个最长(假设长度为2N-1)的子序列,使得前N个元素递增,后N个元素递减。
和高中一道做过的题目几乎一模一样,noip的合唱队行,只不过这个要求递增和递减的数目相同。
正着求一遍最长上升子序列,再逆着求一遍最长上升子序列,然后枚举每一个数,看起两边延伸的最短的长度乘2减一就好。
由于数据很多,LIS要用nlogn的方法,很简单啦。
// Created by Chenhongwei in 2015.
// Copyright (c) 2015 Chenhongwei. All rights reserved.
#include"iostream"
#include"cstdio"
#include"cstdlib"
#include"cstring"
#include"climits"
#include"queue"
#include"cmath"
#include"map"
#include"set"
#include"stack"
#include"vector"
#include"sstream"
#include"algorithm"
using namespace std;
typedef long long ll;
const int inf=1e8;
int a[10010],n;
int f1[10010],f2[10010],g[10010];
int main()
{
//ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n+1;i++)
g[i]=inf;
for(int i=1;i<=n;i++)
{
int k=lower_bound(g+1,g+n+1,a[i])-g;
f1[i]=k;
g[k]=a[i];
}
for(int i=1;i<=n+1;i++)
g[i]=inf;
for(int i=n;i>=1;i--)
{
int k=lower_bound(g+1,g+n+1,a[i])-g;
f2[i]=k;
g[k]=a[i];
}
int ans=0;
for(int i=1;i<=n;i++)
ans=max(ans,min(f1[i],f2[i])*2-1);
cout<<ans<<endl;
}
return 0;
}
找一个最长(假设长度为2N-1)的子序列,使得前N个元素递增,后N个元素递减。
和高中一道做过的题目几乎一模一样,noip的合唱队行,只不过这个要求递增和递减的数目相同。
正着求一遍最长上升子序列,再逆着求一遍最长上升子序列,然后枚举每一个数,看起两边延伸的最短的长度乘2减一就好。
由于数据很多,LIS要用nlogn的方法,很简单啦。
// Created by Chenhongwei in 2015.
// Copyright (c) 2015 Chenhongwei. All rights reserved.
#include"iostream"
#include"cstdio"
#include"cstdlib"
#include"cstring"
#include"climits"
#include"queue"
#include"cmath"
#include"map"
#include"set"
#include"stack"
#include"vector"
#include"sstream"
#include"algorithm"
using namespace std;
typedef long long ll;
const int inf=1e8;
int a[10010],n;
int f1[10010],f2[10010],g[10010];
int main()
{
//ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n+1;i++)
g[i]=inf;
for(int i=1;i<=n;i++)
{
int k=lower_bound(g+1,g+n+1,a[i])-g;
f1[i]=k;
g[k]=a[i];
}
for(int i=1;i<=n+1;i++)
g[i]=inf;
for(int i=n;i>=1;i--)
{
int k=lower_bound(g+1,g+n+1,a[i])-g;
f2[i]=k;
g[k]=a[i];
}
int ans=0;
for(int i=1;i<=n;i++)
ans=max(ans,min(f1[i],f2[i])*2-1);
cout<<ans<<endl;
}
return 0;
}
相关文章推荐
- seajs和requiejs的区别,和用gulp打包方法
- Android异步更新UI的四种方式
- tableView点击Cell跳转传值(segue,storyBoard传值)
- Mirantis Fuel调研
- 音频队列Audioqueue播放pcm文件
- Java多线程总结之线程安全队列Queue
- Java continue的关键字
- SpannableString与SpannableStringBuilder
- android - SpannableString或SpannableStringBuilder以及string.xml文件中的整型和string型代替
- getRequestURI,getRequestURL的区别
- 设置UIButton 对齐方式
- 从源码角度分析Android中UID与GID的分配
- IOS_UI_本地存储
- poj 1679 The Unique MST (次小生成树)
- fix issues
- Qt5.4.2+win7的GUI界面设计
- IOS_UI_数据解析
- iOS 在UILabel显示不同的字体和颜色(转)
- [Bluetooth Core V4.2] VOL2, PartB, 7 Bitstream Processing
- Android之学习笔记 Contacts (一)ContentResolver query 参数详解