• ural1521 War Games 2


    War Games 2

    Time limit: 1.0 second
    Memory limit: 64 MB

    Background

    During the latest war games (this story is fully described in the problem "War games") the Minister of Defense of the Soviet Federation comrade Ivanov had a good chance to make sure personally, that an alertness of the Soviet Army under his command is just brilliant. But there was a thing, that continued to worry him. Being an outstanding commander, he realized, that only physical conditions of the soldiers were demonstrated. So the time came to organize one more war games and examine their mental capacity.
    General Rascal was appointed to be responsible for the war games again. The general donated the allocated funds to the poor and went to bed free-hearted. In his dream, the tactics manual appeared to him and described a scheme, that allows to organize the war games absolutely free of charge.

    Problem

    In accordance with this scheme, the war games are divided into N phases; and N soldiers, successively numbered from 1 to N, are marching round a circle one after another, i.e. the first follows the second, the second follows the third, ..., the (N-1)-th follows the N-th, and the N-th follows the first. At each phase, a single soldier leaves the circle and goes to clean the WC, while the others continue to march. At some phase, the circle is left by a soldier, who is marching Kpositions before the one, who left the circle at the previous phase. A soldier, whose number is K, leaves the circle at the first phase.
    Surely, Mr. Rascal cherished no hope about his soldiers' abilities to determine an order of leaving the circle. "These fools can not even paint the grass properly", - he sniffed scornfully and went to sergeant Filcher for an assistance.

    Input

    The only line contains the integer numbers N (1 ≤ N ≤ 100000) and K (1 ≤ K ≤ N).

    Output

    You should output the numbers of soldiers as they leave the circle. The numbers should be separated by single spaces.

    Sample

    inputoutput
    5 3
    
    3 1 5 2 4
    

    分析:约瑟夫出环顺序,树状数组;

       每次出环是第几个是知道的,所以只需二分求出在原环上的标号即可;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, rt<<1
    #define Rson mid+1, R, rt<<1|1
    const int maxn=1e5+10;
    const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,k,t,a[maxn],now;
    void add(int x,int y)
    {
        for(int i=x;i<=n;i+=(i&(-i)))
            a[i]+=y;
    }
    int get(int x)
    {
        int ans=0;
        for(int i=x;i;i-=(i&(-i)))
            ans+=a[i];
        return ans;
    }
    int main()
    {
        int i,j;
        scanf("%d%d",&n,&k);
        rep(i,1,n)add(i,1);
        for(i=n;i>=1;i--)
        {
            now=(now+k-1)%i;
            int l=1,r=n,ans;
            while(l<=r)
            {
                int mid=l+r>>1;
                if(get(mid)>=now+1)ans=mid,r=mid-1;
                else l=mid+1;
            }
            add(ans,-1);
            printf("%d ",ans);
        }
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    20145316许心远《Java学习笔记(第8版)》课程总结
    小棒组合第三周项目总结
    20145316《Java程序设计》第十周学习总结
    20145316第五次实验报告
    20145316《Java程序设计》第9周学习总结
    20145316第四次实验报告
    20145316 《Java程序设计》第8周学习总结
    MyBatis 的基本介绍及使用
    JPQL 的基本使用
    JPA API与注解
  • 原文地址:https://www.cnblogs.com/dyzll/p/5803132.html
Copyright © 2020-2023  润新知