すべて1の2019.10.23-最長の文字列(ダブルポインタ)

件名の説明:

今はせいぜい持っている文字列、連続した1の最も長い文字列の答え=長さの定義、01〜
今の最大の可能な答えを尋ねる1にKのチャンス、機会あるごとことができます文字列0、 。

入力説明:
入力の最初の2行目の整数N、K、および文字列の長さは、機会の数を表し
、Nの整数の第二の入力ラインを文字列の要素を表します

出力説明:
出力は一行だけでは、答えを表し

サンプル入力:
10 2
。1 1 0 0 0 0 1 1 1 0
サンプル出力:
5
データ範囲:
データの100%保証:1 <= N <= 3 ×10 ^ 5、0 <= K <= N

ダブルポインタスイープは、風が一般的に残し、持っているだけの国境は、唯一のエラーため息、変更、忍耐を打つために、あなたはまだ頑固される\(\)この質問があります

#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