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);
}
}
改最多一个位置,使得值最大。值是 连续相同数字个数的平方和
思路:
枚举转折点
#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);
}
}
相关文章推荐
- HDU-5583-Kingdom of Black and White【2015上海赛区】【暴力】
- hdu 5583 Kingdom of Black and White【枚举】
- HDU-5583 Kingdom of Black and White(思维)
- HDU 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White
- HDU - 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White 水题
- hdu 5583 Kingdom of Black and White
- HDU-5583-Kingdom of Black and White(2015ACM/ICPC亚洲区上海站-重现赛)
- HDU 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White 2015ACM/ICPC亚洲区上海站-重现赛
- hdu 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- hdu 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White
- HDU 5583 Kingdom of Black and White
- hhu Kingdom of Black and White 暴力 TWT Tokyo Olympic 1COMBO -1
- hdu-5583 Kingdom of Black and White(数学,贪心,暴力)
- HDU 5583 Kingdom of Black and White (暴力)
- 文章标题 HDU 5583:Kingdom of Black and White(暴力+代码实现能力)