UVa481 What Goes Up
来源:互联网 发布:redis php扩展 编辑:程序博客网 时间:2024/06/10 22:45
题目大意:求LIS,同时输出该序列。
思路:发现了一个叫lower_bound( )的函数,直接用它二分即可。不过要输出序列,需要记录各元素在二分中的序列中的位置(即以该元素为尾的LIS长度)。然后从后往前找一遍,第一个出现的代表某长度的元素即为序列中的元素(因为该元素最晚被更新),输出即可。
代码如下:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=100005;int l=0,a[maxn],c[maxn],pos[maxn],ori[maxn];bool vis[maxn];void work(){ int len=0; c[0]=-10000; for (int i=1;i<=l;++i) { if (a[i]>c[len]) { len++; pos[i]=len; c[len]=a[i]; } else { int p=lower_bound(c+1,c+len+1,a[i])-c; pos[i]=p; c[p]=a[i]; } } cout<<len<<endl<<'-'<<endl; int p=len,from; bool flag=0; for (int i=l;i>=1;--i) { if (pos[i]==len) { from=i; break; } } ori[len]=a[from]; p--; for (int i=from-1;i>=1;--i) { if (pos[i]==p && a[i]<a[from]) { from=i; ori[p]=a[i]; p--; } } for (int i=1;i<=len;++i) cout<<ori[i]<<endl;}void init(){ int tmp; memset(vis,0,sizeof(vis)); while (scanf("%d",&tmp)==1) { a[++l]=tmp; }}int main(){ init(); work(); return 0;}
0 0
- UVa481 - What Goes Up
- uva481 What Goes Up
- UVa481 What Goes Up
- UVa 481 - What Goes Up
- Uva 481 - What Goes Up
- UVA 481 what goes up
- UVa:481 What Goes Up
- UVA 481 What Goes Up LIS+nlog(n)算法,打印路径
- 【UVA481】
- what's up
- what's up
- USA : Tandy Leather's retail leathercraft segment goes up 38%
- What goes in to the message of a transaction signature?
- What's up with BeginInvoke?
- Windows 2000 Loader What Goes On Inside Windows 2000: Solving the Mysteries of the Loader
- Windows 2000 Loader What Goes On Inside Windows 2000: Solving the Mysteries of the Loader
- What Goes On Inside Windows 2000: Solving the Mysteries of the Loader2
- NTDLL.dll1 : What Goes On Inside Windows 2000: Solving the Mysteries of the Loader
- Libevent实例之系统密码保护程序实现
- 发送短信工具类
- 解读ASP.NET 5 & MVC6系列(5):Configuration配置信息管理
- 蓝牙开启流程(enable)分析
- 动态修改spring job定时器
- UVa481 What Goes Up
- Android Studio中提示:Project SDK is not defined
- struts2学习03——项目走向流转
- Impala002-Impala架构
- 判断当前网络是否可用
- table_schem是什么意思
- 黑马程序员——Java基础---集合类(上)
- leetcode 27:Remove Element
- PCI总线(外设部件互连标准)