Inversion Sequence(csu 1555)
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
题目意思原来自己一直都没懂
在给出的a[]序列中,a[i]是在所求的b[]序列中在i前面比i大的数的个数
eg:5
4 3 1 0 0
a[1]=4,即在b中,1的前面有四个数比1大,a[2]=3 ,即2的前面有3个数比2大...a[4]=0,即在4的前面有0个数比4大
则b[]:4 3 5 2 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | // time mem // 264ms 2032kb //by Orcz // 2015/4/13 /*题目意思原来自己一直都没懂 在给出的a[]序列中,a[i]是在所求的b[]序列中在i前面比i大的数的个数 eg:5 4 3 1 0 0 a[1]=4,即在b中,1的前面有四个数比1大,a[2]=3 ,即2的前面有3个数比2大...a[4]=0,即在4的前面有0个数比4大 则b[]:4 3 5 2 1 */ #include<cstdio> #include<cstring> #include<iostream> using namespace std; int rcd[10005]; int ans[10005]; int n; struct node{ int l,r,len; }tree[4*10005]; void build( int v, int l, int r) { tree[v].l=l; tree[v].r=r; tree[v].len=l-r+1; if (l==r) return ; int mid=(l+r)>>1; build(v*2,l,mid+1); build(v*2+1,mid,r); } int query( int v, int k) { tree[v].len--; if (tree[v].l == tree[v].r) return tree[v].l; if (k <= tree[2*v].len) return query(2*v,k); else query(2*v + 1,k - tree[2*v].len); } void solve() { for ( int i = 1;i <= n; ++i) cin>>rcd[i]; build(1,n,1); int leap=0; int pos; for ( int i = 1;i <= n; ++i){ if (tree[1].len <= rcd[i]) {leap = 1; break ;} pos = query(1,rcd[i] + 1); ans[pos] = i; } if (leap) cout<< "No solution" <<endl; else { for ( int i = n;i > 1; --i) printf ( "%d " ,ans[i]); printf ( "%d\n" ,ans[1]);} } int main() { while (~ scanf ( "%d" ,&n)) solve(); } |
网上看了别人的题解,是用STL的
如例子 4 3 1 0 0 我们只要从后面开始,如a[5]=0,那么5之前就有0个数比5大,所以5的位置是 a[5]+1=1 已确定:5
a[4]=0, 那么4之前就有0个数比4大,所以4的位置是 a[4]+1=1 已确定:4 5
a[3]=1, 那么3之前就有1个数比3大,所以3的位置是 a[3]+1=2 已确定:4 3 5
a[2]=3, 那么2之前就有3个数比2大,所以2的位置是 a[2]+1=4 已确定:4 3 5 2
a[1]=4, 那么1之前就有4个数比1大,所以1的位置是 a[1]+1=5 已确定:4 3 5 2 1
因为是从后面开始插入的,即是从大到小插入i值,所以是正确的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include<cstdio> #include<cstring> #include<vector> #include<iostream> #include<algorithm> using namespace std; int rcd[10005]; int n; void solve() { vector< int > v; for ( int i = 0;i < n; ++i) cin>>rcd[i]; v.push_back(0); bool flag= true ; for ( int i = n-1 ;i >= 0; --i){ if (v.size() <= rcd[i]) { flag = false ; break ; } v.insert(v.begin() + rcd[i] + 1, i + 1); } if (flag) { for ( int i = 1 ;i < n ; ++i) cout<<v[i]<< " " ; cout<<v[n]<<endl; } else cout<< "No solution" <<endl; } int main() { while (~ scanf ( "%d" ,&n)) solve(); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