POJ 1836 Alignment( LIS )
来源:互联网 发布:任我游软件 编辑:程序博客网 时间:2024/05/22 10:26
Alignment
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 13581 Accepted: 4369
Description
In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight line in front of the captain. The captain is not satisfied with the way his soldiers are aligned; it is true that the soldiers are aligned in order by their code number: 1 , 2 , 3 , . . . , n , but they are not aligned by their height. The captain asks some soldiers to get out of the line, as the soldiers that remain in the line, without changing their places, but getting closer, to form a new line, where each soldier can see by looking lengthwise the line at least one of the line's extremity (left or right). A soldier see an extremity if there isn't any soldiers with a higher or equal height than his height between him and that extremity.
Write a program that, knowing the height of each soldier, determines the minimum number of soldiers which have to get out of line.
Write a program that, knowing the height of each soldier, determines the minimum number of soldiers which have to get out of line.
Input
On the first line of the input is written the number of the soldiers n. On the second line is written a series of n floating numbers with at most 5 digits precision and separated by a space character. The k-th number from this line represents the height of the soldier who has the code k (1 <= k <= n).
There are some restrictions:
• 2 <= n <= 1000
• the height are floating numbers from the interval [0.5, 2.5]
There are some restrictions:
• 2 <= n <= 1000
• the height are floating numbers from the interval [0.5, 2.5]
Output
The only line of output will contain the number of the soldiers who have to get out of the line.
Sample Input
81.86 1.86 1.30621 2 1.4 1 1.97 2.2
Sample Output
4
给你n个数,判断最少去掉多少个数,从中间往左是递减的序列,往右是递增的序列
需注意的是中间可能为两个相同的值,如 1 2 3 3 2 1 输出为0
刚看到这个这个题的时候,感觉跟uva 10534那个题很相似,只不过这个存在偶数的情况,在做的时候想当然以为左右对称了,结果写复杂了,改的也比较乱,悲剧的WA了一下午。。。。
正序求一次LIS,得到各个点的最大递增序列的长度(存在数组b中),然后再同样逆序求一次(存在数组b1中),这样我们可以得到以每一个点为中间点,往左递减,往右递增的序列的长度(存在数组s中,,s[i] = b[i] + b1[i]-1 , 减去1是因为求了两次LIS,中间点加了两遍),这样得到的还不是最终的结果,还要考虑中间是两个相同的值的情况,这时可以从两头往中间找,找到相同的两个数,他们对应的s[i]相应加1。。
数组s中的最大值就是站队所需要的最大人数,最后结果为n-max(s)
#include <iostream>#include <stdlib.h>#include <cstdio>using namespace std;const int N = 1010;const int inf = 99999999;int erfen(double key,double c[],int right){ int left=0; int mid=(left + right) / 2; while(left <= right) { if(c[mid] < key) left=mid+1; else if(c[mid] > key) right=mid-1; else return mid; mid=(left + right) / 2; } return left;}int main(){ int n; while(cin>>n) { double a[N],c[N]; int b[N]; double a1[N]; for(int i=0; i<n; i++) { scanf("%lf",&a[i]); a1[n-i-1]=a[i]; } for(int i=0; i<=n; i++) c[i]=inf; c[0]=-1; c[1]=a[0]; b[0]=1; for(int i=1; i<n; i++) //正序求LIS { int j=erfen(a[i],c,n); c[j]=a[i]; b[i]=j; } int b1[N]; double c1[N]; for(int i=0; i<=n; i++) c1[i]=inf; c1[0]=-1; c1[1]=a1[0]; b1[0]=1; for(int i=1; i<n; i++) //逆序求LIS { int j=erfen(a1[i],c1,n); c1[j]=a1[i]; b1[i]=j; } int s[N]; for(int i=0; i<n; i++) s[i]=b[i]+b1[n-i-1]-1; //以每个点为中间点的队列长度 for(int i=0; i<n; i++) { if(i==n-i-1) break; if(a[i]==a1[i]) //判断中间点为两个相同值的情况,如1 2 3 3 2 1 { int x=b[i]+b1[i]; if(x > s[i]) s[i]=x; } } int ans=0; for(int i=0; i<n; i++) if(ans<s[i]) ans=s[i]; printf("%d\n",n-ans); } return 0;}
0 0
- POJ 1836 Alignment( LIS )
- POJ 1836 Alignment (LIS)
- POJ 1836 Alignment DP(LIS)
- POJ 1836--Alignment(LIS,LCS)
- poj 1836 Alignment (DP LIS)
- POJ 1836 Alignment(LIS)
- POJ 1836 Alignment(DP LIS)
- [POJ 1836]Alignment[DP][LIS]
- Poj 1836 Alignment (双重LIS)
- POJ 1836 Alignment DP LIS
- poj 1836 Alignment (LIS变形)
- poj 1836 Alignment(dp,LIS)
- POJ 1836-Alignment(DP/LIS变形)
- poj-1836-Alignment解题报告 LIS
- POJ 1836 Alignment 双向LIS+DP
- POJ 1836 Alignment(DP | LIS)
- POJ 1836 Alignment(LIS变形)
- POJ 1836 Alignment (动态规划LIS)
- 慕课网“顶尖分享”上线,时效课程赢得用户芳心
- primer 1013
- PowerDesigner导出Word
- Android版本和API Level , ADT对应关系
- 随机生成imei imsi mac
- POJ 1836 Alignment( LIS )
- ubuntu 下修改文件访问权限
- 六种姿势拿下连续子序列最大和问题
- sha1算法的C实现与应用
- JLabel、JButton换行问题
- centOS下安装CRF++
- zend studio 12 破解方法(适合Linux,Windows)
- spark内核揭秘-11-Driver中AppClient源码分析
- java虚拟机类初始化顺序