TOJ 2130: Permutation Recovery(思维+vector的使用)
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=2130
时间限制(普通/Java):2000MS/20000MS 内存限制:65536KByte
描述
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 appear 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?
输入
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, a2, ... , an. The input ends with n = 0.
输出
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.
样例输入
8
5
0
1
2
1
2
0
0
10
9
8
7
6
5
4
3
2
1
0
0
样例输出
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 。在出现1之前,有5个比1大的数:2,7,3,5,4。所以a[1] = 5,又比如 6,在6之前有2个比它大的,即:7,8。所以a[6] = 2
题目就是给了a[1]到a[n],分别代表1---n的逆序数,要求求出这个序列
思路:从后往前来。拿题目中的样例1来解释:序列:2,7,3,5,4,1,8,6
a[8] 只能等于0 因为 8 是最大的那个数
a[7] 只能等于0或者 1,因为7是第二大的,a[7]等于0说明7在8的左边,等于1说明7在8的右边
a[6] 只能等于0或者1或者2,像题目中的,等于2的话,说明6在 {7,8} 这个序列的右边
以此类推即可把整个序列求出来
注意输出0结束,以及分割用逗号
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define LL long long
using namespace std;
int a[],n;
void solve(){
for(int i = ; i < n ; i ++){
scanf("%d",&a[i]);
}
vector<int>v;
v.push_back(n);
for(int i = n-;i >= ; i--){
v.insert(v.begin()+a[i-],i);
}
for(int j = ; j < v.size() ; j++){
j == ? printf("%d",v[j]):printf(",%d",v[j]);
}
puts("");
}
int main()
{
while(scanf("%d",&n),n){
solve();
}
return ;
}
TOJ 2130: Permutation Recovery(思维+vector的使用)的更多相关文章
- hdoj 2404 Permutation Recovery【逆序对】
Permutation Recovery Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- Permutation Recovery(模拟)
Permutation Recovery Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- Permutation(构造+思维)
A permutation p is an ordered group of numbers p1, p2, ..., pn, consisting of ndistinct positi ...
- D. Make a Permutation!(思维)
D. Make a Permutation! time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 网络寻路(思维+vector的应用)-----------蓝桥备战系列
标题:网络寻路 X 国的一个网络使用若干条线路连接若干个节点.节点间的通信是双向的.某重要数据包,为了安全起见,必须恰好被转发两次到达目的地.该包可能在任意一个节点产生,我们需要知道该网络中一共有多少 ...
- Codeforces 1159E Permutation recovery(构造+拓扑)
这道题其实只要解决了什么时候输出 -1 ,那么此题的构造方法也就解决了.首先我们可以观察这组 3 3 4 和 3 4 4 ,可以算出第二组是不成立的,在观察一组 2 3 4 5 和 3 2 4 5 ...
- Codeforces 1158C Permutation recovery
https://codeforces.com/contest/1158/problem/C 题目 已知 $p_1, p_2, \dots, p_n$ 是 $1$ 到 $n$ 的一个排列. 给出关于这个 ...
- [Locked] Palindrome Permutation I & II
Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...
- 【LeetCode】60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
随机推荐
- AspxGridView服务器事件列表
2.AutoFilterCellEditorCreate (1)语法:publicdelegate void ASPxGridViewEditorCreateEventHandler(object s ...
- jquery 全选操作
$(function(){ $("#checkedAll").change(function(){ if(this.checked){ $(".checkSingle&q ...
- 机器学习进阶-图像基本处理-视频的读取与处理 1.cv2.VideoCapture(视频的载入) 2.vc.isOpened(载入的视频是否可以打开) 3.vc.read(视频中一张图片的读取) 4.cv2.cvtColor(将图片转换为灰度图)
1.vc = cv2.VideoCapture('test.mp4') #进行视频的载入 2.vc.isOpened() # 判断载入的视频是否可以打开 3.ret, frame = vc.read( ...
- MySQL数据库备份工具mysqldump的使用(转)
说明:MySQL中InnoDB和MyISAM类型数据库,这个工具最新版本好像都已经支持了,以前可能存在于MyISAM的只能只用冷备份方式的说法. 备份指定库: mysqldump -h127.0.0. ...
- URL分发(URLConf)
如果项目中应用太多,都写到顶层的urls.py中,如果个别应用url出问题的话,其他的应用也会受影响,所以我们需要对每个应用下面都写一个urls.py,实现分发 顶层urls.py中写:(属于blog ...
- 算法练习,链表二分最大n个
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class Bin ...
- C++复习:异常
异常处理机制专题 前言 1)异常是一种程序控制机制,与函数机制独立和互补 函数是一种以栈结构展开的上下函数衔接的程序控制系统,异常是另一种控制结构,它依附于栈结构,却可以同时设置多个异常类型作 ...
- 在WINDOWS上开发IOS应用的方法
1,虚拟机上安装MAC系统,里面装XCODE 2,WINDOWS上安装code blocks,这个IDE可以直接在WIN上编译XCODE工程: 最新的IOS在windows环境下编译环境搭建记录
- Linux date命令使用方法
date命令用来显示或设定以及计算系统的日期和时间.本文主要描述date命令的基本语法和工作中常用的使用方法. date命令语法 用法:date [OPTION]... [+FORMAT] 常用参数: ...
- PL/SQL编码规范的一些建议
由于业务的复杂多变,我们编写完的程序,在后期肯定要被修改,而且修改的人很可能不是自己.这种情况我们都遇到过. 而且,看别人的代码可能会觉得很痛苦:为什么他要这样写相关逻辑?为什么变量名称要这样定义?换 ...