zoukankan      html  css  js  c++  java
  • SPOJ 227 Ordering the Soldiers

    As you are probably well aware, in Byteland it is always the military officer's main worry to order his soldiers on parade correctly. In Bitland ordering soldiers is not really such a problem. If a platoon consists of n men, all of them have different rank (from 1 - lowest to n - highest) and on parade they should be lined up from left to right in increasing order of rank.

    Sounds simple, doesn't it? Well, Msgt Johnny thought the same, until one day he was faced with a new command. He soon discovered that his elite commandos preferred to do the fighting, and leave the thinking to their superiors. So, when at the first rollcall the soldiers lined up in fairly random order it was not because of their lack of discipline, but simply because they couldn't work out how to form a line in correct order of ranks. Msgt Johnny was not at all amused, particularly as he soon found that none of the soldiers even remembered his own rank. Over the years of service every soldier had only learned which of the other soldiers were his superiors. But Msgt Johnny was not a man to give up easily when faced with a true military challenge. After a moment's thought a solution of brilliant simplicity struck him and he issued the following order: "men, starting from the left, one by one, do: (step forward; go left until there is no superior to the left of you; get back in line).". This did indeed get the men sorted in a few minutes. The problem was solved... for the time being.

    The next day, the soldiers came in exactly the same order as the day before, and had to be rearranged using the same method. History repeated. After some weeks, Msgt Johnny managed to force each of his soldiers to remember how many men he passed when going left, and thus make the sorting process even faster.

    If you know how many positions each man has to walk to the left, can you try to find out what order of ranks the soldiers initially line up in?

    Input

    The first line of input contains an integer t<=50, the number of test cases. It is followed by t test cases, each consisting of 2 lines. The first line contains a single integer n (1<=n<=200000). The second line contains n space separated integers wi, denoting how far the i-th soldier in line must walk to the left when applying Msgt Johnny's algorithm.

    Output

    For each test case, output a single line consisting of n space separated integers - the ranks of the soldiers, given from left to right in their initial arrangement.

    Example

     Input:
    2
    3
    0 1 0
    5
    0 1 2 0 1

    Output:
    2 1 3
    3 2 1 5 4 

    这题以前是用线段树做的,现在用树状数组来做,这里学到了求逆序数的方法,是用二分来做,因为空位的个数是从左往右一次递增的,所以只要找到等于i-a[i]这个空位的,且之前没有被访问过的(也可以认为是最小的位置),该位置就是i所对应的位置。

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define maxn 200006
    int a[maxn],b[maxn],n,pos[maxn],vis[maxn];
    int lowbit(int x){
    	return x&(-x);
    }
    void update(int pos,int num)
    {
    	while(pos<=maxn){
    		b[pos]+=num;pos+=lowbit(pos);
    	}
    }
    
    int getsum(int pos)
    {
    	int num=0;
    	while(pos>0){
    		num+=b[pos];pos-=lowbit(pos);
    	}
    	return num;
    }
    
    int find(int l,int r,int x)
    {
    	int mid;
    	while(l<=r){
    		mid=(l+r)/2;
    		if(getsum(mid)==x){
    			if(vis[mid]==0)return mid;
    			else {
    				r=mid-1;continue;
    			}
    		}
    		if(getsum(mid)>x)r=mid-1;
    		else l=mid+1;
    	}
    }
    
    int main()
    {
    	int m,i,j,T,t;
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%d",&n);
    		for(i=1;i<=n;i++){
    			scanf("%d",&a[i]);
    			b[i]=lowbit(i);
    			vis[i]=0;
    		}
    		for(i=n;i>=1;i--){
    			t=i-a[i];
    			j=find(1,n,t);
    			pos[i]=j;
    			vis[j]=1;
    			update(j,-1);
    		}
    		for(i=1;i<=n;i++){
    			if(i==n)printf("%d
    ",pos[i]);
    			else printf("%d ",pos[i]);
    		}
    	}
    	return 0;
    }



  • 相关阅读:
    ubuntu18+k8s单机版+kuboard+harbor安装笔记
    Apache commons StringSubstitutor 替换占位符
    Kafka消费与心跳机制
    本地机器如何访问服务器上的docker容器内的tensorboard?
    Pytorch cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.解决办法
    Docker常用方法总结
    SpringBoot
    新版chrome中非https无法打开摄像头
    DDIA----笔记(不定时更新)
    Windows 无法验证此设备所需的驱动程序的数字签名。最近的硬件或软件更改安装的文件可能未正确签名或已损坏,或者可能是来自未知来源的恶意软件。 (代码 52)
  • 原文地址:https://www.cnblogs.com/herumw/p/9464754.html
Copyright © 2011-2022 走看看