codeforces327——A. Flipping Game(前缀和)

简介: codeforces327——A. Flipping Game(前缀和)

原题链接

题意:

给定一个01序列,可以翻转一次区间,求最多得到的1的个数。

思路:

大意了啊没有闪

上来莽了一波错误思路,翻转最长的0的子串。

hack样例

/**
7
0 0 0 1 0 0 0
**/

正解:

考虑每次翻转对答案的贡献,翻转就是0变为1,1变为0,所以变化的1的个数就是该段区间0的数量-1的数量,取max就好了。

代码:

#pragma GCC optimize(3)
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
char F[200];
inline void out(I_int x) {
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0) {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
    //cout<<" ";
}
ll ksm(ll a,ll b,ll p){ll res=1;while(b){if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+100,N=1e6+100;
const double PI = atan(1.0)*4;
const double eps=1e-6;
int a[110],n,m;
/**
7
0 0 0 1 0 0 0
**/
int sum1[110],sum2[110];
int main(){
    n=read();
    int cnt=0;
    for(int i=1;i<=n;i++){
        a[i]=read();
        if(a[i]==0) sum1[i]+=sum1[i-1]+1,sum2[i]+=sum2[i-1];
        else  sum2[i]+=sum2[i-1]+1,sum1[i]+=sum1[i-1],cnt++;
    }
    int maxx=0;
    for(int l=1;l<=n;l++)
        for(int r=l;r<=n;r++)
            maxx=max(maxx,cnt+(sum1[r]-sum1[l-1])-(sum2[r]-sum2[l-1]));
    out(maxx);
    return 0;
}
目录
相关文章
|
机器学习/深度学习 人工智能 vr&ar
A. Mocha and Math(codeforces#738(div2))
A. Mocha and Math(codeforces#738(div2))
62 0
codeforces292——D. Connected Components(并查集+前缀和优化)
codeforces292——D. Connected Components(并查集+前缀和优化)
112 0
codeforces292——D. Connected Components(并查集+前缀和优化)
|
人工智能
HDU6656——Kejin Player(期望DP+前缀和)
HDU6656——Kejin Player(期望DP+前缀和)
111 0
HDU6656——Kejin Player(期望DP+前缀和)
|
人工智能 JavaScript BI
AtCoder Beginner Contest 222 D - Between Two Arrays(前缀和优化dp)
AtCoder Beginner Contest 222 D - Between Two Arrays(前缀和优化dp)
120 0
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
102 0
|
人工智能 vr&ar
Atcoder--Candy Distribution II--前缀和+map
题目描述 There are N boxes arranged in a row from left to right. The i-th box from the left contains Ai candies. You will take out the candies from some consecutive boxes and distribute them evenly to M children. Such being the case, find the number of the pairs (l,r) that satisfy the following:
114 0
HDOJ(HDU) 2115 I Love This Game(排序排序、、、)
HDOJ(HDU) 2115 I Love This Game(排序排序、、、)
103 0
零元学Expression Blend 4 &ndash; Chapter 43 如何指定Childwindow PopUp位置
原文:零元学Expression Blend 4 &ndash; Chapter 43 如何指定Childwindow PopUp位置 有网友询问我有关Childwindow是否能指定弹出位置? 其实只...
1320 0