记录一个菜逼的成长。。
题目大意:两棵树,有T秒钟,每秒钟都会下落一个苹果,接下来T行表示下落苹果的树的编号。最多有W次交换次数,初始在1树。
dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]);
还要判断现在所在的树是否有苹果落下。
还要加上(j%2) + 1 == tree[i]; 0次是在1树, 所以如果交换次数是奇数则在第二棵树,判断i秒钟苹果是否在第二棵树落下
PS:DP的题还是要记一下。。话说刚开始没看懂什么意思。。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <deque>
#include <cctype>
#include <bitset>
#include <cmath>
using namespace std;
#define ALL(v) (v).begin(),(v).end()
#define cl(a) memset(a,0,sizeof(a))
#define bp __builtin_popcount
#define pb push_back
#define fin freopen("D://in.txt","r",stdin)
#define fout freopen("D://out.txt","w",stdout)
#define lson t<<1,l,mid
#define rson t<<1|1,mid+1,r
#define seglen (node[t].r-node[t].l+1)
#define pi 3.1415926
#define e 2.718281828459
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
template <typename T>
inline void read(T &x){
T ans=0;
char last=' ',ch=getchar();
while(ch<'0' || ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans;
x = ans;
}
inline bool DBread(double &num)
{
char in;double Dec=0.1;
bool IsN=false,IsD=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&in!='.'&&(in<'0'||in>'9'))
in=getchar();
if(in=='-'){IsN=true;num=0;}
else if(in=='.'){IsD=true;num=0;}
else num=in-'0';
if(!IsD){
while(in=getchar(),in>='0'&&in<='9'){
num*=10;num+=in-'0';}
}
if(in!='.'){
if(IsN) num=-num;
return true;
}else{
while(in=getchar(),in>='0'&&in<='9'){
num+=Dec*(in-'0');Dec*=0.1;
}
}
if(IsN) num=-num;
return true;
}
template <typename T>
inline void write(T a) {
if(a < 0) { putchar('-'); a = -a; }
if(a >= 10) write(a / 10);
putchar(a % 10 + '0');
}
/******************head***********************/
const int maxn = 1000 + 10;
int dp[maxn][35],tree[maxn];
int main()
{
int T,W;
while(~scanf("%d%d",&T,&W)){
cl(dp);
for( int i = 1; i <= T; i++ ){
scanf("%d",tree+i);
}
for( int i = 1; i <= T; i++ ){
dp[i][0] = dp[i-1][0] + (tree[i] == 1);//交换0次是在1树,判断此时是否有苹果落下。
for( int j = 1; j <= W; j++ ){
dp[i][j] = max(dp[i-1][j] , dp[i-1][j-1]) + ((j % 2) + 1 == tree[i]);
}
}
int ans = 0;
//不一定是交换次数最大 ,收到的苹果最多。
for( int i = 1; i <= W; i++ ){
ans = max(dp[T][i],ans);
}
printf("%d\n",ans);
}
return 0;
}