codeforces 792B——Counting-out Rhyme(数组模拟链表)

本文介绍了一种通过模拟链表来实现计数淘汰游戏的算法。游戏中,n名儿童围成一圈,按顺时针编号从1到n。游戏分为k轮,每轮由当前领导者计数并淘汰一名儿童,随后下一位儿童成为新的领导者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ≤ 1001 ≤ 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.

直接用数组模拟链表,记录后继节点

不过要注意如果转的是一整圈,记得修改节点。唯一的坑




#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int MAXN = 200+10;
int a[MAXN];
int b[MAXN];


int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",a+i);
    }
    for(int i=1;i<=n;i++){
        if(i==n)
            b[i]=1;
        else
            b[i]=i+1;
    }
    int cur=1;
    for(int i=0;i<k;i++){
        int x=a[i]%(n-i);
        if(x==0){
            int temp=b[cur];
            while(1){
                if(b[temp]==cur)
                    break;
                temp=b[temp];
            }
            b[temp]=b[cur];
        }
        while(x>0){
            int temp=cur;
            cur=b[cur];
            if(x==1){
                b[temp]=b[b[temp]];
            }
            x--;
        }
       // printf("[[[%d\n",b[11]);
        printf("%d%c",cur,i==k-1?'\n':' ');
        cur=b[cur];
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值