1555: Inversion Sequence (通过逆序数复原序列 vector的骚操作!!!)

 

1555: Inversion Sequence

      Time Limit: 2 Sec     Memory Limit: 256 Mb     Submitted: 519     Solved: 195    


Description

For sequence i1, i2, i3, … , iN, we set aj to be the number of members in the sequence which are prior to j and greater to j at the same time. The sequence a1, a2, a3, … , aN is referred to as the inversion sequence of the original sequence (i1, i2, i3, … , iN). For example, sequence 1, 2, 0, 1, 0 is the inversion sequence of sequence 3, 1, 5, 2, 4. Your task is to find a full permutation of 1~N that is an original sequence of a given inversion sequence. If there is no permutation meets the conditions please output “No solution”.

 

Input

There are several test cases.
Each test case contains 1 positive integers N in the first line.(1 ≤ N ≤ 10000).
Followed in the next line is an inversion sequence a1, a2, a3, … , aN (0 ≤ aj < N)
The input will finish with the end of file.

 

Output

For each case, please output the permutation of 1~N in one line. If there is no permutation meets the conditions, please output “No solution”.

 

Sample Input

5
1 2 0 1 0
3
0 0 0
2
1 1

Sample Output

3 1 5 2 4
1 2 3
No solution

Source

题目意思:通过逆序数复原序列
题目意思和样例很难懂
比如样例1:
1 2 0 1 0
1 表示序列中1的前面比1大的数有1个
2 表示序列中2的前面比2大的数有2个
0 表示序列中3的前面比3大的数有0个
1 表示序列中4的前面比4大的数有1个
0 表示序列中5的前面比5大的数有0个
解析一下:
1的前面有1个比1大的,2的前面有2个比2大的,4的前面有一个比4大的
首先,1的位置可以直接确定,因为除了1以外所有的数都比1大,所以1肯定在第二个位置
得到 _ 1 _ _ _
然后考虑2,2的前面有两个数字比2大,因为我们已经确定的数字只有1,1已经比2小了,所以要无视1,其实就是说,2的前面要留下2个空位,这样那2个空位只能放3,4,5,就能保证2的前面一定会有2个数字比它大了,所以
得到 _ 1 _ 2 _
再考虑3,3的前面没有比3大的,1和2都比3小应该直接无视,那么3只能放在第一个位置
得到3 1 _ 2 _
再考虑4,4的前面有一个比它大,所以4的前面应该留一个空格
得到3 1 _ 2 4
最后
得到3 1 5 2 4
具体做法:
采用vector模拟该操作
很神奇!......
 
复制代码
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define max_v 10005
using namespace std;
int a[max_v];
vector<int> v;
int main()
{
    int n;
    while(cin>>n)
    {
        v.clear();
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        int flag=1;
        for(int i=n;i>=1;i--)
        {
            if(v.size()<a[i])
            {
                flag=0;
                break;
            }
            v.insert(v.begin()+a[i],i);
        }
        if(flag)
        {
            vector<int>::iterator it;
            it=v.begin();
            printf("%d",*it);
            it++;
            for(;it!=v.end();it++)
                printf(" %d",*it);
            printf("\n");
        }else
        {
            printf("No solution\n");
        }
    }
    return 0;
}
/*
题目意思:通过逆序数复原序列
题目意思和样例很难懂
比如样例1:
1 2 0 1 0
1 表示序列中1的前面比1大的数有1个
2 表示序列中2的前面比2大的数有2个
0 表示序列中3的前面比3大的数有0个
1 表示序列中4的前面比4大的数有1个
0 表示序列中5的前面比5大的数有0个

解析一下:

1的前面有1个比1大的,2的前面有2个比2大的,4的前面有一个比4大的 

首先,1的位置可以直接确定,因为除了1以外所有的数都比1大,所以1肯定在第二个位置
得到 _ 1 _ _ _
然后考虑2,2的前面有两个数字比2大,因为我们已经确定的数字只有1,1已经比2小了,所以要无视1,其实就是说,2的前面要留下2个空位,这样那2个空位只能放3,4,5,就能保证2的前面一定会有2个数字比它大了,所以
得到 _ 1 _ 2 _
再考虑3,3的前面没有比3大的,1和2都比3小应该直接无视,那么3只能放在第一个位置
得到3 1 _ 2 _
再考虑4,4的前面有一个比它大,所以4的前面应该留一个空格
得到3 1 _ 2 4
最后
得到3 1 5 2 4

具体做法:
采用vector模拟该操作
很神奇!......

*/
复制代码

 

 

posted @   西*风  阅读(622)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示