题目链接: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 = ;
int n, a[maxn], sum[maxn];
int flip(int L, int R)
{
int a1 = sum[R] - sum[L-];
int a2 = R - L + - a1;
//cout << "a1=" << a1 << "; a2=" << a2 << ";[" << L << ", " << R << "] == " << sum[n] - sum[R] + sum[L-1] + a2 << endl;
return sum[n] - sum[R] + sum[L-] + a2;
}
int main()
{
cin >> n;
for (int i = ; i <= n; i ++)
{
cin >> a[i];
sum[i] = sum[i-] + a[i];
}
int ans = min(sum[n]+, n-);
for (int i = ; i <= n; i ++)
for (int j = i; j <= n; j ++)
ans = max(ans, flip(i, j));
cout << ans;
return ;
}
C++