| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 11559 | Accepted: 3684 |
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
==============================
求个递增求个递减,注意递增递减边界两只是可以等高的,不是严格先递增后递减,然后枚举
#include <iostream>
#include <cstring>
using namespace std;
double a[1111];
int n,ans;
int f[1111],g[1111];
int main()
{
while(cin>>n)
{
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
for(int i=1; i<=n; i++)
{
cin>>a[i];
}
a[0]=-1e9;
a[n+1]=-1e9;
for(int i=1; i<=n; i++)
{
for(int j=0; j<i; j++)
{
if (a[i]>a[j]&&f[j]+1>f[i])
{
f[i]=f[j]+1;
}
}
}
for(int i=n; i>=1; i--)
{
for(int j=n+1; j>i; j--)
{
if(a[i]>a[j]&&g[j]+1>g[i])
{
g[i]=g[j]+1;
}
}
}
ans=0;
for(int i=1; i<=n; i++)
{
for(int j=i+1; j<=n; j++)
{
ans=max(ans,f[i]+g[j]);
}
}
ans=n-ans;
cout<<ans<<endl;
}
return 0;
}
1万+



被折叠的 条评论
为什么被折叠?



