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

时间:2023-12-18 19:46:32

嗯这道辣鸡题,当时我队友写了错误的代码,我稍微改动了,思路基本上是对了,但是就是超时,我第一直觉是我这个算法思路是没有任何问题的,但是就是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[];
int v,k,r;
} a[];
int vis[];
int main()
{
int t;
int n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(vis,,sizeof(vis));
for (int i=; i<=n; i++)
{ scanf("%s",a[i].word);
if (a[i].word[]=='a' && a[i].word[]=='d')
{
scanf("%d",&a[i].r);
}
else
{
scanf("%d%d",&a[i].v,&a[i].k);
}
}
int time=n*;
int f=;
int ip=;
int r;
r=;
while(time>= && ip<=n)
{
if (vis[ip]>)
{
break;
}
vis[ip]++;
if (a[ip].word[]=='a' && a[ip].word[]=='d')
{
r=(r+a[ip].r)%;
ip++;
}
else if (a[ip].word[]=='b' && a[ip].word[]=='e')
{
if (a[ip].v==r)
{
ip=a[ip].k;
}
else
{
ip++;
}
}
else if (a[ip].word[]=='b' && a[ip].word[]=='n')
{
if (a[ip].v!=r)
{
ip=a[ip].k;
}
else
{
ip++;
}
}
else if (a[ip].word[]=='b' && a[ip].word[]=='l')
{
if (a[ip].v>r)
{
ip=a[ip].k;
}
else
{
ip++;
}
}
else if (a[ip].word[]=='b' && a[ip].word[]=='g')
{
if (a[ip].v<r)
{
ip=a[ip].k;
}
else
{
ip++;
}
}
time--;
}
if (ip>n)printf("Yes\n");
else printf("No\n");
}
return ;
}