Secret Santa 秘密圣诞老人

Secret Santa
秘密圣诞老人
来源codeforses #1530d链接: link.

time limit per test: 2 seconds
每次测试的时间限制:2秒

memory limit per test:512 megabytes
memory limit per test:512 megabytes

input: standard input
输入:标准输入

output: standard output
产出:标准产出

Every December,VK traditionally holds an event for its employees named “Secret Santa”.Here’s how it happens.
每年12月,VK都会为员工举办名为“秘密圣诞老人”的活动。

n.employees numbered from 1 to ntake part in the event.Each employee i is assigned a diferentemployee bj, to which employee i has to
编号从1到n参加事件的雇员。每个雇员我被分配一个不同的雇员Bj,而我必须给他们分配一个不同的雇员Bj。

make a new year gi.Each employee is assigned to exactly one other employee, and nobody is assigned to themselves(but tiwo employees
make a new year gi.Each employee is assigned to exactly one other employee, and nobody is assigned to themselves(but tiwo employees

may be assigned to each other).Formally, all b; must be distinct integers between 1 and n, and for any i,b,≠i must hold.
形式上,所有b;必须是介于1和n之间的不同整数,对于任何i、b、≠,我必须保持。

The assignment is usually generated randomly.This year, as an experiment allevent partcipants have been asked who they wish to make a
作业通常是随机产生的。这一年,作为一项实验,参与者被问到他们希望做谁

gift to.Each employee i has said that they wish to make a gift to employee a;t.
我说过的每一位员工都想给员工送一份礼物。

Find a valid assignment b that maximizes the number of fulfilled wishes of the employees.
Find a valid assignment b that maximizes the number of fulfilled wishes of the employees.

lnput
输入

Each test contains multiple test cases.The firt line contains the number of test cases t(1 Each test contains multiple test cases.The firt line contains the number of test cases t(1

Each test case consists of twolines.The first line contains a single integer n(2 每个测试用例由两个测试用例组成,第一行包含一个整数n(2

The second line contains n integers a1,(2,….,an(1≤ai≤n;;≠)一wishes of the employees in order from 1 to n.
第二行包含n个整数A1,(2,…),a(1≤ai≤n;;≠)一愿望),按1到n的顺序排列。

lt is guaranteed that the sum of n over all test cases does not exceed 2.10.
保证所有测试用例的n和不超过2.10。

output
输出量

For each test case, print two lines.
对于每个测试用例,打印两行。

ln the first line, print a single integerk(0≤k 在第一行中,打印一个整数(0≤k

In the second line,print n distinct integers b,b,…bn(1≤b 在第二行中,打印n个不同的整数b,b,.bn(1≤b

1,2,… , n.
1,2,… , n.

k must be equal to the number of values of i such that a = by, and must be as large as possible.ifthere are mutiple answers, print any.
K必须等于i的值,使a=by,并且必须尽可能大。如果有多个答案,请打印任何答案。

双链表
不要指自己

int a[100001], b[100001], c[100001],d[100001];
int book[100001];
int main()
{
   
    int t;
    cin >> t;
    while (t--)
    {
        int n,num=0;
        cin >> n;
        for (int i = 0; i <=n; ++i)
            book[i] = 0,b[i]=0;
        for (int i = 1; i <= n; ++i)
        {
            int li;
            cin >> li;
            a[i] = li;
            if (!book[li])
                 b[li] = i, ++book[li];
            else
                c[num] = i,d[num++]=li;
        }
        cout << n - num << endl;
        for (int i = 0; i < num; ++i)
        {
            int le=c[i];
                while (1)
                {
                    if (!b[le]||b[le]==b[d[i]])
                        break;
                    else
                        le = b[le];
                }
                if (b[le]==b[d[i]])
                {
                    a[c[i]] = b[d[i]];
                    b[b[d[i]]] = a[c[i]];
                }
                else
                {
                    a[b[d[i]]] = le;
                    b[le] = b[d[i]];
                    b[d[i]] = c[i];
                }
        }
        for (int i = 1; i <= n; ++i)
            cout << a[i] << ' ';
        cout << endl;
    }
    return 0;
}

你可能感兴趣的:(cf)