2019.10.23- longest string of all 1's (the double pointer)

Subject description:

01 to a string, the definition of the answer = length of the longest string of consecutive 1, now you have at most
K chance, every opportunity you can string a 0 into a 1, now ask the greatest possible answers .

Input Description: The
first line of input two integers N, K, and the string length represents a number of opportunities for
second input line of N integers, represents an element of the string

Output Description: The
output is only one line, represents the answer

Sample input:
10 2
. 1. 1 0 0 0 0. 1. 1. 1 0
Sample Output:
5
Data range:
100% guarantee of data: 1 <= N <= 3 * 10 ^ 5, 0 <= K <= N

Double pointer sweep, the wind generally have, leaving only the border is still stubborn, only to beat the error sigh, change, patience, then you \ (A \) This question is a

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))

#define ON_DEBUGG

#ifdef ON_DEBUGG

#define D_e_Line printf("-----------\n")
#define D_e(x) std::cout << (#x) << " : " <<x << "\n"
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define Pause() system("pause")
#include <ctime>
#define TIME() fprintf(stderr, "\nTIME : %.3lfms\n", clock() * 1000.0 / CLOCKS_PER_SEC)

#else

#define D_e_Line ;
#define D_e(x) ;
#define FileOpen() ;
#define FilSave ;
#define Pause() ;
#define TIME() ;

#endif

struct ios {
    template<typename ATP> ios& operator >> (ATP &x) {
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x *= f;
        return *this;
    }
}io;

using namespace std;

template<typename ATP> inline ATP Min(ATP a, ATP b) {
    return a < b ? a : b;
}
template<typename ATP> inline ATP Max(ATP a, ATP b) {
    return a > b ? a : b;
}
template<typename ATP> inline ATP Abs(ATP a) {
    return a < 0 ? -a : a;
}

const int N = 3e5 + 7;

int a[N];
int main() {
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
    int n, K;
    io >> n >> K;
    R(i,1,n){
        io >> a[i];
    }
    int tot = 0, l = 1, r = 1, ans = 0;
    if(a[l] == 0) tot = 1;
    while(1){
//      printf("$%d %d %d %d\n", l, r, ans, tot);
        if(a[r + 1] == 1){
            ++r;
            if(r > n) break;
            ans = Max(ans, r - l + 1);
        }
        else{
            ++r;
            ++tot;
            while(tot > K){
                if(a[l] == 0) --tot;
                ++l;
            }
            if(r > n) break;
            ans = Max(ans, r - l + 1);
        }
    }
    
    printf("%d", ans);
    
    return 0;
}
/*
10 4
0 1 1 0 1 1 1 0 0 1 
*/
/*
10 4
0 0 0 0 0 1 0 0 1 1 
*/
/*
30 12
0 1 1 0 1 1 1 0 0 0 1 0 1 0 0 1 1 1 1 1 0 1 0 0 1 0 0 0 0 0 

26
*/
/*
30 8
1 1 0 0 1 1 0 1 0 1 1 0 1 1 0 0 0 0 0 0 1 1 1 0 1 0 1 1 0 0 

3 17

17
*/
/*
30 3
1 0 0 1 1 1 1 1 0 1 0 0 1 0 0 1 1 0 0 0 1 0 1 0 1 0 1 0 0 0 

10
*/

Guess you like

Origin www.cnblogs.com/bingoyes/p/11729549.html