HDU 5860 Death Sequence(递推)

题意:一排n个人,编号从1到n,从1开始每隔k选一个人,选完后,剩下的人重新排列,继续选,直到选完。

思路:为了方便我们编号从0开始,设此时编号i,如果i被k整除,那么i在第一轮被选出,否则,i可以用i - i / k - 1代替。求出所有人在第几轮第几个选出,然后排个序输出就行了。

#include <cstdio>
#include <algorithm>
#include <iostream>
#include<vector>
#include<cmath>
#include<set>
#include<cstring>
#include<map>
using namespace std;
const int maxn = 31e6 + 10;
const int maxt = 100200;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double pi = acos(-1.0);
typedef long long ll;
struct Node{
    int x, y;
    int id;
    bool operator<(const Node& rhs) const{
        return x < rhs.x || x == rhs.x && y < rhs.y;
    }
}a[maxn];
int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        int n, k, q;
        scanf("%d%d%d", &n, &k, &q);
        int cnt = 1;
        for(int i = 0; i < n; ++i){
            if(i % k == 0){
                a[i].x = 1;
                a[i].y = cnt++;
                a[i].id = i + 1;
            }
            else{
                a[i].x = a[i - i / k - 1].x + 1;
                a[i].y = a[i - i / k - 1].y;
                a[i].id = i + 1;
            }
        }
        sort(a, a + n);
        while(q--){
            int x;
            scanf("%d", &x);
            printf("%d\n", a[x - 1].id);
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值