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;
}
目录
相关文章
LeetCode 390. Elimination Game
给定一个从1 到 n 排序的整数列表。 首先,从左到右,从第一个数字开始,每隔一个数字进行删除,直到列表的末尾。 第二步,在剩下的数字中,从右到左,从倒数第一个数字开始,每隔一个数字进行删除,直到列表开头。 我们不断重复这两步,从左到右和从右到左交替进行,直到只剩下一个数字。 返回长度为 n 的列表中,最后剩下的数字。
77 0
LeetCode 390. Elimination Game
|
人工智能
HDU6656——Kejin Player(期望DP+前缀和)
HDU6656——Kejin Player(期望DP+前缀和)
69 0
HDU6656——Kejin Player(期望DP+前缀和)
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
71 0
AtCoder Beginner Contest 203 Pond(二分+二维前缀和)
大体思路: 二分,将原矩阵根据二分的值变成01矩阵,如果元素值> val 就变为1,否则0 对于k * k 的矩阵,统计区域内元素之和,如果 sum < ⌊k2 / 2⌋ + 1,意味着当前k * k矩阵的中位数小于x,而x是我们的答案(最小中位数), ①sum < ⌊k2 / 2⌋ + 1 情况下x取得太大,r = mid ②反之,x还可能取更小的,l = mid 但是需要注意下l的初始值,当取0 or 1的时候是会wa掉的:
215 0
AtCoder Beginner Contest 203 Pond(二分+二维前缀和)
HDOJ(HDU) 2115 I Love This Game(排序排序、、、)
HDOJ(HDU) 2115 I Love This Game(排序排序、、、)
74 0
|
人工智能
Codeforces 839B Game of the Rows【贪心】
B. Game of the Rows time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output...
1140 0
|
人工智能
Educational Codeforces Round 21(A.暴力,B.前缀和,C.贪心)
A. Lucky Year time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard outp...
1276 0

热门文章

最新文章