题目
A. Curriculum Vitae
1 second 256 megabytes
Hideo Kojima has just quit his job at Konami. Now he is going to find a new place to work. Despite being such a well-known person, he still needs a CV to apply for a job.
During all his career Hideo has produced n games. Some of them were successful, some were not. Hideo wants to remove several of them (possibly zero) from his CV to make a better impression on employers. As a result there should be no unsuccessful game which comes right after successful one in his CV.
More formally, you are given an array s1, s2, ..., sn of zeros and ones. Zero corresponds to an unsuccessful game, one — to a successful one. Games are given in order they were produced, and Hideo can't swap these values. He should remove some elements from this array in such a way that no zero comes right after one.
Besides that, Hideo still wants to mention as much games in his CV as possible. Help this genius of a man determine the maximum number of games he can leave in his CV.
Input
The first line contains one integer number n (1 ≤ n ≤ 100).
The second line contains n space-separated integer numbers s1, s2, ..., sn (0 ≤ si ≤ 1). 0 corresponds to an unsuccessful game, 1 — to a successful one.
Output
Print one integer — the maximum number of games Hideo can leave in his CV so that no unsuccessful game comes after a successful one.
题意
给你长度为的
数列,问1后面不能接0的最大长度是多少?
思路
我一开始做这个题读题后想着,1后面不能紧接着0,那么我可以接受0后面跟0,也就是{1,0,0},写出了这样的代码,一开始ans设置为1.
if (a[i-1]==1&&a[i]==0) ans--;
wa了之后是我多少沾点了,1后面不能紧跟0,那么后面的0补上不也是1后面紧跟0了?
这个题可以减少1的出现,使得更可能多的0出来,这样就是动态规划,其实就是最长不下降子序列的板子题,00000...111111...
直接代板子。
代码
#include<stack>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<iomanip>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=1007;
int a[N],d[N],len,n,pos[N],ans[N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("IO\\in.txt","r",stdin);
freopen("IO\\out.txt","w",stdout);
#endif
IOS;
cin>>n;
for (int i=1;i<=n;i++) cin>>a[i];
d[1]=a[1];len=1;
for (int i=2;i<=n;i++)
{
if (a[i]>=d[len]) d[++len]=a[i],pos[i]=len;
else
{
int t=upper_bound(d+1,d+1+len,a[i])-d;
d[t]=a[i];
}
}
cout<<len<<endl;
return 0;
}