AtCoder Beginner Contest 124 D - Handstand(思维+前缀和)

D - Handstand



Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 400400 points

Problem Statement

NN people are arranged in a row from left to right.

You are given a string SS of length NN consisting of 0 and 1, and a positive integer KK.

The ii-th person from the left is standing on feet if the ii-th character of SS is 0, and standing on hands if that character is 1.

You will give the following direction at most KK times (possibly zero):

Direction: Choose integers ll and rr satisfying 1≤l≤r≤N1≤l≤r≤N, and flip the ll-th, (l+1)(l+1)-th, ......, and rr-th persons. That is, for each i=l,l+1,...,ri=l,l+1,...,r, the ii-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.

Find the maximum possible number of consecutive people standing on hands after at most KK directions.

Constraints

  • NN is an integer satisfying 1≤N≤1051≤N≤105.
  • KK is an integer satisfying 1≤K≤1051≤K≤105.
  • The length of the string SS is NN.
  • Each character of the string SS is 0 or 1.

Input

Input is given from Standard Input in the following format:

NN KK
SS

Output

Print the maximum possible number of consecutive people standing on hands after at most KK directions.


Sample Input 1 Copy

Copy

5 1
00010

Sample Output 1 Copy

Copy

4

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

  • Give the direction with l=1,r=3l=1,r=3, which flips the first, second and third persons from the left.

Sample Input 2 Copy

Copy

14 2
11101010110011

Sample Output 2 Copy

Copy

8

Sample Input 3 Copy

Copy

1 1
1

Sample Output 3 Copy

Copy

1

No directions are necessary.

题意:

给你一个只含有0和1的字符串,并且给你一个数字K,你可以选择最多K次区间,每一个区间L<=R,然后对这个区间的元素进行取反操作。

即0变成1,1变成0,问你在最聪明的操作之后最大可以获得的连续1的串是多长?

可以看样例解释理解题意。

思路:

我们对字符串的连续1和0串进行计数处理,即把连续的1或者0的个数记录起来,

那么字符串会生成如下的数组

例如字符串是  110000111001101

我们把连续的1和连续0分别放入a和b数组,

那么a的元素是2 3 2 1

b的元素是4 2 1

我们思考可以发现,我们想要最长的连续1串,那么我们处理的区间肯定是连续的0区间,让他们变成1,然后来增长连续1串的长度。

即处理的0区间一定是连续的。

那么我们可以知道如下,例如我们处理两个区间,那么可以的得到的最长的连续1串就是这两个区间的0串长度的sum和以及这两个0串前后和中间的1串的sum和。

那么我们只需要枚举连续的K个的0串即可,

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ‘\0‘, sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=100010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int k;
char s[maxn];
std::vector<int> v1,v2;
ll sum1[maxn];
ll sum2[maxn];
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    gbtb;
    cin>>n>>k;
    cin>>s;
    int cnt=0;
//    int i=0;
    if(s[0]==‘0‘)
    {
        v1.pb(0);
    }
    repd(i,0,n-1)
    {
        if(s[i]==‘0‘)
        {
            while(s[i]==‘0‘)
            {
                cnt++;
                i++;
            }
            v2.push_back(cnt);
            cnt=0;
            i--;
        }
        if(s[i]==‘1‘)
        {
            while(s[i]==‘1‘)
            {
                cnt++;
                i++;
            }
            v1.push_back(cnt);
            cnt=0;
            i--;
        }

    }
    int num=max(sz(v1),sz(v2));
    repd(i,1,maxn-2)
    {
        v1.push_back(0);
        v2.push_back(0);
    }
    repd(i,1,maxn-2)
    {
        sum1[i]+=sum1[i-1]+v1[i-1];
        sum2[i]+=sum2[i-1]+v2[i-1];
    }
    int w=0;
    ll ans=0ll;
    repd(i,1,n-k+4)
    {
        int l=i;
        int r=l+k;
        ll temp=sum1[r]-sum1[l-1];
        temp+=sum2[r-1]-sum2[l-1];
        ans=max(ans,temp);
    }
    cout<<ans<<endl;

    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ‘ ‘ || ch == ‘\n‘);
    if (ch == ‘-‘) {
        *p = -(getchar() - ‘0‘);
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 - ch + ‘0‘;
        }
    }
    else {
        *p = ch - ‘0‘;
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 + ch - ‘0‘;
        }
    }
}

