QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#380678 | #6545. Connect the Dots | zhulexuan | WA | 2ms | 14124kb | C++14 | 3.3kb | 2024-04-07 08:34:25 | 2024-04-07 08:34:25 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf INT_MAX
#define fr(i,l,r) for (i=(l); i<=(r); i++)
#define rfr(i,l,r) for (i=(l); i>=(r); i--)
template<typename T>inline void read(T &n){
T w=1; n=0; char ch=getchar();
while (!isdigit(ch) && ch!=EOF){ if (ch=='-') w=-1; ch=getchar(); }
while (isdigit(ch) && ch!=EOF){ n=(n<<3)+(n<<1)+(ch&15); ch=getchar(); }
n*=w;
}
template<typename T>inline void write(T x){
if (x==0){ putchar('0'); return ; }
T tmp;
if (x>0) tmp=x;
else tmp=-x;
if (x<0) putchar('-');
char F[105];
long long cnt=0;
while (tmp){
F[++cnt]=tmp%10+48;
tmp/=10;
}
while (cnt) putchar(F[cnt--]);
}
#define Min(x,y) x = min(x,y)
#define Max(x,y) x = max(x,y)
//基础配置=================================================================================
const ll N = 200005;
ll n,m,ans=0,x,y,l,r,s,z;
ll c[N];
struct List{
struct node{
ll l,r;
}a[N];
void init(ll n){
a[0].r = 1; a[n+1].l = n;
for (ll i=1; i<=n; i++){
a[i].l = i-1;
a[i].r = i+1;
}
}
ll pr(ll x){ return a[x].l; }
ll nx(ll x){ return a[x].r; }
void del(ll x){
a[a[x].l].r = a[x].r;
a[a[x].r].l = a[x].l;
}
};
List ls;
set<ll> t;
void insert(ll x){
ll l,r;
l = ls.pr(x); r = ls.nx(x);
if (l<1 || r>n) return ;
if (c[l]!=c[r]) t.insert(x);
}
struct opt{
ll x,y;
opt(ll _x=0,ll _y=0){
x = _x; y = _y;
}
};
ll cnt=0; opt b[N+N];
ll sum[N];
// map<ll,map<ll,bool> > ma;
// void add(ll l,ll r){
// if (ma[l].count(r)) return ;
// b[++cnt] = opt(l,r);
// }
void del(ll x){
// printf("del : %lld\n",x);
ll l,r;
l = ls.pr(x); r = ls.nx(x);
ls.del(x); sum[c[x]]--;
// if (c[l]!=c[r]) add(l,r);
// if (c[l]!=c[r] && abs(l-r)!=1) b[++cnt] = opt(l,r);
if (c[l]!=c[r]) b[++cnt] = opt(l,r);
if (t.find(x)!=t.end()) t.erase(t.find(x));//一定要判一下!=end
insert(l); insert(r);
}
void spe_solve(ll x){
// printf("spe_solve : %lld\n",x);
while (ls.pr(x)!=1){
del(ls.pr(x));
// if (ls.pr(x)+1<x) b[++cnt] = opt(ls.pr(x),x);
// ls.del(ls.pr(x));
}
while (ls.nx(x)!=n){
del(ls.nx(x));
// if (x+1<ls.nx(x)) b[++cnt] = opt(x,ls.nx(x));
// ls.del(ls.nx(x));
}
if (c[1]!=c[n]) b[++cnt] = opt(1,n);
// if (c[1]!=c[n] && 2<n) b[++cnt] = opt(1,n);
}
void work(){
ll i,j;
read(n); read(m);
fr(i,1,n) read(c[i]);
fr(i,1,m) sum[i] = 0;
fr(i,1,n) sum[c[i]]++;
cnt = 0;
fr(i,1,n-1)
if (c[i]!=c[i+1]) b[++cnt] = opt(i,i+1);
ls.init(n);
t.clear();
fr(i,1,n) insert(i);
ll lun = n-2;
while (lun--){
if (t.begin()==t.end()){ del(ls.nx(1)); continue; }
ll x = *t.begin();
// printf("x = %lld\n",x);
if (sum[c[x]]==1){ spe_solve(x); break; }
else del(x);
}
fr(i,1,cnt)
if (b[i].x>b[i].y) swap(b[i].x,b[i].y);
printf("%lld\n",cnt);
fr(i,1,cnt) printf("%lld %lld\n",b[i].x,b[i].y);
}
int main(){
// freopen("a.in","r",stdin);
// freopen(".out","w",stdout);
ll qt;
read(qt);
while (qt--) work();
return 0;
}
//g++ a.cpp -o a -Wall -Wl,--stack=512000000
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 14040kb
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: 14004kb
input:
1 2 2 1 2
output:
1 1 2
result:
ok all 1 test passed
Test #3:
score: 0
Accepted
time: 2ms
memory: 12336kb
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 4 3 4 4 5 2 4 1 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:
ok all 10 test passed
Test #4:
score: 0
Accepted
time: 0ms
memory: 14124kb
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 2 4 2 5 1 6 8 2 3 3 4 4 5 5 6 6 7 1 3 1 5 1 7 7 2 3 4 5 5 6 1 3 1 4 1 6 1 7 6 3 4 5 6 2 4 1 4 1 5 5 7 7 1 2 3 4 4 5 6 7 1 3 1 5 1 6 7 1 2 2 3 6 7 2 4 2 5 2 6 1 7 7 1 2 2 3 3 4 3 5 3 6 3 7 1 7 8 2 3 3 4 4 5 5 6 6 7 1 3 1 5 1 7 7 1 2 3 4 4 5 5 6 1 3 1 5 5 7 7 2 3 3 4 4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