标签:
题目链接:http://www.codeforces.com/problemset/problem/327/A
题意:你现在有n张牌,这些派一面是0,另一面是1。编号从1到n,你需要翻转[i,j]区间的牌一次,使得看到的牌是1的数量最大。
C++代码:
#include <iostream> using namespace std; const int maxn = 110; int n, a[maxn], sum[maxn]; int flip(int L, int R) { int a1 = sum[R] - sum[L-1]; int a2 = R - L + 1 - a1; //cout << "a1=" << a1 << "; a2=" << a2 << ";[" << L << ", " << R << "] == " << sum[n] - sum[R] + sum[L-1] + a2 << endl; return sum[n] - sum[R] + sum[L-1] + a2; } int main() { cin >> n; for (int i = 1; i <= n; i ++) { cin >> a[i]; sum[i] = sum[i-1] + a[i]; } int ans = min(sum[n]+1, n-1); for (int i = 1; i <= n; i ++) for (int j = i; j <= n; j ++) ans = max(ans, flip(i, j)); cout << ans; return 0; }
codeforces水题100道 第二十题 Codeforces Round #191 (Div. 2) A. Flipping Game (brute force)
标签:
原文地址:http://www.cnblogs.com/moonlightpoet/p/5689374.html