原文地址:https://www.cnblogs.com/qieqiemin/p/10704797.html

时间: 2024-08-30 13:59:28

AtCoder Beginner Contest 124 D - Handstand(思维+前缀和)的相关文章

Atcoder Beginner Contest 124 解题报告

心态爆炸.本来能全做出来的.但是由于双开了Comet oj一个比赛,写了ABC就去搞那个的B题 还被搞死了. 回来写了一会D就过了.可惜比赛已经结束了.真的是作死. A - Buttons #include <cstdio> using namespace std; int main() { int x, y; scanf("%d%d", &x, &y); int ans = x > y ? x : y; if (x > y) x--; else

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one

【ATcoder】AtCoder Beginner Contest 161 题解

题目链接:AtCoder Beginner Contest 161 原版题解链接:传送门 A - ABC Swap 这题太水,直接模拟即可. 1 #include <iostream> 2 using namespace std; 3 int main() { 4 int a, b, c; 5 cin >> a >> b >> c; 6 swap(a, b); 7 swap(a, c); 8 cout << a << " &

AtCoder Beginner Contest 103 D(贪心)

AtCoder Beginner Contest 103 D 题目大意:n个点,除第n个点外第i与第i+1个点有一条边,给定m个a[i],b[i],求最少去掉几条边能使所有a[i],b[i]不相连. 按右端点从小到大排序,如果当前选的去掉的边在区间内,那么符合条件,否则ans++,并贪心地把去掉的边指向右端点,因为前面的区间都满足条件了,所以要去掉的边要尽量向右移使其满足更多的区间. 1 #include <iostream> 2 #include <cstdio> 3 #incl

AtCoder Beginner Contest 136

AtCoder Beginner Contest 136 Contest Duration : 2019-08-04(Sun) 20:00 ~ 2019-08-04(Sun) 21:40 Website: AtCoder BC-136 后面几题都挺考思考角度D. C - Build Stairs 题目描述: 有n座山从左到右排列,给定每一座山的高度\(Hi\),现在你可以对每座山进行如下操作至多一次:将这座山的高度降低1. 问是否有可能通过对一些山进行如上操作,使得最后从左至右,山的高度呈不下降

AtCoder Beginner Contest 155 简要题解

AtCoder Beginner Contest 155 A:签到失败,WA一次. int main() { int a, b, c; cin >> a >> b >> c; if(a == b && b == c) cout << "No"; else if(a == b || a == c || b == c) cout << "Yes"; else cout << &quo

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质)

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质) We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and Vertex bibi. Consider painting each of these edges white or black. There ar

AtCoder Beginner Contest 100 C(思维)

*3 or /2题目大意:有n个数,每次操作将第i个数*3或/2,得到结果必须为整数,且每次操作必须有一次为/2,求最大操作次数.一开始看很懵比,感觉肯定是思维题,对着样例猜了个结论竟然就过了大数据...思路:奇数只能 * 3,所以只考虑偶数.对于一个偶数,可以分解成2^n * a,显然a为奇数,那么如果这个偶数乘3为2^n * 3a,3a也显然为奇数,所以证得:任意一个偶数无论乘多少次3它能/2的次数仍然为n不变.由于次数最大,所以每次只让一个偶数/2.所以答案就是统计每个偶数为2^n * a

AtCoder Beginner Contest 116 C题 【题意:可以在任意区间【L,R】上加1,求通过最少加1次数得到题目给定的区间】】{思维好题}

C - Grand Garden In a flower bed, there are NN flowers, numbered 1,2,......,N1,2,......,N. Initially, the heights of all flowers are 00. You are given a sequence h={h1,h2,h3,......}h={h1,h2,h3,......} as input. You would like to change the height of