zoukankan      html  css  js  c++  java
  • 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 ≤ in), 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.

    样例输入

    样例输出

     

    题意:就是给你每个数的逆序数,让你求这个序列。比如说对于序列: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[1234],n;
    void solve(){
        for(int i = 0 ; i < n ; i ++){
            scanf("%d",&a[i]);
        }
        vector<int>v;
        v.push_back(n);
        for(int i = n-1;i >= 1; i--){
            v.insert(v.begin()+a[i-1],i);
        }
        for(int j = 0 ; j < v.size() ; j++){
            j == 0 ? printf("%d",v[j]):printf(",%d",v[j]);
        }
        puts("");
    }
    int main()
    {
        while(scanf("%d",&n),n){
            solve();
        }
        return 0;
    }
  • 相关阅读:
    Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
    Oracle Instant Client 安装配置
    Android Studio配置文件路径修改
    Java压缩技术(三) ZIP解压缩——Java原生实现
    Java压缩技术(二) ZIP压缩——Java原生实现
    Java压缩技术(一) ZLib
    [JavaEE] JBoss主要版本下载链接一览
    EL表达式
    如何实现hibernate的缓存机制
    如何理解hibernate当中的锁机制
  • 原文地址:https://www.cnblogs.com/Esquecer/p/8513749.html
Copyright © 2011-2022 走看看