zoukankan      html  css  js  c++  java
  • codeforces 792 B. Counting-out Rhyme 约瑟夫环

    ---恢复内容开始---

    B. Counting-out Rhyme
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The last one to be pointed at by the leader is eliminated, and the next player after him becomes the new leader.

    For example, if there are children with numbers [8, 10, 13, 14, 16] currently in the circle, the leader is child 13 and ai = 12, then counting-out rhyme ends on child 16, who is eliminated. Child 8 becomes the leader.

    You have to write a program which prints the number of the child to be eliminated on every step.

    Input

    The first line contains two integer numbers n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ n - 1).

    The next line contains k integer numbers a1, a2, ..., ak (1 ≤ ai ≤ 109).

    Output

    Print k numbers, the i-th one corresponds to the number of child to be eliminated at the i-th step.

    Examples
    input
    7 5
    10 4 11 4 1
    output
    4 2 5 6 1 
    input
    3 2
    2 5
    output
    3 2 
    Note

    Let's consider first example: 

    • In the first step child 4 is eliminated, child 5 becomes the leader. 
    • In the second step child 2 is eliminated, child 3 becomes the leader. 
    • In the third step child 5 is eliminated, child 6 becomes the leader. 
    • In the fourth step child 6 is eliminated, child 7 becomes the leader. 
    • In the final step child 1 is eliminated, child 3 becomes the leader.

    一道典型的约瑟夫环问题,将输入进来的序列取余,就不会超时了

     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     int n,k;
    10     int num[105];
    11     while(cin>>n>>k)
    12     {
    13         int temp;
    14         for(int i = 1; i <= k; i++)
    15             cin>>temp,num[i]=temp%(n-i+1); //这里取余很重要,用来算位置
    16         int child[105];
    17         memset(child,0,sizeof(child));
    18         int pos=1;
    19         int ans[105];
    20         int cc=0;
    21         for(int i = 1; i <= k; i++)
    22         {
    23             int con=0;
    24             while(con<=num[i])
    25             {
    26                 for(int j = pos; j <= n; j++)
    27                     if(child[j]==0 && con<=num[i])
    28                         pos=j,con++;
    29                 if(con<=num[i])
    30                     pos=1;
    31             }
    32             child[pos]=1;
    33             ans[cc++]=pos;
    34             pos++;
    35         }
    36         for(int i = 0; i < cc-1; i++)
    37             cout<<ans[i]<<' ';
    38         cout<<ans[cc-1]<<endl;
    39     }
    40     
    41     return 0;
    42 }

    ---恢复内容结束---

  • 相关阅读:
    logo切图大小相应的尺寸
    logo切图大小相应的尺寸
    Linux学习笔记(二)——文件/目录/VIM
    Linux学习笔记(二)——文件/目录/VIM
    什么湖南小吃?仅1年就获得千万投资
    硕士毕业的他做生鲜电商网站,日流水3万元
    他回到家乡贷款创业,如今公司年产值100万美元
    下岗工人也能致富,她靠养殖昆虫改变人生
    他用8000元互联网创业起家,如今年营业额500万
    他卖骨头汤成为南京“汤王”,如今资产上百万
  • 原文地址:https://www.cnblogs.com/Xycdada/p/6692956.html
Copyright © 2011-2022 走看看