QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#384025 | #6545. Connect the Dots | waretle | WA | 1ms | 5880kb | C++14 | 1.3kb | 2024-04-09 20:06:32 | 2024-04-09 20:06:32 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e5+5;
int n,m,a[N],st[N],tp,cnt[N];
bool del[N];
vector<pair<int,int>>ans;
void work(int x)
{
int l=x,r=x;
for(int i=x-1;i;--i)if(!del[i])
{
if(l!=x)ans.emplace_back(i,x);
l=i;
}
for(int i=x+1;i<=n;++i)if(!del[i])
{
if(r!=x)ans.emplace_back(x,r);
r=i;
}
if(a[l]!=a[r]&&l!=x&&r!=x)ans.emplace_back(l,r);
}
void solve()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)scanf("%d",a+i);
memset(del,0,n+1),ans.clear(),memset(cnt,0,m+1<<2),tp=0;
for(int i=1;i<=n;++i)++cnt[a[i]];
for(int i=1;i<n;++i)if(a[i]!=a[i+1])ans.emplace_back(i,i+1);
for(int i=1;i<=n;++i)
{
while(tp>1&&a[st[tp-1]]!=a[i])
{
if(cnt[a[i]]==1)
{
work(i);i=n+1;tp=0;break;
}
ans.emplace_back(i,st[tp-1]),del[st[tp]]=1,--cnt[st[tp]],--tp;
}
st[++tp]=i;
}
for(int i=4;i<=tp;i+=2)ans.emplace_back(st[1],st[i]);
printf("%d\n",(int)ans.size());
for(auto i:ans)printf("%d %d\n",i.first,i.second);
}
int main()
{
int _;scanf("%d",&_);
while(_--)solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3836kb
input:
3 4 2 1 1 2 2 4 2 1 2 1 2 3 3 1 2 3
output:
3 2 3 3 1 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: 3968kb
input:
1 2 2 1 2
output:
1 1 2
result:
ok all 1 test passed
Test #3:
score: 0
Accepted
time: 1ms
memory: 5880kb
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 3 1 4 1 5 1 2 2 3 3 4 5 3 1 5 4 3 4 4 5 2 4 1 4 5 1 2 3 4 4 5 3 1 1 5 4 1 2 3 4 3 1 5 3 4 3 4 4 2 4 1 5 1 5 2 3 3 4 4 5 3 1 1 5 4 1 2 4 5 3 1 4 1
result:
ok all 10 test passed
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 3840kb
input:
10 7 2 1 2 1 1 1 2 1 7 2 1 1 2 1 2 1 2 7 2 2 2 1 1 2 1 1 7 2 1 1 1 2 2 1 1 7 2 1 2 2 1 2 2 1 7 2 2 1 2 2 2 2 1 7 2 1 2 1 2 2 2 2 7 2 2 2 1 2 1 2 1 7 2 2 1 1 2 1 2 2 7 2 2 2 1 2 1 1 2
output:
7 1 2 2 3 5 6 6 7 4 2 5 2 1 6 8 2 3 3 4 4 5 5 6 6 7 3 1 1 5 1 7 7 2 3 4 5 5 6 3 1 4 1 7 5 1 7 6 3 4 5 6 4 2 4 1 1 5 5 6 7 1 2 3 4 4 5 6 7 3 1 6 4 1 6 7 1 2 2 3 6 7 4 2 5 2 6 2 1 7 7 1 2 2 3 3 4 5 3 6 3 7 3 1 7 8 2 3 3 4 4 5 5 6 6 7 3 1 1 5 1 7 7 1 2 3 4 4 5 5 6 3 1 7 5 1 5 7 2 3 3 4 4 5 6 7 3 1 6 4 ...
result:
wrong answer TC #4: two identical wire.