HDU 5583 Kingdom of Black and White [Ad Hoc]
来源:互联网 发布:熟练使用办公软件 编辑:程序博客网 时间:2024/05/20 16:42
2015上海区域赛 K题
Description
给一串0和1,然后你可以变一个
这样就分成了若干块
求然后每块长度的平方和最大
Algorithm
两种情况
1.长度大于1
这样变两头
2.长度=1,这样3段就练成一段
Hint
貌似必须用vector,不然超时?
Code
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>#include <vector>using namespace std;string s;void solve(){ vector<long long> a; cin>>s; a.push_back(0); a.push_back(1); long long ans0 = 0; for (int i = 1; i < s.size(); i++) if (s[i] == s[i - 1]) a[a.size() - 1]++; else { a.push_back(1); } a.push_back(0); for (int i = 0; i < a.size(); i++) ans0 += (a[i] * a[i]); long long ans = ans0; for (int i = 1; i < a.size() - 1; i++) if (a[i] > 1) { long long t = ans0 + (a[i] - 1) * (a[i] - 1) + (a[i + 1] + 1) * (a[i + 1] + 1) - a[i] * a[i] - a[i + 1] * a[i + 1]; if (t > ans) ans = t; t = ans0 + (a[i] - 1) * (a[i] - 1) + (a[i - 1] + 1) * (a[i - 1] + 1) - a[i] * a[i] - a[i - 1] * a[i - 1]; if (t > ans) ans = t; }else { long long t = ans0 + (a[i - 1] + 1 + a[i + 1]) * (a[i - 1] + 1 + a[i + 1]) - (a[i - 1] * a[i - 1] + a[i] * a[i] + a[i + 1] * a[i + 1]); if (t > ans) ans = t; } cout << ans << endl;}int main(){// freopen("input.txt", "r", stdin); int n; scanf("%d\n", &n); for (int i = 1; i <= n; i++) { printf("Case #%d: ", i); solve(); }}
0 0
- HDU 5583 Kingdom of Black and White [Ad Hoc]
- 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
- 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(模拟)
- HDU 5583 Kingdom of Black and White 暴力
- HOOJ1290 2050(递推)
- c++作业6
- Android Notification的基本应用 (8.1)
- HTML---网络编程基础知识(2)
- Android高级UI SwipeRefreshLayout下拉刷新
- HDU 5583 Kingdom of Black and White [Ad Hoc]
- hdu1556Color the ball(线段树)
- go语言的变量声明
- 逻辑运算符与关系运算符
- hihoCoder 1287 Miller-Rabin质数测试
- 问题 F: 因子和
- 使用Android studio 开发xposed插件
- 一些其它
- C语言中的atan和atan2