• 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;
    }
    
    
  • 相关阅读:
    spring mvc随便接收list<objeect>参数
    python django model类型摘要
    【Unity3D自我记录】解决NGUI通过问题触发事件点
    sqlcipher移植
    外键约束列并没有导致大量建筑指数library cache pin/library cache lock
    34一个美丽的生活窍门
    html表格合并(行,一排)
    01标题背包水章 HDU2955——Robberies
    苹果Swift编程语言新手教程【中国版】
    神经网络和BP算法推导
  • 原文地址:https://www.cnblogs.com/OvOq/p/14853081.html
Copyright © 2020-2023  润新知