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

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

POJ 1836 Alignment (LIS)

2019-11-11 02:18:19
字體:
來源:轉載
供稿:網友

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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 胶南市| 山西省| 徐汇区| 广灵县| 甘洛县| 黄陵县| 英吉沙县| 蕉岭县| 宁德市| 金门县| 商城县| 尼勒克县| 靖州| 鄂伦春自治旗| 石阡县| 太仆寺旗| 庆元县| 文水县| 库尔勒市| 文山县| 竹山县| 福清市| 伊金霍洛旗| 赣榆县| 广东省| 正宁县| 巴彦淖尔市| 囊谦县| 海伦市| 河北区| 武安市| 固镇县| 金堂县| 象州县| 沂源县| 阿瓦提县| 汝南县| 中宁县| 汪清县| 长垣县| 白玉县|