Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 17331 | Accepted: 5694 |
Description
Write a program that, knowing the height of each soldier, determines the minimum number of soldiers which have to get out of line.
Input
There are some restrictions:
• 2 <= n <= 1000
• the height are floating numbers from the interval [0.5, 2.5]
Output
Sample Input
8 1.86 1.86 1.30621 2 1.4 1 1.97 2.2
Sample Output
4
Source
1 #include <iostream> 2 #include<algorithm> 3 #include<stack> 4 #include<cstdio> 5 #include<cstring> 6 using namespace std; 7 #define inf 0x3f3f3f3f 8 typedef long long LL; 9 const int MAX = 1005; 10 int a[MAX], b[MAX], g[MAX]; 11 int solve(int m,int n) 12 { 13 memset(g, inf, sizeof(g)); 14 int i, j, k1=0, k2=0; 15 for (i = 1;i < m;++i) 16 { 17 int k = lower_bound(g,g+n+1,a[i])-g; 18 g[k] = a[i]; 19 }k1 = lower_bound(g, g + n+1, a[m]) - g+1; 20 memset(g, inf, sizeof(g)); 21 for (i = 1;i <=n-m;++i) 22 { 23 if (b[i] > a[m]) continue; 24 int k = lower_bound(g, g + n+1, b[i]) - g; 25 g[k] = b[i]; 26 k2 = max(k2,k+1); 27 } 28 //printf("%d %d\n", k1, k2); 29 return n - (k1 + k2); 30 } 31 int main() 32 { 33 int N, i, j, k ; 34 double d; 35 while (cin >> N) { 36 int ans = inf; 37 for (i = 1;i <= N;++i)scanf("%lf", &d), a[i] = b[N + 1 - i] = d * 100000; 38 for (i = 1;i <= N;++i) { 39 ans = min(ans, solve(i, N)); 40 } 41 printf("%d\n", ans); 42 } 43 return 0; 44 }