• C. Friends and Gifts


    C. Friends and Gifts

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    There are nn friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself.

    For each friend the value fifi is known: it is either fi=0fi=0 if the ii-th friend doesn't know whom he wants to give the gift to or 1≤fi≤n1≤fi≤n if the ii-th friend wants to give the gift to the friend fifi.

    You want to fill in the unknown values (fi=0fi=0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory.

    If there are several answers, you can print any.

    Input

    The first line of the input contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of friends.

    The second line of the input contains nn integers f1,f2,…,fnf1,f2,…,fn (0≤fi≤n0≤fi≤n, fi≠ifi≠i, all fi≠0fi≠0 are distinct), where fifi is the either fi=0fi=0 if the ii-th friend doesn't know whom he wants to give the gift to or 1≤fi≤n1≤fi≤n if the ii-th friend wants to give the gift to the friend fifi. It is also guaranteed that there is at least two values fi=0fi=0.

    Output

    Print nn integers nf1,nf2,…,nfnnf1,nf2,…,nfn, where nfinfi should be equal to fifi if fi≠0fi≠0 or the number of friend whom the ii-th friend wants to give the gift to. All values nfinfi should be distinct, nfinfi cannot be equal to ii. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself.

    If there are several answers, you can print any.

    Examples

    input

    Copy

    5
    5 0 0 2 4
    

    output

    Copy

    5 3 1 2 4 
    

    input

    Copy

    7
    7 0 0 1 4 0 6
    

    output

    Copy

    7 3 2 1 4 5 6 
    

    input

    Copy

    7
    7 4 0 3 0 5 1
    

    output

    Copy

    7 4 2 3 6 5 1 
    

    input

    Copy

    5
    2 1 0 0 0
    

    output

    Copy

    2 1 4 5 3 
    

    =========================================================================

    很自然的思路是把没送礼的和没收礼的放在队列里进行一一配对,并且相同的不能配对,如果仅仅是这样的话由于加入队列顺序的原因,很可能造成我们最终还是配对了相同的。所以我们可以在发现配对相同的时候,交换和上一个的答案,这样就完成了完美的匹配

    1. # include
    2. # include
    3. # include
    4. # include
    5. # include
    6. using namespace std;
    7. int a[200000+10];
    8. bool book1[200000+10],book2[200000+10];
    9. queue<int>v1,v2;
    10. int main ()
    11. {
    12. int n;
    13. cin>>n;
    14. for(int i=1; i<=n; i++)
    15. {
    16. cin>>a[i];
    17. if(a[i])
    18. {
    19. book1[i]=1;
    20. book2[a[i]]=1;
    21. }
    22. }
    23. // book1是已经送 book2是已经收礼
    24. for(int i=1; i<=n; i++)
    25. {
    26. if(!book1[i])
    27. {
    28. v1.push(i);
    29. }
    30. if(!book2[i])
    31. {
    32. v2.push(i);
    33. }
    34. }
    35. int pre;
    36. while(!v1.empty())
    37. {
    38. int now=v1.front();
    39. v1.pop();
    40. while(!v2.empty())
    41. {
    42. int temp=v2.front();
    43. v2.pop();
    44. if(temp==now)
    45. {
    46. if(v2.size())
    47. {
    48. int temp2=v2.front();
    49. v2.pop();
    50. a[now]=temp2;
    51. v2.push(temp);
    52. break;
    53. }
    54. else
    55. {
    56. int temp3=a[pre];
    57. a[pre]=now;
    58. a[now]=temp3;
    59. }
    60. break;
    61. }
    62. a[now]=temp;
    63. break;
    64. }
    65. pre=now;
    66. }
    67. for(int i=1; i<=n; i++)
    68. {
    69. cout<" ";
    70. }
    71. return 0;
    72. }

  • 相关阅读:
    如何高效利用阿里云Docker镜像仓库管理您的容器镜像
    BatchNormalization:解决神经网络中的内部协变量偏移问题
    java计算机毕业设计高校网上报销系统MyBatis+系统+LW文档+源码+调试部署
    会员权益营销中,等级会员的五种权益设置
    Halcon 常用通道Scale灰度元操作整理
    用excel计算一个矩阵的转置矩阵
    Seurat | 完美整合单细胞测序数据(部分交集数据的整合)(一)
    Shell脚本大全
    Word控件Spire.Doc 【图像形状】教程(8): 如何借助C#/VB.NET在 Word 中插入艺术字
    vue3监听input保留两位小数点
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126303976