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
// 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值,所以是正确的

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

Inversion Sequence(csu 1555)的更多相关文章

  1. STL or 线段树 --- CSU 1555: Inversion Sequence

    Inversion Sequence Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1555 Mean: 给你一 ...

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

    1555: Inversion Sequence Submit Page    Summary    Time Limit: 2 Sec     Memory Limit: 256 Mb     Su ...

  3. CSUOJ 1555 Inversion Sequence

    1555: Inversion Sequence Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 107  Solved: 34 Description ...

  4. csu 1555(线段树经典插队模型-根据逆序数还原序列)

    1555: Inversion Sequence Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 469  Solved: 167[Submit][Sta ...

  5. ACM: 强化训练-Inversion Sequence-线段树 or STL·vector

    Inversion Sequence Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%lld & %llu D ...

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

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

  7. CSU 1060 Nearest Sequence

    题意:求三个序列的最长公共子序列. 思路:一开始以为只要求出前两个的LCS,然后和第三个再求一遍LCS就是答案了.但是样例就对我进行啪啪啪打脸了.实际上就跟两个序列的差不多,换成三维的就行了. 代码: ...

  8. CSU 1515 Sequence (莫队算法)

    题意:给n个数,m个询问.每个询问是一个区间,求区间内差的绝对值为1的数对数. 题解:先离散化,然后莫队算法.莫队是离线算法,先按按询问左端点排序,在按右端点排序. ps:第一次写莫队,表示挺简单的, ...

  9. CSU 1515 Sequence

    莫队算法+map #include<cstdio> #include<cstring> #include<cmath> #include<map> #i ...

随机推荐

  1. JS 保留两位小数问题收集

    1.使用四舍五入的方法,保留小数点后的两位小数: toFixed里面的参数表示保留的小数的位数,范围是0-20,超过20位就会报错了 <script> var num=22.127456; ...

  2. October 6th 2016 Week 41st Thursday

    The outer world you see is a reflection of your inner self. 你拥有什么样的内心,你就会看到什么样的世界. And we eventually ...

  3. UIColor+Hex

    #import <UIKit/UIKit.h> @interface UIColor (Hex) + (UIColor *)colorWithHex:(long)hexColor;+ (U ...

  4. 《Thinking in Java》十七章_容器深入研究_练习13(Page484)

    练习13: 单词计数器 import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFou ...

  5. Mac系统下使用VirtualBox虚拟机安装win7--第二步 创建win7系统

    第二步 创建win7系统   启动 Virtual Box 以后,点击窗口左上角的“新建”按钮,如图所示

  6. LR结果分析——TPS和吞吐率

    针对吞吐率和TPS的关系,这个在结果分析中如何使用,就个人经验和朋友讨论后,提出如下建议指导,欢迎同僚指正. TPS:transaction per second 服务器每秒处理的事务数. 吞吐率:测 ...

  7. 什么是DMI,SMBIOS,符合SMBIOS规范的计算机的系统信息获取方法

    转自:http://www.cnblogs.com/gunl/archive/2011/08/08/2130719.html DMI是英文单词Desktop Management Interface的 ...

  8. 【Java EE 学习 20】【使用过滤器实现登陆验证、权限认证】【观察者模式和监听器(使用监听器实现统计在线IP、登录IP 、踢人功能)】

    一.使用过滤器实现登录验证.权限认证 1.创建5张表 /*使用过滤器实现权限过滤功能*/ /**创建数据库*/ DROP DATABASE day20; CREATE DATABASE day20; ...

  9. 【数据结构】建立和平衡AVL二叉树

    一步一步写平衡二叉树(AVL树) 原文地址:http://www.cppblog.com/cxiaojia/archive/2012/08/20/187776.html 我添加了一些内容,以充实整个算 ...

  10. JavaScript中判断对象类型方法大全1

    我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...