比赛 20121106 评测结果 WWWAWAWAWWWWWWW
题目名称 二十一点 最终得分 20
用户昵称 feng 运行时间 0.112 s
代码语言 C++ 内存使用 16.26 MiB
提交时间 2012-11-06 11:53:17
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
struct node{
	int s,nex;
}a[1100];
struct edge{
	int y,v,nex;
}e[800000];
bool vis[1010];
int f[1010][1010];
int aa[1010];
int dis[1010];
int queue[10000];
int DP[1010];
int n,i,j,p,k;
bool cmp(int a,int b){
	return a>b;
}
void dfs(int i,int j,int sum1,int sum2){
	if (sum2>21){
		f[i][j]=-1;
		return;
	}
	if (sum1>=sum2) f[i][j]=-1;
	if (sum1<sum2)  f[i][j]=1;
	bool judge=false;
	if (sum1<=16){
		if (j<n){
			judge=true;
			sum1+=aa[++j];
			if (sum1>21){
				f[i][j]=1;
				return ;
			}
		}
	}
		if (j<n)	dfs(i,j+1,sum1,sum2+aa[j+1]);
		if (judge){
			while (sum1<=16){
				sum1+=aa[++j];
			}
			if (sum1>21){
				f[i][j]=1;
				return ;
			}
			if (sum1>=sum2) f[i][j]=-1;
			if (sum1<sum2)  f[i][j]=1;
		}
		/*if (!judge){
				if (sum1>=sum2) f[i][j]=-1;
				if (sum1<sum2)  f[i][j]=1;
		}*/
		if (!judge){
			while (sum2<=21){
				if (sum1>=sum2) f[i][j]=-1;
				if (sum1<sum2)  f[i][j]=1;
				sum2+=aa[++j];
			}
			if (sum2>21){
				f[i][j]=-1;
				return;
			}
		}
}
void add(int x,int y,int v){
	p++;
	a[x].s++;
	e[p].y=y;
	e[p].v=v;
	e[p].nex=a[x].nex;
	a[x].nex=p;
}
void SPFA(int s){
	int t;
	int tmp;
	memset(vis,true,sizeof(vis));
	memset(dis,1,sizeof(dis));
	memset(queue,0,sizeof(queue));
	vis[s]=false;
	dis[s]=0;
	int head=0;
	int tail=1;
	queue[tail]=s;
	while (head<=tail){
		head++;
		tmp=queue[head];
		vis[tmp]=true;
		t=a[tmp].nex;
		for (int i=1;i<=a[tmp].s;i++){
			if (dis[tmp]+e[t].v<dis[e[t].y]){
				dis[e[t].y]=dis[tmp]+e[t].v;
				if (vis[e[t].y]){
					vis[e[t].y]=false;
					queue[++tail]=e[t].y;
				}
			}
			t=e[t].nex;
		}
	}
}
int maxx(int a,int b){
	return a>b?a:b;
}
int main()
{
	freopen("jack.in","r",stdin);
	freopen("jack.out","w",stdout);
	scanf("%d",&n);
	for (i=1;i<=n;i++)
		scanf("%d",&aa[i]);
	memset(f,0,sizeof(f));
	for (i=1;i<=n-5;i++){
		dfs(i,i+3,aa[i]+aa[i+2],aa[i+1]+aa[i+3]);
	}
	for (i=1;i<=n;i++)
		for (j=1;j<=n;j++){
			if (f[i][j]==0) f[i][j]=-2;
			if (f[i][j]==-1) f[i][j]=0;
		}
	for (i=1;i<=n;i++)
		for (j=1;j<=n;j++){
			if (f[i][j]==0) add(i,j+1,0);
			if (f[i][j]==1) add(i,j+1,-1);
		}
		
	memset(DP,-1,sizeof(DP));
	DP[0]=0;
	for (i=1;i<=n;i++)
		for (j=0;j<i-4;j++){
				if (f[j+1][i]>=0)
					if (DP[i]<0 && DP[j]>=0) DP[i]=0;
				if (DP[i]>=0)
					DP[i]=maxx(DP[i],f[j+1][i]+DP[j]);
		}
	printf("%d\n",DP[n]);
	//SPFA(1);
	//	int maxn=0;
	//for (i=1;i<=n+1;i++)
	//	if (dis[i]<maxn) maxn=dis[i];
	//printf("%d",-maxn);
	return 0;
}