【題解】LuoGu3952:時間複雜度

原題傳送門
寫的我一口老血

你得先想好
什麼時候會ERR\text{ERR}

  • 變量名衝突(那麼需要的是一個變量名的桶usedused
  • E多了或F多了(需要一個記錄目前存在幾重循環的變量tottot

然後你得知道當前的循環到底有沒有用
具體來說是這個循環的外面是否有類似
F i 100 1 或 F i n 100\text{F i 100 1 或 F i n 100}的情況
那麼需要一個記錄外面依然有幾個這樣的循環的變量cannotcannot

接着你要明確當前是否嵌套nn以及已經嵌套了幾層nn
那麼我又要處理x,yx,y的大小關係,以及一個記錄嵌套層數的變量cyclenumcyclenum,記錄答案maxnummaxnum

最後你EE的時候根據當前是否在無用循環裏面(cannotcannot還不爲0)或者當前結束的循環到底有沒有嵌套出一個n來決定是否--cyclenum;--cannot

梳理了一下感覺這個模擬題目就十分清晰有沒有
但是還是千萬記住一點,別再中途breakbreak,否則你讀不完

Code:

#include <bits/stdc++.h>
#define maxn 10010
using namespace std;
char s[maxn];
struct node{
	char x;
	int time;
}a[maxn];
int used[maxn], n;

int main(){
	int T;
	scanf("%d", &T);
	while (T--){
		int goal = 0, cyclenum = 0, iferror = 0, cannot = 0, maxtime = 0, tot = 0;
		memset(used, 0, sizeof(used));
		scanf("%d", &n), scanf("%s", s + 1);
		if (s[3] == 'n')
			for (int i = 5; i <= strlen(s + 1) - 1; ++i) goal = (goal << 1) + (goal << 3) + (s[i] ^ 48);
		while (n--){
			scanf("%s", s + 1);
			if (s[1] == 'E'){
				if (!tot) iferror = 1;
				used[a[tot].x] = 0;
				if (!a[tot].time) --cannot; else
				if (!cannot && a[tot].time == 2) --cyclenum;
				--tot;
			} else{
				scanf("%s", s + 1);
				char x = s[1];
				if (used[x]) iferror = 1;
				used[x] = 1;
				
				scanf("%s", s + 1);
				int pos = 1, l = 0, r = 0, time;
				if (s[pos] == 'n') l = 1e4; else while (pos <= strlen(s + 1)) l = (l << 1) + (l << 3) + (s[pos++] ^ 48);
				scanf("%s", s + 1);
				pos = 1;
				if (s[pos] == 'n') r = 1e4; else while (pos <= strlen(s + 1)) r = (r << 1) + (r << 3) + (s[pos++] ^ 48);
				if (r < l) time = 0; else
				if (r - l > 1e3) time = 2; else time = 1;
				
				if (!time) ++cannot; else
				if (time == 2 && !cannot) maxtime = max(maxtime, ++cyclenum);
				a[++tot] = (node){x, time};
			}
		}
		if (tot || iferror) puts("ERR"); else
		if (maxtime == goal) puts("Yes"); else puts("No");
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章