Submission #1039612

# Submission time Handle Problem Language Result Execution time Memory
1039612 2024-07-31T05:44:57 Z pcc Parking (CEOI22_parking) C++17
20 / 100
1029 ms 21960 KB
#include <bits/stdc++.h>
using namespace std;

#define pii pair<int,int>
#define fs first
#define sc second

const int mxn = 2e5+10;
int arr[mxn][2];
vector<int> pos[mxn];
set<int> spare;
int N,M;
priority_queue<pii,vector<pii>,greater<pii>> pq;
bitset<mxn> done;
vector<pii> ans;

void add(int col){
	if(!col)return;
	assert(pos[col].size() == 2);
	int p1 = pos[col][0],p2 = pos[col][1];
	if(p1&1)swap(p1,p2);
	if((p1&1)&&(p2&1))pq.push(pii(1,col));
	else if(!(p1&1)&&(p2&1)){
		if((arr[p1>>1][1]))pq.push(pii(3,col));
		else pq.push(pii(0,col));
	}
	else if(!(p1&1)&&!(p2&1)){
		if(arr[p1>>1][1]&&arr[p2>>1][1])pq.push(pii(4,col));
		else if(arr[p1>>1][1]||arr[p2>>1][1])pq.push(pii(2,col));
		else pq.push(pii(0,col));
	}
	else assert(false);
}

void mv(int a,int b){
	//cerr<<"MV: "<<a<<' '<<b<<endl;
	ans.push_back(pii(a,b));
	assert(spare.find(a) == spare.end());
	assert(arr[a][0]&&!arr[b][1]);
	int pa = (a<<1)|(arr[a][1]?1:0);
	int pb = (b<<1)|(!arr[b][0]?0:1);
	int col = arr[pa>>1][pa&1];
	assert(!(pb&1)||arr[pa>>1][pa&1] == arr[pb>>1][0]);
	swap(arr[pb>>1][pb&1],arr[pa>>1][pa&1]);
	pos[col].erase(find(pos[col].begin(),pos[col].end(),pa));
	pos[col].push_back(pb);
	assert(pos[col].size() == 2);
	if(!arr[pa>>1][0])spare.insert(pa>>1);
	add(arr[pa>>1][0]);
	add(col);
	add(arr[pb>>1][0]);
	return;
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>N>>M;
	for(int i = 1;i<=M;i++){
		for(int j = 0;j<2;j++){
			cin>>arr[i][j];
			if(arr[i][j])pos[arr[i][j]].push_back((i<<1)|j);
		}
		if(!arr[i][0])spare.insert(i);
	}
	for(int i = 1;i<=N;i++){
		assert(pos[i].size() == 2);
		if((pos[i][0]^pos[i][1]) == 1)done[i] = true;
		else{
			add(i);
		}
	}
	while(!pq.empty()){
		auto [tp,col] = pq.top();pq.pop();
		if(done[col])continue;
		assert(pos[col].size() == 2);
		int p1 = pos[col][0],p2 = pos[col][1];
		if(p1&1)swap(p1,p2);
		cerr<<tp<<' '<<col<<':'<<p1<<' '<<p2<<endl;
		if(tp == 4||tp == 1||tp == 3){
			if(spare.empty()){
				cout<<"-1\n";
				return 0;
			}
			auto emp = *spare.begin();
			spare.erase(emp);
			assert(!arr[emp][0]);
			mv(p1>>1,emp);
		}
		else if(tp == 2){
			if(arr[p1>>1][1])swap(p1,p2);
			assert(!(p1&1)&&!(p2&1)&&!(arr[p1>>1][1]));
			if(spare.empty()){
				cout<<"-1\n";
				return 0;
			}
			auto emp = *spare.begin();
			spare.erase(emp);
			assert(!arr[emp][0]);
			mv(p2>>1,emp);
		}
		else if(tp == 0){
			mv(p2>>1,p1>>1);
			done[col] = true;
		}
	}
	//cerr<<"ANS: "<<endl;
	for(int i = 1;i<=M;i++){
		//cerr<<arr[i][0]<<' '<<arr[i][1]<<endl;
	}
	for(int i = 1;i<=M;i++)assert(arr[i][0] == arr[i][1]);
	//cerr<<endl<<endl;
	cout<<ans.size()<<'\n';
	for(auto [a,b]:ans)cout<<a<<' '<<b<<'\n';
	return 0;
}

/*
3 4
0 0
1 2
3 1
3 2
*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 3 ms 4952 KB Output is correct
3 Correct 2 ms 4952 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 2 ms 4952 KB Output is correct
8 Correct 2 ms 4952 KB Output is correct
9 Correct 2 ms 5160 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 695 ms 20992 KB Output is correct
2 Correct 1029 ms 21960 KB Output is correct
3 Correct 501 ms 19740 KB Output is correct
4 Correct 503 ms 19252 KB Output is correct
5 Correct 908 ms 21960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 5208 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 5208 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 10 ms 5208 KB Output is correct
4 Correct 14 ms 5212 KB Output is correct
5 Correct 3 ms 5212 KB Output is correct
6 Correct 10 ms 5208 KB Output is correct
7 Correct 3 ms 5208 KB Output is correct
8 Incorrect 4 ms 5208 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 3 ms 4952 KB Output is correct
3 Correct 2 ms 4952 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 2 ms 4952 KB Output is correct
8 Correct 2 ms 4952 KB Output is correct
9 Correct 2 ms 5160 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 695 ms 20992 KB Output is correct
12 Correct 1029 ms 21960 KB Output is correct
13 Correct 501 ms 19740 KB Output is correct
14 Correct 503 ms 19252 KB Output is correct
15 Correct 908 ms 21960 KB Output is correct
16 Incorrect 3 ms 5208 KB Output isn't correct
17 Halted 0 ms 0 KB -