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);
        }
    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章