codeforce 792B By Assassin [数据结构]

B. Counting-out Rhyme

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.

好久没有做codeforces了感觉英文都不会了…首先题目我半天没看懂…就是有编号为1-n的小朋友,进行k轮游戏,每次有一个小朋友作为起点顺时针数数,每次数多少给定为a[i] ,每轮结束后将本次的终点给删掉! 然后就成了比较经典的问题了,我想到了用vector数组加上迭代器进行数值的删减。

首先将1-n压入vector中,然后我们假设每个人的编号为0-n-1(至于为什么这么做就是为了方便取模,而且也方便我们利用迭代器的begin()位置找到应该删除的位置,体会一下)。然后我们只需要每次将找到的末尾位置用erase()去掉。那么每次的末尾位置的数值就是下一轮的起点位置,有点抽象,比如

现在1,2,3,4,5存在,1是起点,那么对应在vector中的位置是0,我们假设第一轮在4结束,那么4对应vector的位置是3(起点是0)当我们删掉4之后,起点5在vector中的位置也是4.

当然因为是环,每次取模需要注意模的数值大小都会变小,因为你从vector中删掉数值了嘛!

直接看代码吧!

#include<bits/stdc++.h>
using namespace std;
int n,k;
long long  value[101];
vector<long long>v;
int main(){
    int n;
    vector<long long>::iterator it;
    while(scanf("%d%d",&n,&k)!=EOF){
        for(int i=0;i<k;i++){
            scanf("%I64d",&value[i]);
        }
        v.clear();
        for(int i=1;i<=n;i++){
            v.push_back(i);
        }
        int pos=0;
        for(int i=0;i<k;i++){               //游戏总共进行k次 
            pos=(pos+value[i])%(n-i);       //注意每次的模的值都要减少 
            it=v.begin()+pos;               //找到本轮的末尾位置 
            cout<<(*it);
            if(i<k-1) cout<<" ";
            else cout<<endl;
            v.erase(it);                    //输出之后删掉该位置内容,pos作为新的起点相对位置没变,不需要改变数值大小 
        }
    }
    return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值