Kingdom of Black and White(模拟)
http://acm.hdu.edu.cn/showproblem.php?pid=5583
题意: 给你一个只含有01的字符串,然后这个串的权值就是每一段连续的0或1的长度的平方和,然后你可以修改一个数,使得这个数变成0,或者使这个数变成1
然后问你最大权值能为多少。
解法:暴力枚举改变每一个联通块,注意当联通块值为1时,需合并3块联通块。
#include<bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <string> #include <stdio.h> #include <queue> #include <stack> #include <map> #include <set> #include <string.h> #include <vector> using namespace std; typedef long long ll ; #define int ll #define mod 1000000007 #define gcd(m,n) __gcd(m, n) #define rep(i , j , n) for(int i = j ; i <= n ; i++) #define red(i , n , j) for(int i = n ; i >= j ; i--) #define ME(x , y) memset(x , y , sizeof(x)) int lcm(int a , int b){return a*b/gcd(a,b);} //ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;} //int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;} //const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len} #define INF 0x3f3f3f3f #define PI acos(-1) #define pii pair<int,int> #define fi first #define se second #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define pb push_back #define mp make_pair #define all(v) v.begin(),v.end() #define size(v) (int)(v.size()) #define cin(x) scanf("%lld" , &x); const int N = 1e7+9; const int maxn = 1e5+9; const double esp = 1e-6; char s[maxn]; int cnt ; vector<int>v; void solve(){ vector<int>v; int sum = 0; scanf("%s" , s+1); int n = strlen(s+1); int ans = 1 ; cout << "Case #" << ++cnt << ": " ; rep(i , 2 , n){ if(s[i] == s[i-1]){ ans++; }else{ sum += ans * ans ; v.pb(ans); ans = 1 ; } } v.pb(ans); sum += ans * ans ; int num = sum ; if(size(v) == 1){ cout << sum << endl; return ; } for(int i = 0 ; i < size(v)-1 ; i++){ if(i != size(v)-1 && i != 0 && v[i] == 1){ int a = v[i-1]*v[i-1] + v[i]*v[i] + v[i+1]*v[i+1] , b = (v[i-1]+v[i]+v[i+1])*(v[i-1]+v[i]+v[i+1]); sum =max(sum , num - a + b) ; }else{ int a = (v[i]*v[i] + v[i+1]*v[i+1]) , b = ((v[i]-1)*(v[i]-1))+((v[i+1]+1)*(v[i+1]+1)); sum =max(sum , num - a + b) ; b = ((v[i]+1)*(v[i]+1))+((v[i+1]-1)*(v[i+1]-1)); sum =max(sum , num - a + b) ; } } cout << sum << endl; } signed main() { int t; scanf("%lld" , &t); while(t--) solve(); }