cogs 942. [東方S3] 比那名居天子
Posted ZlycerQan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cogs 942. [東方S3] 比那名居天子相关的知识,希望对你有一定的参考价值。
二次联通门 : 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; }
以上是关于cogs 942. [東方S3] 比那名居天子的主要内容,如果未能解决你的问题,请参考以下文章
cogs——1001. [WZOI2011 S3] 消息传递