国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

POJ 1836 Alignment (LIS)

2019-11-11 01:04:45
字體:
來源:轉載
供稿:網友

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

81.86 1.86 1.30621 2 1.4 1 1.97 2.2

Sample Output

4

題意

求刪除最少的數,使得序列中任取一個 a[i],使得有 a[0]~a[i] 嚴格遞增,a[i]~a[n-1]嚴格遞減。

思路

首先對于原序列正向、逆向求LIS,然后枚舉左邊遞增序列右端點與右邊遞減序列左端點,求得該序列的最長遞增遞減子序列長度,需要刪除的數的個數即是剩余的數的個數。

AC 代碼

#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>using namespace std;#include<vector>#include<queue>double a[1005];int prev[1005],next[1005];int lis(int n){ int ans=0; prev[0]=next[n-1]=1; for(int i=1; i<n; i++) { prev[i]=1; for(int j=0; j<i; j++) if(a[i]>a[j]) prev[i]=max(prev[i],prev[j]+1); } for(int i=n-2; i>=0; i--) { next[i]=1; for(int j=n-1; j>i; j--) if(a[j]<a[i]) next[i]=max(next[i],next[j]+1); } for(int i=0; i<n-1; i++) for(int j=i+1; j<n; j++) ans=max(ans,prev[i]+next[j]); return ans;}int main(){ int n; while(cin>>n) { for(int i=0; i<n; i++) cin>>a[i]; cout<<n-lis(n)<<endl; } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湘潭县| 全椒县| 木里| 手游| 平舆县| 巴林左旗| 定南县| 泰和县| 太和县| 静宁县| 安多县| 斗六市| 湘潭市| 乐都县| 南靖县| 西乌珠穆沁旗| 卫辉市| 独山县| 临朐县| 右玉县| 明光市| 平武县| 上虞市| 奇台县| 余姚市| 安塞县| 永康市| 黄石市| 孝感市| 泾川县| 淳化县| 开化县| 长治县| 抚顺县| 杨浦区| 文山县| 石楼县| 新宁县| 连城县| 梁平县| 车致|