QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#380676#6545. Connect the DotszhulexuanWA 3ms14068kbC++143.0kb2024-04-07 08:26:242024-04-07 08:26:25

Judging History

你现在查看的是最新测评结果

  • [2024-04-07 08:26:25]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:14068kb
  • [2024-04-07 08:26:24]
  • 提交

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];
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]) 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){
        b[++cnt] = opt(ls.pr(x),x);
        ls.del(ls.pr(x));
    }
    while (ls.nx(x)!=n){
        b[++cnt] = opt(x,ls.nx(x));
        ls.del(ls.nx(x));
    }
    if (c[1]!=c[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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 14068kb

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: 3ms
memory: 14044kb

input:

1
2 2
1 2

output:

1
1 2

result:

ok all 1 test passed

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 12000kb

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 4
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 TC #7: two identical wire.