ACM刷题之hdu————Kingdom of Black and White

这是一篇关于ACM竞赛中的一道模拟题——Kingdom of Black and White的解析。文章介绍了如何将区间转化为数字形式进行储存,并探讨了求解问题的关键在于最大化(a[i] + 1)^2 - (a[i - 1])^2 - a[i]^2和(a[i] + 1)^2 - (a[i + 1])^2 - a[i]^2的值。文章还提供了AC代码作为解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    

Kingdom of Black and White

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


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

 
20000110101
 

Sample Output

 
Case #1: 26Case #2: 10
 

Source



一道模拟题

可以把所有的区间都转成数字的形式储存,如 10011 可以转成1,2, 2

然后我们让 (a[i] + 1 )^2 - (a[i -1])^2 - a[i]^2  和 a[i] + 1 )^2 - (a[i +1])^2 - a[i]^2 的值最大就好了


下面是ac代码

#include<bits/stdc++.h>
using namespace std;
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int N=2e5+7;


char a[N];

vector<long long> v;

int main()
{
//	freopen("f:/input.txt", "r", stdin);
	int n, i , j , k;
	scanf("%d", &n);
		for (int co = 1; co <= n; co ++) {
			v.clear();
			CLR(a,0);
			scanf("%s", a);
			int le = strlen(a);
			int cnt = 1;
			int maxn = -INF, maxi = -INF, vi;
			for (i = 1 ; i < le ; i ++) {
				if (a[i] == a[i - 1]) {
					++cnt;
				} else {
					v.push_back(cnt);
					if (cnt > maxn) {
						maxn = cnt;
						//结束 i
						maxi = i - 1;
						vi = v.size();
					}

					cnt = 1;

				}
			}
			v.push_back(cnt);
			
			
			long long sum = 0;
			int vs = v.size();
			
			long long ct = -INF; 
			int cti = -1;
			
			for (i = 1; i < vs; i ++) {
				long long cct;
				if (v[i - 1] != 1)
				cct = (v[i] + 1)*(v[i] + 1) - (v[i - 1] ) * (v[i - 1] );
				else {
					if (i - 2 >= 0)
					cct = (v[i] + 1 + v[i - 2])*(v[i] + 1 + v[i - 2]) - 1 - v[i - 2] * v[i - 2];
					else 
					cct = (v[i] + 1)*(v[i] + 1 ) - 1;

				}
				cct -= (v[i] * v[i]);
				if (cct > ct) {
					ct = cct;
					cti = i;
				}
			}
			int cti2 = -1;
			for (i = 1; i < vs; i ++) {
				long long cct ;
				if (v[i] != 1)
				cct = (v[i - 1] + 1) * (v[i - 1] + 1)  - (v[i] ) * (v[i] );
				else {
					if (i < vs - 1)
					cct = (v[i - 1] + 1 + v[i + 1]) * (v[i - 1] + 1 + v[i + 1]) - 1 - v[i + 1] * v[i + 1];
					else 
					cct = (v[i - 1] + 1) * (v[i - 1] + 1) - 1;
				}
				cct -= (v[i - 1] * v[i - 1]);
				if (cct > ct) {
					ct = cct;
					cti2 = i;
				}
			}
			
			if (cti2 != -1) {
				int sum1 = 0;
				for (i = 0; i < cti2; i++) {
					sum1 += v[i];
				}
//				++sum1;
				if (a[sum1] == '0') a[sum1] = '1';
				else a[sum1] = '0';
			} else {
				int sum1 = 0;
				for (i = 0; i < cti; i++) {
					sum1 += v[i];
				}
				--sum1;
//				++sum1;
				if (a[sum1] == '0') a[sum1] = '1';
				else a[sum1] = '0';
			}
			
			v.clear();
			cnt = 1;
			for (i = 1 ; i < le ; i ++) {
				if (a[i] == a[i - 1]) {
					++cnt;
				} else {
					v.push_back(cnt);
					if (cnt > maxn) {
						maxn = cnt;
						//结束 i
						maxi = i - 1;
						vi = v.size();
					}

					cnt = 1;

				}
			}
			v.push_back(cnt);

			vs = v.size();
			for (i = 0; i < vs; i ++) {
				sum += (long long)v[i] * v[i];
			}
			printf("Case #%d: %lld\n", co, sum);
		}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值