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 0a[1]=4,即在b中,1的前面有四个数比1大,a[2]=3 ,即2的前面有3个数比2大...a[4]=0,即在4的前面有0个数比4大则b[]:4 3 5 2 1
// time  mem
// 264ms 2032kb
//by Orcz
// 2015/4/13/*题目意思原来自己一直都没懂
在给出的a[]序列中,a[i]是在所求的b[]序列中在i前面比i大的数的个数
eg:54 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值,所以是正确的

#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();
}

转载于:https://www.cnblogs.com/orchidzjl/p/4423064.html

Inversion Sequence(csu 1555)相关推荐

  1. Contest2071 - 湖南多校对抗赛(2015.03.28)

    Contest2071 - 湖南多校对抗赛(2015.03.28) 本次比赛试题由湖南大学ACM校队原创 http://acm.csu.edu.cn/OnlineJudge/contest.php?c ...

  2. 湖南多校对抗赛(2015.03.28)

    Contest2071 - 湖南多校对抗赛(2015.03.28) Contest2071 - 湖南多校对抗赛(2015.03.28) 本次比赛试题由湖南大学ACM校队原创 http://acm.cs ...

  3. 全国省市县sql(完整版)

     省 Insert into PROVINCE(PROVINCE_ID, NAME, SEQ)Values(4, '山西', 4); Insert into PROVINCE(PROVINCE_I ...

  4. 水题(1)直接计算答案

    目录 CSU 1111 三家人 CSU 1018 Avatar CSU 1039 三个数 CSU 1190 Staginner's Paper CSU 1191 Staginner Is Smarte ...

  5. CSU 1203 Super-increasing sequence

    CSU 1203 Super-increasing sequence Time Limit: 1 Sec Memory Limit: 128 MB Submit: 592 Solved: 243 De ...

  6. Inversion of Control Containers and the Dependency Injection pattern--Martin Fowler

    原文地址:https://martinfowler.com/articles/injection.html n the Java community there's been a rush of li ...

  7. Minimum Inversion Number HDU - 1394(权值线段树/树状数组)

    The inversion number of a given number sequence a1, a2, -, an is the number of pairs (ai, aj) that s ...

  8. Minimum Inversion Number HDU - 1394(求一个数字环的逆序对+多种解法)

    题意: 给出n个数(0~n-1,每个数仅出现一次),问它长为n的循环序列中逆序对最少的数量. 多种解法:暴力+树状数组+分治+规律推导公式 题目: The inversion number of a ...

  9. Minimum Inversion Number 线段树

    The inversion number of a given number sequence a1, a2, -, an is the number of pairs (ai, aj) that s ...

最新文章

  1. C++程序安装卸载WDM驱动
  2. Java基础05 实施接口
  3. EChart中使用地图方式总结(转载)
  4. logging模块介绍
  5. 人间真实:程序员的 60 个崩溃瞬间!
  6. win10如何下载python3_Win10环境中如何实现python2和python3并存
  7. 关于 Swift 单例的例子
  8. iOS 疑难杂症 — — 推送本地国际化 loc-key 本地化失败的问题
  9. Elasticsearch 备份数据到 AWS S3
  10. [网络安全自学篇] 二十一.GeekPwn 2019极客大赛之安全攻防技术总结及ShowTime
  11. appscan如何扫描移动应用APP
  12. Office文档忘保存了?办公必学技能:快速找回未保存的文档
  13. Docker:镜像加速器
  14. Adobe Photoshop CC 2017图文安装教程,附下载地址
  15. 用户特殊权限SUID,SGID, SBIT理解学习
  16. MFC软件欢迎界面(基于对话框,VS2013)
  17. NVIDIA GPU常用命令及设置汇总
  18. 89.破碎的玻璃横幅
  19. 入手评测 i7 13700和13700K的区别 i713700和i713700K差距
  20. 机器学习-特征归一化

热门文章

  1. RabbitMQ学习总结(7)——Spring整合RabbitMQ实例
  2. nagios监控远程端口
  3. NumPy Essentials 带注释源码 三、NumPy 数组使用
  4. Python开发笔记之正则表达式的使用
  5. 性能测试之开源的性能监控软件
  6. session.invalidate()
  7. 【实践】WCF 传输安全 1 前期准备之证书制作
  8. BCH的压力测试其实已经开始了
  9. es6之扩展运算符...
  10. Linux修改mysql的密码