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. jquery上传插件uploadify 报错http error 302 解决方法之一

    前段时间用到jquery上传插件uploadify时,始终出现系统报出 http error 302 的错误. 网上大量搜集信息,基本上都是说session值丢失的问题,根据网友提供的解决方案进行修改 ...

  2. juery mobile select下来菜单选项提交form问题

    注意: data-native-menu="false"  虽然具有渲染作用,但是无法进行js提交. <script type="text/javascript&q ...

  3. 为什么很多语言选择在JVM上实现

    非常经济地实现跨平台.你的语言编译器后端只需要输出 JVM 字节码就可以.跨平台需要极大的工作量,举个例子,只是独立开发生成本地代码,就需要花费大量精力去针对不同平台和处理器进行优化(比如 Firef ...

  4. 通过本地加载ga.js文件提高Google Anlytics性能

    Google Anlytics 分析代码是异步加载的,一般来讲不会影响网页性能,但是技术部的网页性能报告里老是提到ga.js的状态为Aborted,说明ga虽然是异步跟踪,但某些情况下对网页性能与加载 ...

  5. hdu 3072

    强连通分量——tarjin 算法 这道题和前面那道hdu 2767唯一不同就是,2767需要找出最小数量的边使图成为连通分量,而这个题需要一点点贪心的思想在里面,它需要求出代价最小的边使图成为连通分量 ...

  6. 关于entity framework

    http://www.cnblogs.com/lsxqw2004/archive/2009/05/31/1495240.html http://www.open-open.com/lib/view/o ...

  7. mysql注入攻击及防范

    一.注入攻击种类     1. GET注入         输入参数通过URL发送.     2. POST注入         输入参数通过HTTP正文发送     3. COOKIE注入      ...

  8. Cxf + Spring3.0 入门开发WebService

    转自原文地址:http://sunny.blog.51cto.com/182601/625540/ 由于公司业务需求, 需要使用WebService技术对外提供服务,以前没有做过类似的项目,在网上搜寻 ...

  9. 【Xamarin开发 Android 系列 3】循序渐进的学习顺序

    原文:[Xamarin开发 Android 系列 3]循序渐进的学习顺序 指定合理的学习步骤,将各个技术点进行强化.慢慢 的就从点到线 到面的飞跃,一切仅仅是时间问题,开始前,请记住,学习是最佳的投资 ...

  10. Android如何在一个线性布局里完美显示两个listview啊?

    复写一个listView ,在你布局文件中使用此view: <ScrollView android:layout_width="fill_parent" android:la ...