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

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

POJ 1836 Alignment (LIS)

2019-11-11 00:09:35
字體:
來源:轉載
供稿:網友

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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宽城| 松溪县| 鞍山市| 商丘市| 炉霍县| 津南区| 额尔古纳市| 库尔勒市| 鲁甸县| 广水市| 神池县| 金平| 双江| 南木林县| 北碚区| 庄河市| 墨玉县| 开江县| 建阳市| 六枝特区| 岳阳市| 宜兴市| 姚安县| 襄城县| 石林| 全南县| 饶平县| 房产| 读书| 克拉玛依市| 蕲春县| 翁源县| 绥德县| 苗栗市| 桑日县| 炎陵县| 辽阳市| 康定县| 凌源市| 河曲县| 阿鲁科尔沁旗|