二次联通门 : cogs 942. [東方S3] 比那名居天子
/* cogs 942. [東方S3] 比那名居天子 二分水题 二分所求区间长度 判断后缩小范围即可 */ #include <cstring> #include <cstdio> #define Max 1000000 void read (int &now) { now = 0; register char word = getchar (); while (word < '0' || word > '9') word = getchar (); while (word >= '0' && word <= '9') { now = now * 10 + word - '0'; word = getchar (); } } char line[Max]; int N, K; bool Judge (int key) { int Count = 0; for (register int i = 0; i < N; i ++) if (line[i] == '1') { i += key - 1; Count ++; if (Count > K) return false; } return true; } int main (int argc, char *argv[]) { freopen ("tenshi.in", "r", stdin); freopen ("tenshi.out", "w", stdout); read (N); read (K); scanf ("%s", line); int pos = 0; for (pos = 0; line[pos] == '0'; pos ++); if (pos == N) { putchar ('0'); return 0; } register int Answer; for (register int l = 1, r = (N << 1), Mid; l <= r; ) { Mid = l + r >> 1; if (Judge (Mid)) { r = Mid - 1; Answer = Mid; } else l = Mid + 1; } printf ("%d", Answer); return 0; }