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. linux 文件操作编程

    Linux中所有的设备和文件的操作都使用文件描述符来进行. 文件描述符是一个非负的整数,它是一个索引值,指向内核中每个进程打开的记录表. 当打开一个文件或者创建一个新文件时,内核就向进程返回一个文件描 ...

  2. .net ajax式上传文件

    今天在这里介绍一下ajax上传文件.其实也不算是真的使用xmlhttprequest上传,只是使用了iframe实现了无刷新上传而已,最多也只算 是仿ajax上传文件.然而网上关于使用xmlhttpr ...

  3. jquery放大镜插件与样式

    这是放大镜插件链接,我已经上传到我博客http://files.cnblogs.com/valiant1882331/%E6%94%BE%E5%A4%A7%E9%95%9C%E6%8F%92%E4%B ...

  4. (WinForm)FormBorderStyle属性

    此属性就是获取或设置窗体的边框样式,默认值为 FormBorderStyle.Sizable.共7个值. 属性 意义 None 无边框 FixedSingle 固定的单行边框 Fixed3D 固定的三 ...

  5. HTML5 File API

    1.File API 一直以来,不能直接访问用户计算机中的文件都是web应用开发当中的一大障碍.File API的宗旨是为web开发人员提供一种安全的方式,以便在客户端访问用户计算机中的文件,并更好的 ...

  6. whoosh使用笔记

    1. whoosh安装 pip install Whoosh 2. 添加索引 第一步:生成schema 第二步:根据schema生成index.index就是一个目录中的一堆文件  (针对某个目录,调 ...

  7. Python的简介以及安装和第一个程序以及用法

    Python的简介: 1.Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.自从20世纪90年代初Python语言诞生至今,它逐渐被广泛应用于处理系统管理任务和Web编程.Pytho ...

  8. 使用WMI监控进程启动与结束

    需要添加引用System.Management 代码: static void Main(string[] args) { //创建WQL事件查询,监视进程开启 var qCreate = new W ...

  9. Swift(三.函数)

    一.swift中的函数分为以下几类吧 1>无参无返   2>无参有返 3>有参无返  4>有参有返  5>有参多返 二.看下面几个例子吧 1>无参无返 func a ...

  10. C++ 11 笔记 (四) : std::bind

    std::bind 接受一个可调用的对象,一般就是函数呗.. 还是先上代码: void func(int x, int y, int z) { std::cout << "hel ...