hdoj Kingdom of Black and White 5583 (模拟&&DP) 好题

211 篇文章 1 订阅
25 篇文章 0 订阅

Kingdom of Black and White

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 185 Accepted Submission(s): 78


Problem Description
In the Kingdom of Black and White (KBW), there are two kinds of frogs: black frog and white frog.

Now N frogs are standing in a line, some of them are black, the others are white. The total strength of those frogs are calculated by dividing the line into minimum parts, each part should still be continuous, and can only contain one kind of frog. Then the strength is the sum of the squared length for each part.

However, an old, evil witch comes, and tells the frogs that she will change the color of at most one frog and thus the strength of those frogs might change.

The frogs wonder the maximum possible strength after the witch finishes her job.

Input
First line contains an integer T , which indicates the number of test cases.

Every test case only contains a string with length N , including only 0 (representing
a black frog) and 1 (representing a white frog).

1T50 .

for 60% data, 1N1000 .

for 100% data, 1N105 .

the string only contains 0 and 1.

Output
For every test case, you should output " Case #x: y",where x indicates the case number and counts from 1 and y is the answer.

Sample Input
  
  
2 000011 0101

Sample Output
  
  
Case #1: 26 Case #2: 10
//唉,写了一下午,都没暴力出来,看了大神的博客,顿时感觉好机智。

定义01串:将串分成最少的连续的块(相同字符),该串价值即为 所有块的字符个数平方 之和。

题意:给定一个01串,最多只能改变一个字符(1->0 或 0->1),问最大价值。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define ll long long
#define N 100010
using namespace std;
struct zz
{
	ll sum;
}num[N];
char s[N];
int main()
{
	int t,T=1,i;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",s);
		int l=strlen(s);
		int top=0,pre=-1;
		for(i=0;i<l;i++)
		{
			int op=s[i]-'0';
			if(op!=pre)
			{
				top++;
				num[top].sum=1;
				pre=op;
			}
			else
				num[top].sum++;
		}
		if(top==1)
		{
			printf("Case #%d: %lld\n",T++,num[top].sum*num[top].sum);
			continue;
		}
		ll ans=0;
		for(i=1;i<=top;i++)
			ans+=num[i].sum*num[i].sum;
		ll ans1=ans;
		for(i=1;i<=top;i++)
		{
			ll tmp=ans;
			ll l=0,r=0,m=num[i].sum;
			if(i==1)
				r=num[i+1].sum;
			else if(i==top)
				l=num[i-1].sum;
			else
			{
				l=num[i-1].sum;
				r=num[i+1].sum;
			}
			tmp-=m*m;
			tmp+=(m-1)*(m-1);
			if(m==1)
			{
				tmp-=l*l+r*r;
				tmp+=(l+r+1)*(l+r+1);
			}
			else
			{
				if(l==0)
				{
					tmp-=r*r;
					tmp+=(r+1)*(r+1);
				}
				else if(r==0)
				{
					tmp-=l*l;
					tmp+=(l+1)*(l+1);
				}
				else
				{
					tmp-=max(l,r)*max(l,r);
					tmp+=(max(l,r)+1)*(max(l,r)+1);
				}
			}
			ans1=max(ans1,tmp);
		}
		printf("Case #%d: %lld\n",T++,max(ans,ans1));
	}
	return 0;
}

//下面的是我写的,感觉考虑的挺全面的,但就是WA。。。

#include<stdio.h>
#include<string.h>
#define N 100010
#define ll long long
#include<algorithm>
using namespace std;
int a[N];
char s[N];
int main()
{
	int t,T=1,i,j,k,l;
	ll sum;
	scanf("%d",&t);
	while(t--)
	{		
		scanf("%s",s);
		l=strlen(s);
		int mm=0;
		int n1=0x3f3f3f;//
		int cnt=0;
		int p;k=0;
		memset(a,0,sizeof(a));
		for(i=0;i<l;i++)
		{
			if(s[i]==s[i+1])
				cnt++;
			else
			{
				cnt++;
				a[k++]=cnt;
				mm=max(mm,cnt);
				n1=min(n1,cnt);
				cnt=0;
			}
		}
		if(n1!=1)
		{
			int nn=100;
			for(i=0;i<k;i++)
			{
				if(a[i]==mm)
				{
					if(i==0)
						nn=min(nn,a[i+1]);
					else if(i==k-1)
						nn=min(nn,a[i-1]);
					else 
						nn=min(nn,min(a[i-1],a[i+1]));
				}
			}
			sum=0;
			for(i=0;i<k;i++)
				sum+=(a[i]*a[i]);
			ll nm=nn*nn+mm*mm;
			ll mn=(nn-1)*(nn-1)+(mm+1)*(mm+1);
			sum-=nm;sum+=mn;
			printf("Case #%d: %lld\n",T++,sum);
		}
		else
		{
			int nn=0x3f3f3f;
			int kk=0;
			int m1=0;
			for(i=0;i<k;i++)
			{
				if(a[i]==1)
				{
					if(m1<a[i+1]+a[i-1]+a[i])
					{
						m1=a[i+1]+a[i-1]+a[i];
						j=i;
					}
				}
			}
			a[j]=0;a[j+1]=m1;a[j-1]=0;
			sum=0;
			for(i=0;i<k;i++)
				sum+=a[i]*a[i];
			printf("Case #:%d %lld\n",T++,sum);
		}
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值