国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扎赉特旗| 龙里县| 台湾省| 朔州市| 西平县| 武强县| 齐齐哈尔市| 碌曲县| 庐江县| 绥芬河市| 德安县| 佛教| 历史| 萝北县| 梨树县| 林甸县| 密云县| 肥城市| 五大连池市| 巧家县| 西宁市| 岑溪市| 绿春县| 长治县| 原阳县| 西安市| 滕州市| 庐江县| 炉霍县| 东平县| 沂水县| 洛浦县| 繁昌县| 池州市| 化德县| 葫芦岛市| 万安县| 保靖县| 岚皋县| 临安市| 青神县|