Permutation Recovery

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

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

题意:给你一个序列 如2,7,3,5,4,1,8,6  a1=5代表1前边比1大的数的个数为5 (2 7 3 5 4)a4=2代表4前边比4大的数的个数为2个(7  5)现在给你每个数前边比这个数大的数的个数让你求出这个序列

题解:因为所给的数都是按照1~n的顺序给的,所以我们可以看做有n个空格 可以根据这个顺序来向空格中放数,首先,当输入0时代表前边没有比它自己大的数则证明前边的空格都已放满,此时找到数组a的

第一个0的位置就是这个数的位置,如果输入的数k不为0则证明这个数前边还有k个比自己大的数,那么找到第k+1个0的位置,就是这个数的位置

#include<stdio.h>
#include<string.h>
#define MAX 510
int a[MAX];//输入数据
int main()
{
int n,m,j,i,k,t;
while(scanf("%d",&n),n)
{
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
{
scanf("%d",&k);
int ok=0;
for(j=1;j<=n;j++)
{
bool flag=false;
if(k==0)
{
for(t=1;t<=n;t++)
{
if(a[t]==0)
{
a[t]=i;
flag=true;
break;
}
}
if(flag)
break;
} else
{
if(a[j]==0)
ok++;
if(ok==k+1)
{
a[j]=i;
break;
}
}
}
}
for(i=1;i<n;i++)
printf("%d,",a[i]);
printf("%d",a[n]);
printf("\n");
}
return 0;
}

  

hdoj 2404 Permutation Recovery【逆序对】的更多相关文章

  1. CF785CAnton and Permutation(分块 动态逆序对)

    Anton likes permutations, especially he likes to permute their elements. Note that a permutation of  ...

  2. HDU 1394Minimum Inversion Number 数状数组 逆序对数量和

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  3. HDU-1394 Minimum Inversion Number 线段树+逆序对

    仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/O ...

  4. Gym 100463A Crossings 逆序对

    Crossings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description ...

  5. HDU 1394 树状数组求逆序对

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  6. UVA 11990 ``Dynamic'' Inversion 动态逆序对

    ``Dynamic'' Inversion Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/index ...

  7. Permutation Recovery(模拟)

    Permutation Recovery Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. hdu 1394 逆序对(nlgn+o(n) )

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...

  9. Gym 100463A Crossings (树状数组 逆序对)

    Crossings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description ...

随机推荐

  1. spring拦截器

    一:拦截器配置 <!-- 拦截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path=&qu ...

  2. Razor引擎总结

    1.显示格式化小数:@(string.Format("{0:0.00}",ViewData["TradeAmount"].ToNullString()))

  3. Swift开发之 ---- Swift宏定义

    swift中没有了#Define这种宏定义了,可以用let来声明常量来取代,判断当前系统版本 let IS_IOS7 = (UIDevice.currentDevice().systemVersion ...

  4. WebApp开发:ajax请求跨域问题的解决

    服务端:PHP 客户端:Andorid, HTML5, jQuery, ajax 现象:本想通过jQuery的ajax功能从服务器取回数据存到手机的缓存里,结果总是错误,后来想到可能是跨域问题,所以查 ...

  5. ms-on-input

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. MAVEN:::::: maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supported

    zhuan:http://elan1986.iteye.com/blog/1537967 <!--add by wangquanjun 20140529-->            < ...

  7. Swifter初体验;按照惯例,来一个Swift版本的:iOS图片验证码?

    不多解释,上图,上代码:代码

  8. 子查询解嵌套not in 无法展开改写

    SQL> explain plan for select * from OPS$CZTEST1.SAVJ_ATOMJOURBAK where ((list_flag = '1' and prt_ ...

  9. Android textAppearance的属性设置及TextView属性详解

    textAppearance的属性设置 android:textAppearance="?android:attr/textAppearanceSmall" android:tex ...

  10. King(差分约束)

    http://poj.org/problem?id=1364 题意真心看不大懂啊... 现在假设有一个这样的序列,S={a1,a2,a3,a4...ai...at}其中ai=a*si,其实这句可以忽略 ...