Alignment poj1836
来源:互联网 发布:软件测试人员保密协议 编辑:程序博客网 时间:2024/06/05 10:36
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.
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]
Output
The only line of output will contain the number of the soldiers who have to get out of the line.
Sample Input
8
1.86 1.86 1.30621 2 1.4 1 1.97 2.2
Sample Output
4
(1)题意:有士兵n个,根据编号排为一列,但是身高不一,现在要求去掉几个人,使得剩下的每一个人可以向左或向右看到队头,就是使新队列呈三角形分布。
(2)解法:dp-LIS 从左到右求最长递减子串dp1,从右到左也求最长递减子串dp2.然后枚举位置i 使得dp1[i]+dp2[i]最大,则为所求结果。
#include<cstdio>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>using namespace std;double a[1005];int main(){ int n; while(scanf("%d",&n)==1) { int i,j; int dp1[1005]={1},dp2[1005]={1}; for(i=0;i<n;i++) { scanf("%lf",&a[i]); dp1[i]=1; dp2[i]=1; } for(i=1;i<n;i++) //sheng { for(j=i-1;j>=0;j--) { if(a[i]>a[j]&&dp1[j]+1>dp1[i]) dp1[i]=dp1[j]+1; } } for(i=n-2;i>=0;i--) { for(j=i+1;j<n;j++) { if(a[i]>a[j]&&dp2[j]+1>dp2[i]) dp2[i]=dp2[j]+1; } } int cnt=0; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) cnt=max(cnt,dp1[i]+dp2[j]); } printf("%d\n",n-cnt); } return 0;}
- POJ1836--Alignment
- poj1836 Alignment
- poj1836 Alignment
- poj1836 Alignment
- poj1836 Alignment
- poj1836 Alignment
- Alignment poj1836
- 【POJ1836】【Alignment】
- poj1836 Alignment
- POJ1836-Alignment
- [POJ1836]Alignment
- POJ1836 Alignment DP
- POJ1836——Alignment
- poj1836--Alignment题解
- poj1836(dp) Alignment
- POJ1836——Alignment
- poj1836 Alignment(LIS)
- poj1836 Alignment LIS傻逼题
- maven 安装第三方构建到本地仓库
- QListWidget的item上实现右键菜单
- hd-1095:A+B for Input-Output Practice (VII)
- lnmp+wordpress: 您的PHP似乎没有安装运行WordPress所必需的MySQL扩展
- spring mongodb数组修改器—$push、$ne、$addtoset、$pop、$pull
- Alignment poj1836
- 算法学习之java实现希尔排序
- 如何访问DataTable中的某个元素
- 菲波那契数列
- 黑马程序员—JAVA基础—集合类
- iGrimace IG 3.0 VX v3 iOS神器 新机 抹机 优步Uber 陌陌 微信用igrimace v3
- [转载]转:CreateFileMapping实现的共享内存及用法
- 文章标题
- Linux kernel 分析之九:fork()系统调用