# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1039521 |
2024-07-31T03:00:12 Z |
pcc |
Parking (CEOI22_parking) |
C++17 |
|
1881 ms |
23744 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;
int p1 = pos[col][0],p2 = pos[col][1];
if(p1&1)swap(p1,p2);
if((p1&1)&&(p2&1))pq.push(pii(3,col));
else if(!(p1&1)&&(p2&1)){
if((arr[p1>>1][1]))pq.push(pii(2,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(1,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];
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);
}
}
if(N == M){
if(done.count() != N)cout<<"-1\n";
else cout<<"0\n";
return 0;
}
while(!pq.empty()){
auto [tp,col] = pq.top();pq.pop();
if(done[col])continue;
int p1 = pos[col][0],p2 = pos[col][1];
if(p1&1)swap(p1,p2);
cerr<<tp<<' '<<col<<':'<<p1<<' '<<p2<<endl;
if(tp == 3||tp == 2){
if(spare.empty()){
cout<<"-1\n";
return 0;
}
auto emp = *spare.begin();
spare.erase(emp);
mv(p1>>1,emp);
}
else if(tp == 1){
if(arr[p1>>1][1])swap(p1,p2);
if(spare.empty()){
cout<<"-1\n";
return 0;
}
auto emp = *spare.begin();
spare.erase(emp);
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;
cerr<<endl<<endl;
cout<<ans.size()<<'\n';
for(auto [a,b]:ans)cout<<a<<' '<<b<<'\n';
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:70:19: warning: comparison of integer expressions of different signedness: 'std::size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
70 | if(done.count() != N)cout<<"-1\n";
| ~~~~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4952 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 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 |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1881 ms |
23744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
5208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4952 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 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 |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Incorrect |
1881 ms |
23744 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |