QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#211065 | #6545. Connect the Dots | ucup-team1004 | WA | 1ms | 3900kb | C++14 | 1.9kb | 2023-10-12 07:29:14 | 2023-10-12 07:29:15 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
if(x.empty())return out<<"[]";
out<<'['<<x[0];
for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
cerr<<x<<' ',debug(y...);
}
const int N=2e5+10;
int T,n,m,a[N],cnt[N],pre[N],nex[N];
bool chk(int x){
return pre[x]&&nex[x]<=n&&a[pre[x]]!=a[nex[x]];
}
void del(int x){
nex[pre[x]]=nex[x],pre[nex[x]]=pre[x];
}
void get(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)cnt[a[i]]++;
for(int i=0;i<=n;i++)nex[i]=i+1,pre[i+1]=i;
set<int>s;
for(int i=1;i<=n;i++)if(chk(i))s.insert(i);
vector<pair<int,int> >ans;
ans.clear();
for(int i=1;i<n;i++){
if(a[i]^a[i+1])ans.push_back({i,i+1});
}
for(int t=n-2;t--;){
if(!s.empty()){
int i=*s.begin();
if(cnt[a[i]]==1){
for(;pre[i]!=1;del(pre[i])){
ans.push_back({pre[pre[i]],i});
}
for(;nex[i]!=n;del(nex[i])){
ans.push_back({i,nex[nex[i]]});
}
if(a[pre[i]]^a[nex[i]]){
ans.push_back({pre[i],nex[i]});
}
break;
}else{
if(chk(pre[i]))s.erase(pre[i]);
if(chk(nex[i]))s.erase(nex[i]);
ans.push_back({pre[i],nex[i]});
del(i);
if(chk(pre[i]))s.insert(pre[i]);
if(chk(nex[i]))s.insert(nex[i]);
}
}else{
int i=nex[1];
del(i);
if(chk(pre[i]))s.insert(pre[i]);
if(chk(nex[i]))s.insert(nex[i]);
}
}
printf("%lu\n",ans.size());
for(auto x:ans)printf("%d %d\n",x.first,x.second);
}
void clr(){
for(int i=1;i<=m;i++)cnt[i]=0;
}
int main(){
for(scanf("%d",&T);T--;clr())get();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3900kb
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 3 4 1 2 2 3 3 4 1 4 3 1 2 2 3 1 3
result:
wrong answer TC #1: two identical wire.