Codeforces Round #733 (Div. 1 + Div. 2) D. Secret Santa

Codeforces Round #733 (Div. 1 + Div. 2) D. Secret Santa

题目内容
输入样例

题意:

  1. 有t组样例
  2. 输入一个长度n
  3. 输入n个数字
  4. a[1],a[2],a[3], … ,a[n];
  5. 求一个i!=a[i]的序列(错排)

尝试过程

将所有数,放入vis中,判断是否出现大于一次,如果出现1次以上,将数放入b数组中,从大到小排序,将b放回到数组中去,如果当前的 i==b[j],与下一个位置的b交换。

wa了一次后,
如果当前的位置的vis大于1,并且i!=b[j],将a[i]=b[j]。按从大到小排序,最后将所有b放入a中。

TLE 因为memset超时,改了很久,最后才发现。

AC代码

方法一

将所有数,放入vis中,判断是否出现大于一次,如果出现1次以上,将数放入b数组中,从大到小排序,如果当前的位置的vis大于1,并且i!=b[j],将a[i]=b[j]。按从大到小排序,最后将所有b放入a中

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+9;
 
int n;
int v[N];
int a[N],b[N];
bool cmp(int a,int b){
    return a>b;
}
int main(){
    int i,j;
    int t;
    scanf("%d", &t);
    while(t--){
        int len=1;
        for (int i = 1; i <= n; i ++ ){
            v[i]=b[i]=0;
        }

        scanf("%d", &n);
        for (int i = 1; i <= n; i ++ ){
            scanf("%d", &a[i]);
            v[a[i]]++;
        }
        for (int i = 1; i <= n; i ++ ){
            if(v[i]==0)  b[len++]=i;
        }
        
        sort(b+1,b+len+1,cmp);
        int num=0;
        for (i = 1,j= 1; i <= n; i ++ ){
            if(v[a[i]]>1&&i!=b[j]){
                v[a[i]]--;
                a[i]=b[j++];
                num++;
            }
        }
        
        for (int i = 1; i <= n; i ++ ){
            if(v[a[i]]>1){
                v[a[i]]--;
                a[i]=b[--len];
                num++;
            }
        }
        
        printf("%d\n",n-num);
        for (int i = 1; i <= n; i ++ ) printf("%d ",a[i]);
        puts("");
    }
}

方法二

将所有数,放入vis中,判断是否出现大于一次,如果出现1次以上,将数放入b数组中,从大到小排序,如果当前的位置的vis大于1,并且i!=b[j],将a[i]=b[j],否则continue。

因为可以将后一次出现的位置上的数进行更改,所以不会出现i==a[i]的情况。

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+9;
 
int n;
int v[N];
int a[N],b[N];
bool cmp(int a,int b){
    return a>b;
}
int main(){
    int i,j;
    int t;
    scanf("%d", &t);
    while(t--){
        int len=1;
        for (int i = 1; i <= n; i ++ ){
            v[i]=b[i]=0;
        }
        // memset(v, 0, sizeof v);
        // memset(b, 0, sizeof b);
        scanf("%d", &n);
        for (int i = 1; i <= n; i ++ ){
            scanf("%d", &a[i]);
            v[a[i]]++;
        }
        for (int i = 1; i <= n; i ++ ){
            if(v[i]==0)  b[len++]=i;
        }
        
        sort(b+1,b+len+1,cmp);
        int num=0;
        for (i = 1,j= 1; i <= n; i ++ ){
            if(v[a[i]]>1){
                if(i==b[j]) continue;
                v[a[i]]--;
                a[i]=b[j++];
                num++;
            }
        }
        
        printf("%d\n",n-num);
        for (int i = 1; i <= n; i ++ ) printf("%d ",a[i]);
        puts("");
    }
}

引用大佬解释

Another Solution for D : First, Place all the values of b[i]=a[i] where a[i] is chosen to given gift by only one member

Then, Those who are desired by more than one person give them any random person as it wont matter (Explained later).

Then, maintain of set of variables who have not yet received any gift from any person and start from index 0 and the person who hasn’t chosen yet ,give him a gift not equal to itself i.e either beginning element of set and ending element of set.

Now comes the main part u should notice that there can be atmost one value equal to itself and and the person whom this index wished to give a gift must have atleast 2 person desiring him so simply just find another index with same value of a[i] and just swap the two and you will acheive the desirable state.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值