모두 1 (이중 포인터)의 2019.10.23- 긴 문자열

주제 설명 :

문자열, 대답의 정의 = 연속 1의 가장 긴 문자열의 길이에 01, 지금은 기껏해야
, K 확률 0 1로 당신이 문자열 할 수있는 모든 기회를 지금 최대한의 답변을 요청 .

입력 설명 :
입력 첫번째 라인 두 정수 N, K, 그리고 스트링 길이를위한 기회의 수 나타내고
, N 정수의 제 2 입력 라인 문자열의 원소를 나타내고

출력 설명 :
출력, 하나 개의 라인은 답을 나타냅니다

시료 입력 :
10 2
. 1 1 0 0 0 0 1 1 1 0
샘플 출력 :
5
데이터 범위 :
데이터 100 % 보증 1 <= N <= 3 * 10 ^ 5, 0 <= K <= N

더블 포인터 스윕, 바람은 일반적으로 남겨두고 있습니다 만 테두리 만 오류 한숨, 변경, 인내심을 이길, 당신 여전히 고집한다 \ (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
*/

추천

출처www.cnblogs.com/bingoyes/p/11729549.html