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

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

POJ 1836 Alignment (LIS)

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

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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 奇台县| 简阳市| 繁峙县| 原阳县| 丹巴县| 安仁县| 枣阳市| 宽城| 抚州市| 平度市| 崇州市| 灵石县| 塔河县| 柳州市| 开阳县| 玉山县| 漯河市| 凉城县| 巍山| 安阳市| 达日县| 东山县| 科技| 新田县| 三亚市| 香河县| 临颍县| 句容市| 绥阳县| 榆中县| 育儿| 长顺县| 图木舒克市| 永丰县| 蕲春县| 甘肃省| 溆浦县| 蒲江县| 晋江市| 京山县| 淮滨县|