FZU 2041 Checker (贪心+模拟)

题目地址: FZU 2041

这个题是昨天的队内选拔赛用的套题里的其中一道题,我当时想到方法了,但是没敢写。。一个是对复杂度有些不确定,万一组数很多的话好像就会跪。。而且感觉不太好实现,队里还卡着两道题,就打算等别的该出的题出了之后再写,结果没时间了。。

刚才按照那思路写了一下。。结果就过了。。。真心醉了。。我&……%¥%**……%%

思路是先枚举每个空隙,然后对该空隙向左向右贪心的一步步的去移动,剩下的就是小模拟了。然后找出所有空隙可能扩大的最大值就可以了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <cmath>
#define LL __int64
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define eqs 1e-15
const int mod = 1e9+7 ;
using namespace std ;
char s[600];
int a[600], cnt;
struct node {
        int l, r, x;
} fei[600];
int main()
{
        int t, n, m, i, j, flag, lstep, rstep, max1, step, Case=0, mm;
        scanf("%d",&t);
        while(t--) {
                scanf("%d%d",&n,&mm);
                scanf("%s",s+1);
                s[0]=‘1‘;
                s[n+1]=‘1‘;
                cnt=0;
                flag=0;
                max1=0;
                for(i=0; i<=n+1; i++) {
                        if(s[i]==‘1‘) {
                                if(flag) {
                                        flag=0;
                                        fei[cnt].r=i;
                                        cnt++;
                                }
                                fei[cnt].l=i;
                                fei[cnt].x=0;
                        } else {
                                flag=1;
                                fei[cnt].x++;
                                max1=max(max1,fei[cnt].x);
                        }
                }
                for(i=0; i<cnt; i++) {
                        m=mm;
                        for(j=0; j<=n+1; j++) {
                                a[j]=s[j]-‘0‘;
                        }
                        while(m) {
                                lstep=INF;
                                for(j=fei[i].l; j>=0; j--) {
                                        if(!a[j]) {
                                                lstep=fei[i].l-j;
                                                break;
                                        }
                                }
                                rstep=INF;
                                for(j=fei[i].r; j<=n+1; j++) {
                                        if(!a[j]) {
                                                rstep=j-fei[i].r;
                                                break;
                                        }
                                }
                                step=min(lstep,rstep);
                                if(m<step) {
                                        max1=max(fei[i].x,max1);
                                        break;
                                }
                                m-=step;
                                fei[i].x++;
                                if(lstep<=rstep) {
                                        a[fei[i].l]=0;
                                        a[fei[i].l-step]=1;
                                        fei[i].l--;
                                } else {
                                        a[fei[i].r]=0;
                                        a[fei[i].r+step]=1;
                                        fei[i].r++;
                                }
                                if(!m) {
                                        max1=max(max1,fei[i].x);
                                        break;
                                }
                        }
                }
                printf("Case %d: %d\n",++Case,max1);
        }
        return 0;
}
时间: 2024-08-07 23:08:51

FZU 2041 Checker (贪心+模拟)的相关文章

[贪心+模拟] zoj 3829 Known Notation

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science.

POJ 1979 POJ 3009 AOJ 0033 AOJ 0118 [搜索类题目][0033贪心模拟]

/** POJ 1979 BFS */ #include <stdio.h> #include <string.h> #include <iostream> #include <queue> using namespace std; const int N = 20 + 5; int mp[N][N]; int sx,sy; int n, m; int vis[3000]; int dirx[] = {0, 1, 0, -1}; int diry[] = {

hdu 4915 Parenthese sequence (贪心+模拟)

题目大意: 一个序列中有左括号和右括号,还有问号,问号可以任意转换成左右括号. 问这个序列有多少种情况的转变使得这个序列变成合法的括号匹配序列. 思路分析: 首先我们分析一下,如何使得一个序列是合法的括号匹配序列. 我们很容易想到的是用栈模拟匹配过程. 当遇到左括号就进栈,当遇到右括号就让栈顶的左括号出栈. 那么在模拟的过程中,造成这个序列的不合法的原因只有当右括号来的时候,此时的栈已经为空. 这里补充一句,一旦一个序列给定,那么这里面的问号有多少变成左括号,多少变成右括号,是一定的. 看完以上

1002 Game (贪心模拟)

1002 Game (贪心模拟)Accepts: 572 Submissions: 6218 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description 度度熊在玩一个好玩的游戏. 游戏的主人公站在一根数轴上,他可以在数轴上任意移动,对于每次移动,他可以选择往左或往右走一格或两格. 现在他要依次完成 n 个任务,对于任务 i,只要他处于区间 [ai,bi]

UVA 11776 - Oh Your Royal Greediness! - [贪心/模拟]

题目链接:https://cn.vjudge.net/problem/UVA-11776 题意: 给出数字n(0<=n<=1000),代表有n个农民,接下来有n行,每行两个数字S和E代表这个农民工作时间为[S,E]: 每个农民工作时,需要有一个enforcer来监督,且每个enforcer一次只能监督一个农民: 求最少需要多少个enforcer. 题解: 这道题我也不太清楚算贪心还是算模拟,可能两者都有一点吧. 首先我们根据正常思维,既然要模拟分配监工去监督农民,那么肯定先给第一个开始做的农民

【BZOJ2457】[BeiJing2011]双端队列 贪心+模拟

[BZOJ2457][BeiJing2011]双端队列 Description Sherry现在碰到了一个棘手的问题,有N个整数需要排序. Sherry手头能用的工具就是若干个双端队列. 她需要依次处理这N个数,对于每个数,Sherry能做以下两件事: 1.新建一个双端队列,并将当前数作为这个队列中的唯一的数: 2.将当前数放入已有的队列的头之前或者尾之后. 对所有的数处理完成之后,Sherry将这些队列排序后就可以得到一个非降的序列. Input 第一行包含一个整数N,表示整数的个数.接下来的

FZU 2152 文件系统 (小模拟)

       Problem 2152 文件系统 Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description 每个Linux文件具有四种访问权限:可读(r).可写(w).可执行(x)和无权限(-). 利用ls -l命令可以看到某个文件或目录的权限,它以显示数据的第一个字段为准.第一个字段由10个字符组成,如下: -rwxr-xr-x 第1位表示文件类型,-表示文件,d表示目录 2-4位表示文件所有者的权限,u权限 5-

FZU 2102 Solve equation(水,进制转化)&amp;&amp; FZU 2111(贪心,交换使数字最小)

C Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice FZU 2102 Description You are given two positive integers A and B in Base C. For the equation: A=k*B+d We know there always existing many non-negativ

To Fill or Not to Fill(贪心模拟)

题目描述: With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are