您的位置:首页 > 其它

HDU 5583 Kingdom of Black and White 暴力

2017-10-14 18:06 405 查看
题意:

改最多一个位置,使得值最大。值是 连续相同数字个数的平方和

思路:

枚举转折点

#include<cstdio>
#include<algorithm>
#include<string>
#include<iostream>
#include<cstring>
using namespace std;

typedef long long ll;
const int N = 1e5 + 5;
string s;
ll a
, tot, ans, cnt, cal;

ll get(ll k){
    return k * k;
}

int main(){
    ios::sync_with_stdio(0);
    int T;
    cin >> T;
    for(int kase = 1; kase <= T; kase++){
        cin >> s;
        cnt = 1;
        memset(a, 0, sizeof(a));
        tot = cal = 0;
        int len = s.length();
        for(int i = 0; i < len; i++){
            if(i + 1 == len || s[i] != s[i + 1]){
                a[++tot] = cnt;
                cal += cnt * cnt;
                cnt = 1;
            }else cnt++;
        }    
        ans = cal;
        for(int i = 1; i < tot; i++) {
            if(a[i] == 1) {
                ans = max(ans, cal - get(a[i - 1]) - get(a[i + 1]) - 1 + get(a[i - 1] + a[i + 1] + 1));
            }else {
                if(a[i + 1] != 1 || i == tot - 1) {
                    ans = max(ans, cal - get(a[i]) - get(a[i + 1]) + get(a[i] + 1) + get(a[i + 1] - 1));
                }
                ans = max(ans, cal - get(a[i]) - get(a[i + 1]) + get(a[i] - 1) + get(a[i + 1] + 1));
            }
        }
        printf("Case #%d: %lld\n", kase, ans);
    }
    
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: