HDOJ 2404 Permutation Recovery (逆序列)

Permutation Recovery

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 474    Accepted Submission(s): 331


Problem Description
Professor Permula gave a number of permutations of the n integers 1, 2, ..., n to her students. For each integer i (1 <= i <= n), she asks the students to write down the number of integers greater than i that appears before i in the given permutation. This number is denoted ai. For example, if n = 8 and the permutation is 2,7,3,5,4,1,8,6, then a1 = 5 because there are 5 numbers (2, 7, 3, 5, 4) greater than 1 appearing before it. Similarly, a4 = 2 because there are 2 numbers (7, 5) greater than 4 appearing before it.

John, one of the students in the class, is studying for the final exams now. He found out that he has lost the assignment questions. He only has the answers (the ai's) but not the original permutation. Can you help him determine the original permutation, so he can review how to obtain the answers?
 

Input
The input consists of a number of test cases. Each test case starts with a line containing the integer n (n <= 500). The next n lines give the values of a1, ..., an. The input ends with n = 0.
 

Output
For each test case, print a line specifying the original permutation. Adjacent elements of a permutation should be separated by a comma. Note that some cases may require you to print lines containing more than 80 characters.
 

Sample Input
  
  
8 5 0 1 2 1 2 0 0 10 9 8 7 6 5 4 3 2 1 0 0
 

Sample Output
  
  
2,7,3,5,4,1,8,6 10,9,8,7,6,5,4,3,2,1
注 - 此题为 :  HDOJ 2404 Permutation Recovery

题意:     输入整数n,表示接下来要输入n个数(n为0时结束),输入n个数(1, 2, ..., n),例如 第一组示例: n 为 8 时; a1=5, a2=0, a3=1, a4=2, a5=1, a6=2, a7=0,                a8=0, 其中,a1=5表示在数字  1   前面有5个数比他大,a2=0,表示2前面有零个数比他大,a3=1表示3前面有1个数比他大....以此类推,求出符合此规律的数列;

思路:   已知某一排列序列的逆序列,如何恢复原来的排列序列, 先考虑当全部数组都为 0  时,如果1的逆序数为m,那么最小值 1显然 会在第m+1个0的位置上,1 位置固定,不再对后面的数产生影响,顺序处理1,2.......k,(递增序) 假设此时k的逆序数为m'显然 1,2,,,,k-1都无法对k的逆序数产生影响,则1,2,,,,k-1占据 的位置可以直接忽视,只统计那些可以对逆序数产生影响的 位置(即当前依然为 0 的位置),从左往右遍历数组,如果 遇到 0 位置,则统计 0  位置数量,

已AC代码:

#include<cstdio>
#include<cstring>
int s[600],a[600];
int main()
{
	int i,j,t,n;
	while(scanf("%d",&n),n)
	{
		for(i=1;i<=n;++i)
			scanf("%d",&s[i]);
		memset(a,0,sizeof(a)); // 全部数组都为 0  
		for(i=1;i<=n;++i) // 顺序处理1,2.......k,(递增序) 
		{
			t=0;
			for(j=1;j<=n;++j) // 从左往右遍历数组,如果 遇到 0 位置,则统计空位置数量 t, 
			{
				if(t==s[i])
					break;
				if(a[j]==0)
					t++;
			}
			for(;j<=n;++j) // i在第 s[i]+1个 0 的位置上
				if(a[j]==0)
					break;
			a[j]=i;
		}
		printf("%d",a[1]);
		for(i=2;i<=n;++i)
			printf(",%d",a[i]);
		printf("\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值