hdu 2404 Permutation Recovery
来源:互联网 发布:中国国籍 知乎 编辑:程序博客网 时间:2024/05/16 17:36
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?
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
8501212001098765432100
Sample Output
2,7,3,5,4,1,8,610,9,8,7,6,5,4,3,2,1已知某一排列序列的逆序列,如何恢复原来的排列序列,先考虑当全部数组都为空时,如果1的逆序数为m,那么1显然会在m+1位置上,顺序处理1,2.......k,(递增序),假设此时k的逆序数为m',那么k会出现在什么位置呢?显然1,2,,,,k-1都无法对k的逆序数产生影响,则1,2,,,,k-1占据的位置可以直接忽视,只统计那些可以对逆序数产生影响的位置(即当前依然为空的位置),从左往右遍历数组,如果遇到已被(1,2,,,,k-1)占据的数据时,直接跳过,如果遇到空位置,则统计空位置数量,直至扫描至某一位index,空数据位置数量刚好达到m',则k的位置就是index+1#include <iostream>#include <string>using namespace std;int reserve[1000];int per[1000];int n;void permulation(){int i, j;memset(per,0,sizeof(per));for ( i = 1; i <= n; ++i )//枚举每个数应该在哪个位置{for ( j = 1; j <= n; ++j ){if(per[j] == 0){if ( reserve[i] != 0 )//统计空白格子--reserve[i];elsebreak;}}per[j] = i;}}int main(){int i;while(cin >> n && n != 0){for(i=1; i <= n; ++i)cin >> reserve[i];permulation();cout << per[1];for(i = 2; i <= n; ++i)cout << ',' << per[i];cout << endl;}return 0;}
0 0
- hdu 2404 Permutation Recovery
- 2404 Permutation Recovery【模拟】
- hdoj 2404 Permutation Recovery【水水】
- hdoj 2404 Permutation Recovery 【水题(规律)】
- hdoj 2404 Permutation Recovery【基础题】
- HDOJ 2404 Permutation Recovery (逆序列)
- hdu2404:Permutation Recovery
- Permutation Recovery(排列恢复)
- Permutation Recovery 2404 (逆序列 规律 好题)
- HDOJ 2404 Permutation Recovery(好题,学习了)
- hdu 4345 Permutation
- Hdu 4345 Permutation
- HDU 2583 permutation
- HDU 4917 Permutation
- hdu-2583 permutation
- HDU 3664 Permutation Counting
- HDU 4345 Permutation dp
- HDU 2583 permutation
- Cocos2d-X研究之3.0 场景切换特效汇总
- MYSQL中'TYPE=MyISAM'错误的解决方案
- 查找AIX系统开启端口号对应服务程序
- IOS应用开发05——登录界面
- Linux文件误删除恢复操作
- hdu 2404 Permutation Recovery
- OC中的数据类型
- Windows平台视频显示问题
- 流媒体协议引见(rtp/rtcp/rtsp/rtmp/mms/hls)
- 有哪些可供Web设计师&开发者消遣的网站?
- 开源中国iOS客户端学习——(八)网络通信AFNetworking类库
- VIM搜索指令
- SMSLib实现Java短信收发的功能
- 哭了,写了一篇博文变成待审核了,哪位大大给审核啊