zoukankan      html  css  js  c++  java
  • 2016暑假多校联合---Death Sequence(递推、前向星)

    原题链接

    Problem Description
    You may heard of the Joseph Problem, the story comes from a Jewish historian living in 1st century. He and his 40 comrade soldiers were trapped in a cave, the exit of which was blocked by Romans. They chose suicide over capture and decided that they would form a circle and start killing themselves using a step of three. Josephus states that by luck or maybe by the hand of God, he and another man remained the last and gave up to the Romans.

    Now the problem is much easier: we have N men stand in a line and labeled from 1 to N, for each round, we choose the first man, the k+1-th one, the 2*k+1-th one and so on, until the end of the line. These poor guys will be kicked out of the line and we will execute them immediately (may be head chop, or just shoot them, whatever), and then we start the next round with the remaining guys. The little difference between the Romans and us is, in our version of story, NO ONE SURVIVES. Your goal is to find out the death sequence of the man.

    For example, we have N = 7 prisoners, and we decided to kill every k=2 people in the line. At the beginning, the line looks like this:

    1 2 3 4 5 6 7

    after the first round, 1 3 5 7 will be executed, we have

    2 4 6

    and then, we will kill 2 6 in the second round. At last 4 will be executed. So, you need to output 1 3 5 7 2 6 4. Easy, right?

    But the output maybe too large, we will give you Q queries, each one contains a number m, you need to tell me the m-th number in the death sequence.
     
    Input
    Multiple cases. The first line contains a number T, means the number of test case. For every case, there will be three integers N (1<=N<=3000000), K(1<=K), and Q(1<=Q<=1000000), which indicate the number of prisoners, the step length of killing, and the number of query. Next Q lines, each line contains one number m(1<=m<=n).
     
    Output
    For each query m, output the m-th number in the death sequence.
     
    Sample Input
    1
    7 2 7
    1
    2
    3
    4
    5
    6
    7
     
    Sample Output
    1
    3
    5
    7
    2
    6
    4
     
    Author
    BUPT
     
    Source
     
    Recommend
    wange2014   |   We have carefully selected several similar problems for you:  5867 5866 5865 5864 5863 
     
    题意:输入n表示1到n ,输入k,q   去掉一些数,从1开始 1、k+1、2*k+1、3*k+1、4*k+1、5*k+1........直到i*k+1>n结束   然后剩下的数又成一个1到(n)  的序列,重复操作,直至序列为空,q次询问,每次输入x,输出第x次去掉的数的下标;
     
    思路:仔细推一下,发现存在递推关系,若第i个数满足(i-1)%k==0 则a[i]=1;否则a[i]=a[i-(i-1)/k-1]+1;  并且在循环中用前行星记录当前数是在第几层去掉的,然后再一重循环从层次最大处开始给a[i]赋值是第i次去掉的数在序列中的下标;
     
    代码如下:
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <map>
    using namespace std;
    const int N=3000005;
    int head[N];
    int a[N];
    struct Node
    {
        int to;
        int next;
    }node[N];
    
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            int n,k,q;
            scanf("%d%d%d",&n,&k,&q);
            memset(head,0,sizeof(head));
            int tot=1;
            for(int i=1;i<=n;i++)
            {
                if((i-1)%k==0) a[i]=1;
                else a[i]=a[i-(i-1)/k-1]+1;
                node[tot].to=i;
                node[tot].next=head[a[i]];
                head[a[i]]=tot++;
            }
            int cnt=n;
            for(int i=tot-1;i>=1;i--)
            {
                for(int j=head[i];j;j=node[j].next)
                {
                    a[cnt--]=node[j].to;
                }
            }
            while(q--)
            {
                int x;
                scanf("%d",&x);
                printf("%d
    ",a[x]);
            }
        }
        return 0;
    }
  • 相关阅读:
    SpringMVC中@Controller和@RequestMapping用法和其他常用注解
    eclipse maven install 时控制台乱码问题解决
    使用模板实现编译期间多态(类名当参数)
    QT中Dialog的使用(使用QStackedWidget维护页面切换)
    QT中的各种对话框
    Qt 5 最小构建笔记(只编译QtBase)
    忽然懂了:“视图”的用途不仅仅是临时表,更适用于变化比较大的情况,而且不用改客户端一行代码
    React-Native
    一位OWin服务器新成员TinyFox
    Access Toke调用受保护的API
  • 原文地址:https://www.cnblogs.com/chen9510/p/5792393.html
Copyright © 2011-2022 走看看