QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#376684 | #6545. Connect the Dots | zhouqixuan | WA | 1ms | 5840kb | C++14 | 1.9kb | 2024-04-04 15:04:20 | 2024-04-04 15:04:21 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
using namespace std;
typedef pair<int,int>PII;
const int N=2e5+5;
int n,m;
int a[N];
int cnt[N];
vector<PII>ans;
int L[N],R[N];
set<int>s;
void del(int u){
R[L[u]]=R[u],L[R[u]]=L[u];
cnt[a[u]]--;
}
void calc(){
ans.clear();
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++) cnt[i]=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
cnt[a[i]]++;
}
for(int i=1;i<n;i++)
if(a[i]!=a[i+1])
ans.push_back(make_pair(i,i+1));
for(int i=2;i<n;i++) L[i]=i-1,R[i]=i+1;
s.clear();
R[1]=2,L[n]=n-1;
for(int i=n-2;i;i--) if(a[L[i]]!=a[R[i]]) s.insert(i);
for(int t=n-2;t;t--){
if(s.size()){
int u=*s.begin();
s.erase(s.begin());
if(cnt[a[u]] == 1){
for(int i=L[u];i>1;i=L[i]) ans.push_back(make_pair(L[i],u)),del(i);
for(int i=R[u];i<n;i=R[i]) ans.push_back(make_pair(u,R[i])),del(i);
if(a[1]!=a[n]) ans.push_back(make_pair(1,n));
break;
}
else{
ans.push_back(make_pair(L[u],R[u]));
int x=L[u],y=R[u];
del(u);
if(x>1 && a[L[x]]!=a[R[x]])s.insert(x);
else{
if(s.find(x)!=s.end()) s.erase(x);
}
if(y<n && a[L[y]]!=a[R[y]]) s.insert(y);
else{
if(s.find(y)!=s.end()) s.erase(y);
}
}
}
else{
int u=L[1],v=R[u];
del(u);
if(v<n && a[1]!=a[R[v]]) s.insert(v);
}
}
cout<<(int)ans.size()<<endl;
for(int i=0;i<ans.size();i++) printf("%d %d\n",ans[i].first,ans[i].second);
return;
}
int main(){
int T;scanf("%d",&T);
while(T--) calc();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5840kb
input:
3 4 2 1 1 2 2 4 2 1 2 1 2 3 3 1 2 3
output:
4 2 3 0 2 2 4 1 4 5 1 2 2 3 3 4 0 2 0 3 4 1 2 2 3 1 3 1 3
result:
wrong answer output = 4, answer = 3.