QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#376435 | #6545. Connect the Dots | Tx_Lcy | WA | 1ms | 5960kb | C++14 | 1.6kb | 2024-04-04 09:56:41 | 2024-04-04 09:56:42 |
Judging History
answer
//A tree without skin will surely die.
//A man without face will be alive.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mid ((l+r)>>1)
#define lowbit(x) (x&-x)
#define all(x) (x).begin(),(x).end()
#define rep(i,j,k) for (int i=j;i<=k;++i)
#define per(i,j,k) for (int i=j;i>=k;--i)
int const N=2e5+10;
int n,m,a[N],cnt[N];
set<int>all,dif;
vector< pair<int,int> >ans;
inline void push(int id){
if (id==1 || id==n) return;
auto it=all.find(id);
auto pr=it,nt=it;
--pr,++nt;
if (a[*pr]!=a[*nt]) dif.insert(id);
}
inline void del(int k){
auto it=all.find(k);
auto pr=it,nt=it;
--pr,++nt;
int P=*pr,N=*nt;
if (a[P]!=a[N]) ans.push_back({P,N});
if (dif.count(*pr)) dif.erase(P);
if (dif.count(*nt)) dif.erase(N);
dif.erase(k),all.erase(k),--cnt[a[k]];
push(P),push(N);
}
inline void solve(){
cin>>n>>m,all.clear(),dif.clear(),ans.clear();
rep(i,1,m) cnt[i]=0;
rep(i,1,n) cin>>a[i],++cnt[a[i]];
rep(i,1,n) all.insert(i);
rep(i,2,n-1)
if (a[i-1]!=a[i+1]) dif.insert(i);
rep(i,1,n-1)
if (a[i]!=a[i+1]) ans.push_back({i,i+1});
rep(i,1,n-2){
if (!dif.size()){
del(*(++all.begin()));
continue;
}
int g=*dif.begin();
if (cnt[g]==1){
vector<int>q;
for (auto i:all)
if (i!=1 && i!=n && i!=g) q.push_back(i);
for (auto i:q) del(i);
del(g);
break;
}
del(g);
}
cout<<ans.size()<<'\n';
for (auto i:ans) cout<<i.first<<' '<<i.second<<'\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t=1;
cin>>t;
while (t--) solve();
cerr<<"Time: "<<(double)clock()/CLOCKS_PER_SEC<<" s\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3920kb
input:
3 4 2 1 1 2 2 4 2 1 2 1 2 3 3 1 2 3
output:
3 2 3 1 3 1 4 4 1 2 2 3 3 4 1 4 3 1 2 2 3 1 3
result:
ok all 3 test passed
Test #2:
score: 0
Accepted
time: 0ms
memory: 3952kb
input:
1 2 2 1 2
output:
1 1 2
result:
ok all 1 test passed
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 5960kb
input:
10 5 2 2 2 2 1 2 5 2 2 1 2 1 2 5 2 1 2 2 2 1 5 2 2 1 2 1 1 5 2 1 1 1 2 1 5 2 1 2 2 1 2 5 2 2 1 1 2 2 5 2 2 2 2 1 1 5 2 1 1 2 1 2 5 2 1 2 2 2 1
output:
4 3 4 4 5 2 4 1 4 5 1 2 2 3 3 4 4 5 1 4 4 1 2 4 5 1 3 1 4 5 1 2 2 3 3 4 3 5 1 5 3 3 4 4 5 2 4 5 1 2 3 4 4 5 1 3 1 5 4 1 2 3 4 1 3 3 5 4 3 4 2 4 1 4 1 5 5 2 3 3 4 4 5 1 3 1 5 4 1 2 4 5 1 3 1 4
result:
wrong answer output = 3, answer = 4.