fzu 2041 Checker 枚举 模拟

来源:互联网 发布:玲珑网游加速器mac 编辑:程序博客网 时间:2024/04/25 14:51

Description

Checker game is an interesting game. But now Bob is tired of playing with others, he wants to play by himself. The following are the rules of his games:

1. The chessboard is a straight line which has n lattices.

2. There is only one kind of chess.

3. In the initial state, there are some chesses in the chessboard. There would be at most one chess in each lattice.

4. One chess can be moved directly to the adjacent empty lattice.

5. Your can move m steps at most. You can stop any time before you use up m steps.

6. The final score you get is the length of the longest continuous empty lattice.

Now Bob want to get as high score as possible. Can you tell him the possible highest score? The following is one example when m equals to 3. The highest score you can get in this case is 6.

Input

The first line of the input contains an integer T(T≤100), indicating the number of test cases. Each case begins with two integers n(1≤n≤500) and m(1≤m≤n*n), the number of lattices and the steps you can move at most, and is followed by a 0-1 string of length n. Here, 0 indicates an empty lattice and 1 indicates a full one.

Output

For each test case, print a line containing the test case number (beginning with 1) and the possible highest score Bob can get.

Sample Input

24 1101010 31000100010

Sample Output

Case 1: 2Case 2: 6

思路:

枚举每段空格,然后贪心扩大这段空格,看最大能扩大到多少,最后去最大值即可

#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <vector>using namespace std;typedef long long LL;const double eps = 1e-6;const int INF = 0x3f3f3f3f;const int MAXN = 500 + 10;int t, len, step;char G[MAXN], CG[MAXN];int Calc(int left, int right) {    strcpy(CG, G);    int cnt = step;    int low = left - 1, high = right + 1;    while (cnt--) {        if (low - 1 < 0 && high + 1 >= len) break;        for (;;) {            if (low - 1 < 0 && high + 1 >= len) break;            int lena = INF, lenb = INF;            bool yes = false;            if (low - 1 >= 0 && CG[low - 1] == '0') {                lena = left - (low - 1);                yes = true;            }             if (high + 1 < len && CG[high + 1] == '0') {                lenb = high + 1 - right;                yes = true;            }                        if (!yes) {                --low;                ++high;                continue;            }            if (lena < lenb) {                swap(CG[low], CG[low - 1]);                if (CG[left - 1] == '0') {                    --left;                }            } else {                swap(CG[high], CG[high + 1]);                if (CG[right + 1] == '0') {                    ++right;                }            }            low = left - 1;            high = right + 1;            break;        }    }    return right - left + 1;}int main() {#ifdef NIGHT_13    freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);#endif    scanf("%d", &t);    int cas = 0;    while (t--) {        scanf("%d%d%s", &len, &step, G);        int ans = 0;        for (int i = 0; i < len;) {            if (G[i] == '0') {                int j = i;                while (j + 1 < len && G[j + 1] == '0') { ++j; }                ans = max(ans, Calc(i, j));                i = j + 1;            } else {                ++i;            }        }        printf("Case %d: %d\n", ++cas, ans);    }    return 0;}




0 0