题解:[NOIP 2017 提高组] 时间复杂度

洛谷P3952

Posted by TH911 on April 12, 2025

题目传送门

模拟

对于这种模拟题,其实题解也几乎没什么好说的——依据题意模拟即可。

注意事项

  • 某些循环体不会被执行,其内部的循环体也不会被执行,不应当计入时间复杂度。
  • 注意变量的上下界 $x,y$ 当 $x>y$ 时不会执行,$x=y$ 时为常数复杂度(包括 $x=y=n$)。

AC 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<stack>
#include<list>
#include<unordered_set>
using namespace std;
constexpr const int L=100;
int l;
string oType;//"1","value"
int oValue;
void getO(){
	string pl;
	cin>>pl;
	pl=pl.substr(2,pl.size()-3);
	if(pl=="1"){
		oType="1";
	}else{
		oType="value";
		oValue=atoi(pl.substr(2,pl.size()-2).c_str());
	}
}
struct solve{
	char type;
	string value,valueType;
	bool can;//能否被执行 
}a[L+1];
void getSolve(){
	for(int i=1;i<=l;i++){
		string pl;
		cin>>pl;
		a[i].type=pl[0];
		a[i].can=true;
		if(a[i].type=='F'){
			cin>>a[i].value;
			string x,y;
			cin>>x>>y;
			if(x=="n"&&y=="n"||x!="n"&&y!="n"){
				a[i].valueType="1";
				if(x!="n"){
					if(atoi(x.c_str())>atoi(y.c_str())){
						a[i].can=false;
					}
				} 
			}else{
				a[i].valueType="n";
				if(x=="n"&&y!="n"){
					a[i].can=false;
				}
			}
		}
	}
}
string query(){
	int Max=0;
	//变量id,n的层数 
	vector<pair<int,int>>s;
	unordered_set<string>flag;
	//cnt>0:不执行 
	int cnt=0;
	for(int i=1;i<=l;i++){
		if(a[i].type=='F'){
			//变量冲突 
			if(flag.count(a[i].value)){
				return "ERR";
			}
			cnt+=!a[i].can;
			int w=(a[i].valueType=="n"&&!cnt);
			if(s.size()){
				w+=s.back().second;
			}
			Max=max(Max,w);
			s.push_back({i,w});
			flag.insert(a[i].value);
		}else{
			if(s.size()){
				flag.erase(a[s.back().first].value);
				cnt-=!a[s.back().first].can;
				s.pop_back();
			}else{
				//F,E不匹配:缺 F 
				return "ERR";
			}
		}
	}
	//F,E不匹配:缺 E 
	if(s.size()){
		return "ERR";
	}
	bool mode;
	if(oType=="1"){
		mode=!Max;
	}else{
		mode=(Max==oValue);
	}
	return (mode?"Yes":"No");
}
int main(){
	/*freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);*/
	
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	int T;
	cin>>T;
	while(T--){
		cin>>l; 
		getO();
		getSolve();
		cout<<query()<<endl;
	}
	
	/*fclose(stdin);
	fclose(stdout);*/
	return 0;
}