程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 1890 SplayÇø¼ä·­×ª

HDU 1890 SplayÇø¼ä·­×ª

編輯:C++入門知識

HDU 1890 SplayÇø¼ä·­×ª


D - Robotic Sort Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1890 Appoint description:

Description

Somewhere deep in the Czech Technical University buildings, there are laboratories for examining mechanical and electrical properties of various materials. In one of yesterday¡¯s presentations, you have seen how was one of the laboratories changed into a new multimedia lab. But there are still others, serving to their original purposes.

In this task, you are to write software for a robot that handles samples in such a laboratory. Imagine there are material samples lined up on a running belt. The samples have different heights, which may cause troubles to the next processing unit. To eliminate such troubles, we need to sort the samples by their height into the ascending order.

Reordering is done by a mechanical robot arm, which is able to pick up any number of consecutive samples and turn them round, such that their mutual order is reversed. In other words, one robot operation can reverse the order of samples on positions between A and B.

A possible way to sort the samples is to find the position of the smallest one (P1) and reverse the order between positions 1 and P1, which causes the smallest sample to become first. Then we find the second one on position P and reverse the order between 2 and P2. Then the third sample is located etc.

\


The picture shows a simple example of 6 samples. The smallest one is on the 4th pZ†·Ÿ"http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc2l0aW9uLCB0aGVyZWZvcmUsIHRoZSByb2JvdCBhcm0gcmV2ZXJzZXMgdGhlIGZpcnN0IDQgc2FtcGxlcy4gVGhlIHNlY29uZCBzbWFsbGVzdCBzYW1wbGUgaXMgdGhlIGxhc3Qgb25lLCBzbyB0aGUgbmV4dCByb2JvdCBvcGVyYXRpb24gd2lsbCByZXZlcnNlIHRoZSBvcmRlciBvZiBmaXZlIHNhbXBsZXMgb24KIHBvc2l0aW9ucyAyPzYuIFRoZSB0aGlyZCBzdGVwIHdpbGwgYmUgdG8gcmV2ZXJzZSB0aGUgc2FtcGxlcyAzPzQsIGV0Yy4gPGJyPgo8YnI+CllvdXIgdGFzayBpcyB0byBmaW5kIHRoZSBjb3JyZWN0IHNlcXVlbmNlIG9mIHJldmVyc2FsIG9wZXJhdGlvbnMgdGhhdCB3aWxsIHNvcnQgdGhlIHNhbXBsZXMgdXNpbmcgdGhlIGFib3ZlIGFsZ29yaXRobS4gSWYgdGhlcmUgYXJlIG1vcmUgc2FtcGxlcyB3aXRoIHRoZSBzYW1lIGhlaWdodCwgdGhlaXIgbXV0dWFsIG9yZGVyIG11c3QgYmUgcHJlc2VydmVkOiB0aGUgb25lIHRoYXQgd2FzIGdpdmVuIGZpcnN0IGluIHRoZSBpbml0aWFsIG9yZGVyIG11c3QKIGJlIHBsYWNlZCBiZWZvcmUgdGhlIG90aGVycyBpbiB0aGUgZmluYWwgb3JkZXIgdG9vLgogCgoKCjxwIGNsYXNzPQ=="pst"> Input

The input consists of several scenarios. Each scenario is described by two lines. The first line contains one integer number N , the number of samples, 1 ¡Ü N ¡Ü 100 000. The second line lists exactly N space-separated positive integers, they specify the heights of individual samples and their initial order.

The last scenario is followed by a line containing zero.

Output

For each scenario, output one line with exactly N integers P1 , P1 , . . . PN ,separated by a space.
Each Pi must be an integer (1 ¡Ü Pi ¡Ü N ) giving the position of the i-th sample just before the i-th reversal operation.

Note that if a sample is already on its correct position Pi , you should output the number Pi anyway, indicating that the ¡°interval between Pi and Pi ¡± (a single sample) should be reversed.

Sample Input

 6
3 4 5 1 6 2
4
3 3 2 1
0 

Sample Output

 4 6 4 5 6 6
4 2 4 4 

Ó÷­×ª¶ÔÇø¼äÅÅÐò£¬½¨Ò»¸ösplayΪ1,2£¬...n£¬¸ãÒ»¸ö½á¹¹ÌåÊý×éA[maxn]£¬first±£´ækey£¬second±£´æÔÚ³õʼÊý×éÖеÄϱ꣬°´ÕÕkeyÅŸöÐò£¬ÔòA[i].second¾ÍÊǵÚi´óµÄÔªËØÔÚsplayÖеıàºÅ£¬Ã¿´Î½«ÆäÐýתµ½¸ú£¬±ÈËûСµÄÔªËؼÓÉÏi¾ÍÊÇ´ËʱËûÔÚÔªËØÖеÄλÖã¬Êä³öºóɾ³ýÊ÷¸ù(ÒòΪÕâÒ»²¿·ÖÒѾ­ÓÐÐòÀ²,²»É¾³ý»á¶ÔºóÃæµÄ²Ù×÷ÓÐÓ°Ïì)¡£·­×ª×îºÃÒ»´Î·­×ªÁ½²ã£¬¿ªÊ¼ÎÒÖ»·­×ªÒ»²ã¾Í³¬Ê±ÁË¡£

´úÂ룺

/*************************************************************************
    > File Name: Spaly.cpp
    > Author: acvcla
    > QQ:
    > Mail: [email protected]
    > Created Time: 2014Äê11ÔÂ16ÈÕ ÐÇÆÚÈÕ 00ʱ14·Ö26Ãë
 ************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 100;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int ch[maxn][2],pre[maxn],siz[maxn],rev[maxn];
int root,tot;
struct  node
{
	int first,second;
	bool operator < (const node &a)const{
		if(first==a.first)return secondR)return;
	int mid=(R+L)>>1;
	newnode(x,fa,mid);
	built(ch[x][0],L,mid-1,x);
	built(ch[x][1],mid+1,R,x);
	push_up(x);
}
inline void init(int n)
{
	pre[0]=siz[0]=0;
	root=tot=ch[0][0]=ch[0][1]=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&A[i].first);
		A[i].second=i;
	}
	sort(A+1,A+1+n);
	built(root,1,n,0);
}
int main(int argc, char const *argv[])
{
	int n;
	while(~scanf("%d",&n)&&n){
		init(n);
		rep(i,1,n-1){
			Splay(A[i].second,0);
			update_rev(ch[root][0]);
			printf("%d ",i+siz[ch[root][0]]);
			remove_root();
		}
		printf("%d\n",n);
	}
	return 0;
}





  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved