ICPC青岛站网络赛-C-高效模拟

嗯这道辣鸡题,当时我队友写了错误的代码,我稍微改动了,思路基本上是对了,但是就是超时,我第一直觉是我这个算法思路是没有任何问题的,但是就是TLE,我感觉这个算法已经优化的不能再优化了啊。。。后面就怀疑我们自己的算法有问题,于是改算法,想很多莫名奇妙的,却无法实现的东西,最后导致我另外一个队友那边卡题无法进行,最后三题滚粗,这道题我从来没有想到过,会因为map超时,因为这是从来没有出现过的事情。。。后来确实是map超时,赛后补题二维map依旧超内存,后来看网上的代码,的确用vis[]标记出现次数,如果某个语执行次数超过256,我们知道这个语句一定有值的重复,因为一个语句最多对应256个数字,这样不需要用二维map,只需要vis就行

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
struct node
{
    char word[10];
    int v,k,r;
} a[10005];
int vis[10005];
int main()
{
    int t;
    int n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(vis,0,sizeof(vis));
        for (int i=1; i<=n; i++)
        {

            scanf("%s",a[i].word);
            if (a[i].word[0]=='a' && a[i].word[1]=='d')
            {
                scanf("%d",&a[i].r);
            }
            else
            {
                scanf("%d%d",&a[i].v,&a[i].k);
            }
        }
        int time=n*255;
        int f=0;
        int ip=1;
        int r;
        r=0;
        while(time>=0 && ip<=n)
        {
            if (vis[ip]>256)
            {
                break;
            }
            vis[ip]++;
            if (a[ip].word[0]=='a' && a[ip].word[1]=='d')
            {
                r=(r+a[ip].r)%256;
                ip++;
            }
            else if (a[ip].word[0]=='b' && a[ip].word[1]=='e')
            {
                if (a[ip].v==r)
                {
                    ip=a[ip].k;
                }
                else
                {
                    ip++;
                }
            }
            else if (a[ip].word[0]=='b' && a[ip].word[1]=='n')
            {
                if (a[ip].v!=r)
                {
                    ip=a[ip].k;
                }
                else
                {
                    ip++;
                }
            }
            else if (a[ip].word[0]=='b' && a[ip].word[1]=='l')
            {
                if (a[ip].v>r)
                {
                    ip=a[ip].k;
                }
                else
                {
                    ip++;
                }
            }
            else if (a[ip].word[0]=='b' && a[ip].word[1]=='g')
            {
                if (a[ip].v<r)
                {
                    ip=a[ip].k;
                }
                else
                {
                    ip++;
                }
            }
            time--;
        }
        if (ip>n)printf("Yes
");
        else printf("No
");
    }
    return 0;
}
有不懂欢迎咨询 QQ:1326487164(添加时记得备注)
原文地址:https://www.cnblogs.com/bluefly-hrbust/p/9664477.html