QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#142947#5665. AA Country and King Dreamoonammardab3anTL 0ms3476kbC++203.7kb2023-08-20 06:46:442023-08-20 06:46:46

Judging History

你现在查看的是最新测评结果

  • [2023-08-20 06:46:46]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3476kb
  • [2023-08-20 06:46:44]
  • 提交

answer


// By AmmarDab3an 

#include "bits/stdc++.h"

using namespace std;

#define int int64_t
#define ll  int64_t

// typedef unsigned int        uint;
// typedef long long int       ll;
// typedef unsigned long long  ull;
typedef pair<int, int>    pii;
typedef pair<ll, ll>      pll;
typedef pair<int, pii>    iii;
typedef pair<ll, pll>     lll;
typedef vector<int>       vi;
typedef vector<ll>        vl;
typedef vector<pii>       vpii;
typedef vector<pll>       vpll;

#define endl '\n'
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define freopenI freopen("input.txt", "r", stdin);
#define freopenO freopen("output.txt", "w", stdout);

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double  PI = acos(-1);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
int rand(int x, int y) {
	return uniform_int_distribution<int>(x, y)(rng);
}

int mul(int a, int b){
	int ret = (1ll * (a%MOD) * (b%MOD)) % MOD;
	return (ret+MOD)%MOD;
}
 
int add(int a, int b){
	int ret = (1ll * (a%MOD) + (b%MOD)) % MOD;
	return (ret+MOD)%MOD;
}
 
int pow_exp(int n, int p){
	if(!p) return 1;
	if(p&1) return mul(n, pow_exp(n, p-1));
	int tmp = pow_exp(n, p/2);
	return mul(tmp, tmp);
}

int inv(int x){
	return pow_exp(x, MOD-2);
}
 
const int  MAX = 2e5 + 10;
const int NMAX = 2e5 + 10;
const int MMAX = 2e5 + 10;
const int LOG_MAX = ceil(log2(double(NMAX)));
const int BLOCK = ceil(sqrt(double(NMAX)));

int32_t main(){
    
    fastIO;
	
    int t; cin >> t; while(t--){

		int n;
		cin >> n;
		
		int m = 2*n-1;
		m = max(m, int(0));
		
		vi vec(m);
		for(auto &i : vec) cin >> i, --i;
		
		if(n==0){
			n = 3e5;
			m = 2*n-1;
			vec = vi(m, -1);
		}
		
		vi par(n, -1);
		vector<set<int>> adj(n);
		set<int> st_not_exists, st_suf;
		vector<bool> vis(n);
		
		vec[0] = 0;
		vec[m-1] = 0;
		
		for(int i = 1; i < m && (vec[i]!=-1); i++){
			int p = vec[i-1];
			if(par[vec[i]]==-1) par[vec[i]] = p;
		}
		
		for(int i = m-2; i >= 0 && (vec[i]!=-1); i--){
			
			int p = vec[i+1];
			if(par[vec[i]]==-1) adj[p].insert(vec[i]);
			st_suf.insert(vec[i]);
			
			if(par[vec[i]] != -1 && par[vec[i]]==vec[i-1]){
				adj[vec[i-1]].erase(vec[i]);
				vis[vec[i]] = true;
			}
			
			if(par[vec[i]]==-1) par[vec[i]] = p;
		}
		
		for(int i = 0; i < m && (vec[i] != -1); i++) if(vec[i] != -1){
			vis[vec[i]] = true;
		}
		for(int i = 0; i < n; i++) if(!vis[i]){
			st_not_exists.insert(i);
		}
		
		par[0] = INF;
		st_suf.insert(0);
		
		int l = 0;
		while(l < m && vec[l] != -1) l++;
		
		// for(auto e : par) cout << e << ' '; cout << endl;
		
		
		for(int i = l; i < m && (vec[i]==-1); i++){
			
			int p = vec[i-1];
			
			if(adj[p].empty()){
				
				int st_path = !st_suf.count(p) ? p : INF;
				int nd_path = !st_not_exists.empty() ? *st_not_exists.begin() : INF;
				
				// cout << st_path << ' ' << nd_path << endl << flush;
				
				if(st_path < nd_path){
					vec[i] = par[p];
				}
				else{
					vec[i] = nd_path;
					st_not_exists.erase(st_not_exists.begin());
					par[vec[i]] = p;
				}
			}
			else{
				
				int st_path = *adj[p].begin();
				int nd_path = !st_not_exists.empty() ? *st_not_exists.begin() : INF;
				
				if(st_path < nd_path){
					vec[i] = st_path;
					adj[p].erase(adj[p].begin());
				}
				else{
					vec[i] = nd_path;
					st_not_exists.erase(st_not_exists.begin());
					par[vec[i]] = p;
				}
			}
			
			// cout << i << endl;
			// for(auto e : vec) cout << e << ' '; cout << endl;
			// cout << flush;
		}
		
		for(auto e : vec) cout << e+1 << ' '; cout << endl;
    }	
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3476kb

input:

9
5
1 2 3 2 0 2 1 5 1
5
1 2 3 0 0 2 1 5 1
5
1 2 0 0 0 2 1 5 1
5
1 2 0 0 0 0 1 5 1
5
1 0 0 0 0 0 1 5 1
5
1 0 0 0 0 0 0 5 1
5
1 0 0 0 0 0 0 0 1
5
1 0 0 0 0 0 0 0 0
5
0 0 0 0 0 0 0 0 0

output:

1 2 3 2 4 2 1 5 1 
1 2 3 2 4 2 1 5 1 
1 2 3 2 4 2 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 

result:

ok 9 lines

Test #2:

score: -100
Time Limit Exceeded

input:

28668
2
0 2 1
2
0 0 1
2
0 0 0
2
1 0 1
2
1 0 0
2
1 2 0
3
0 2 1 3 1
3
0 0 1 3 1
3
0 0 0 3 1
3
0 0 0 0 1
3
0 0 0 0 0
3
1 0 1 3 1
3
1 0 0 3 1
3
1 0 0 0 1
3
1 0 0 0 0
3
1 2 0 3 1
3
1 2 0 0 1
3
1 2 0 0 0
3
1 2 1 0 1
3
1 2 1 0 0
3
1 2 1 3 0
3
0 2 3 2 1
3
0 0 3 2 1
3
0 0 0 2 1
3
1 0 3 2 1
3
1 0 0 2 1
3
1 2 ...

output:


result: