【浙江理工大学2016年新生赛暨全国新生邀请赛】 J 萌新吃果果,ZSTUOJ 4286【模拟】

来源:互联网 发布:linux chown r 编辑:程序博客网 时间:2024/04/29 23:14

4246: 萌新吃果果

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 840  Solved: 319

Description

开学第一天,萌新要排排坐吃果果啦,KI要求萌新们坐成一排。
现在萌新们必须要按KI的秩序表一个一个地就坐。
萌新中包括男孩子、女孩子和扶她。
男孩子会毫不犹豫地坐到当前已经坐好的人的后一个座位,女孩子在入座时会和前面的**男孩子**的隔一个座位坐,而扶她会观察前面连续坐的人数,若人数大于等于心理容忍度$k$,那么扶她会隔一个座位坐,否则直接坐到当前的后一个座位。
那么问题来了,KI想知道至少需要多少把椅子,才能让这些萌新正好坐成一排。

Input

第一行有一个整数$T$,表示测试的组数。
接下来每一个测试组第一行输入$n, k$,表示总人数,和扶她的心理容忍度。
第二行输入一个长度为$n$的字符串,表示KI的秩序表。字符串仅由$a, b, c$三种字符串组成,分别指代男孩子、女孩子和扶她。

数据范围:T <= 50, 1 <= n,k <=100000

Output

对于每个测试组输出一行,表示最少需要的椅子数量。

Sample Input

26 2aaabcc8 3abbccbaa

Sample Output

810

HINT

例如样例中的第一组数据,aaabcc, 且 k = 2,最后的座位坐法如下,需要8个座位

aaa_bc_c

Source


原题链接:http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4246

按照题意模拟会超时,用一个变量来记录当前连续坐了多少个人。


AC代码:

/*** 行有余力,则来刷题!  * 博客链接:http://blog.csdn.net/hurmishine  **/#include <cstdio>#include <iostream>#include <cstring>using namespace std;const int maxn=200000+5;char a[maxn];#include <ctime>#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)int main(){    int T;    //freopen ("I.in", "r", stdin);    //freopen ("ha.out", "w", stdout);    cin>>T;    int n,k;    while(T--)    {        cin>>n>>k;        char c;        int cnt=0;        int sum=0;        while(n--)        {            cin>>c;            if(c=='a')            {                a[cnt++]=c;                sum++;            }            else if(c=='b')            {                if(cnt-1>=0&&a[cnt-1]=='a')                {                    a[cnt++]='_';                    sum=0;                }                a[cnt++]='b';                sum++;            }            else            {                if(sum>=k)                {                    a[cnt++]='_';                    sum=0;                }                a[cnt++]='c';                sum++;            }        }        cout<<cnt<<endl;    }    //showtime;//1.96s    return 0;}

TLE代码:

/*** 行有余力,则来刷题!  * 博客链接:http://blog.csdn.net/hurmishine  **/#include <cstdio>#include <iostream>#include <cstring>#include <ctime>using namespace std;const int maxn=200000+5;char a[maxn];#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)int main(){    int T;    freopen ("I.in", "r", stdin);    freopen ("ha.out", "w", stdout);    cin>>T;    int n,k;    while(T--)    {        cin>>n>>k;        char c;        int cnt=0;        while(n--)        {            cin>>c;            if(c=='a')                a[cnt++]=c;            else if(c=='b')            {                if(cnt-1>=0&&a[cnt-1]=='a')                    a[cnt++]='_';                a[cnt++]='b';            }            else            {                int sum=0;                for(int i=cnt-1;i>=0;i--)                {                    if(a[i]!='_')                        sum++;                    else                        break;                }                if(sum>=k)                    a[cnt++]='_';                a[cnt++]='c';            }        }        cout<<cnt<<endl;    }    showtime;//8.875s    return 0;}/**************************************************************Problem: 4246User: hurmishineLanguage: C++Result: Time Limit Exceed****************************************************************/

标程代码:

#include <bits/stdc++.h>using namespace std;const int maxn = 1e5 + 10;char s[maxn];#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)//0.234int main(){    freopen ("I.in", "r", stdin);    freopen ("ha.out", "w", stdout);    int t;    scanf("%d", &t);    while(t --)    {        int n, k;        scanf("%d%d%s", &n, &k, s);        int cnt = 0, ans = 0, last = 0;        for(int i = 0; i < n; i ++)        {            if(s[i] == 'a')            {                last = 1;                ans ++;            }            if(s[i] == 'b')            {                ans ++;                if(last)                {                    ans ++;                    cnt = 0;                }                last = 0;            }            if(s[i] == 'c')            {                last = 0;                ans ++;                if(cnt >= k)                {                    cnt = 0;                    ans ++;                }            }            cnt ++;        }        printf("%d\n", ans);    }    showtime;    return 0;}
标程BF代码:

#include <bits/stdc++.h>using namespace std;const int maxn = 2e5+10;char s[maxn];char sb[maxn];#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)//8.25sint main(){    int t;    freopen ("I.in", "r", stdin);    freopen ("ha.out", "w", stdout);    scanf("%d", &t);    while(t --)    {        int n, k;        scanf("%d%d%s", &n, &k, s);        memset(sb, 0, sizeof(sb));        int cnt = 0;        for(int i = 0; i < n; i ++)        {            if(s[i] == 'a')            {                sb[cnt++] = 'a';            }            if(s[i] == 'b')            {                if(sb[cnt-1] == 'a')                {                    sb[cnt++] = '#';                }                sb[cnt++] = 'b';            }            if(s[i] == 'c')            {                int ct = 0;                for(int j = cnt-1; j >= 0; j --)                {                    if(sb[j] == '#') break;                    ct ++;                }                if(ct >= k) sb[cnt++] = '#';                sb[cnt ++] = 'c';            }        }        printf("%d\n", cnt);    }    showtime;}





0 0
原创粉丝点击