首页 > 其他分享 > Codeforces Round #733 D

Codeforces Round #733 D

时间:2022-10-17 23:45:05浏览次数:36  
标签:const int Codeforces long 733 ans return Round

D. Secret Santa

答案就是每一个数字是否出现
很容易想到的就是我们只能满足一个人的要求(如果这一组人都选择同一个人
所以我们直接就这样乱搞就可以了 然后剩下的随便连一连就行了
但是是不对的 因为剩下的几个点 我们随便连 不管按照什么方式都有可能
变成自己连自己
我们如何解决这个冲突呢
显然我们可以将该点要求满足
让被抢的那个点连接该点 这样子显然不会减少ans

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int M = 998244353;
const int mod = 1e9+7;
#define int long long
int up(int a,int b){return a<0?a/b:(a+b-1)/b;}
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define pi acos(-1)
#define INF 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);

void solve() {
    int n,cnt=0;cin>>n;
    vector<int>p(n+1),st(n+1),ans(n+1),v;
    map<int,int>mp;
    for(int i=1;i<=n;i++){
        cin>>p[i];
        st[p[i]]++;
        mp[p[i]]=i;
    }
    for(int i=1;i<=n;i++)if(!st[i])v.push_back(i);
    for(int i=1;i<=n;i++){
        if(st[p[i]]==1)ans[i]=p[i],cnt++;
        else{
            ans[i]=v.back(),v.pop_back();
            st[p[i]]--;
        }
    }
    cout<<cnt<<endl;
    for (int i = 1;i <= n; i++) {
        if (i == ans[i]) {
            ans[i] = p[i];
            ans[mp[p[i]]] = i;
            mp[p[i]] = i;
        }
        cout<<ans[i]<<' ';
    }cout<<endl;
}
signed main(){
    fast
    int t;t=1;cin>>t;
    while(t--) {
        solve();
    }
    return ~~(0^_^0);
}

标签:const,int,Codeforces,long,733,ans,return,Round
From: https://www.cnblogs.com/ycllz/p/16801155.html

相关文章

  • Codeforces Global Round 23 -D.Paths on the Tree
    题意给定一个树,树上每个节点i有一个权值s[i]。一共有k条从1(一定是根节点)开始的简单路径。设i点有c条路径通过,则其总权值为c*s.现在在限制:如果p[u]=i,p[v]=i,则abs(c[u]......
  • Codeforces Round #726 E1
    E1.EraseandExtend(EasyVersion)首先我们来证一个东西就是最优解一定由先删若干次然后一直copy而来而不会在中途再删一次因为在中途再删一次就证明这个后缀不如前......
  • Codeforces Round #828 (Div. 3)
    CodeforcesRound#828(Div.3)https://codeforces.com/contest/1744罚时爆炸的a~e1A.NumberReplacement数字一样的对应字母一定要一样#include<bits/stdc++.h>......
  • Codeforces Round #827 (Div. 4) 复盘+题解
    原比赛链接复盘:ABC签到,手速太慢了。D捣鼓了好久才想起来从更小的值域出发去做。E简单二分答案。然后就timeout了。D题搞错方向浪费太久时间了。F思维题,拐两个弯再$r......
  • Codeforces Round #729 (Div. 2) C
    C.StrangeFunction考虑反想我们将x确定看看有多少个i对于f[i]=x我们显然i%lcm(1,2,3,...x-1)!=0这里就可以通过容斥直接求解i%lcm(1,2,3,...x-1)是含有1,2,3,...x-1......
  • Educational Codeforces Round 112 D
    D.SayNotoPalindromes很牛逼我们手动模拟一下可以知道只有3个字母不构成回文串只有可能是这样的abcabc....acbacb.......6种情况所以直接暴力预处理即可#inclu......
  • Codeforces Global Round 16 D
    D2.SeatingArrangements(hardversion)题意我们要先按照a来排序然后再来安排d的位置最开始都能想到的一点就是我们可以每一组内按照逆序排序我们就可以让组内是0贡......
  • Codeforces Round #781 (Div. 2) - D. GCD Guess
    GCD+位运算[Problem-1665D-Codeforces](https://codeforces.com/problemset/problem/1627/D)题意交互题,有一个未知数\(x\;(1<=x<=10^9)\),最多有30次询问,每次......
  • Codeforces Round #742 (Div. 2) C
    C.CarryingConundrum这样子进位显然奇偶就独立了我们分别对于奇偶计算方案数然后乘法法则即可比如我们现在奇数位num1偶数位num2我们的方案就是num1+1偶数位就是n......
  • Codeforces Round #628 (Div. 2)——D(二进制,构造,思维)
    https://codeforces.com/problemset/problem/1325/D题目大意给你\(u,v\)两个数,叫你构造出最短的数组,满足所有的异或等于\(u\),所有的和等于\(v\)思路首先我们可以......