1555: Inversion Sequence

Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: 469  Solved: 167
[Submit][Status][Web Board]

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

HINT

题意:知道1-n 所有数的逆序数,要求还原序列.例如 : 1 2 0 1 0 ,那么 1 前面有 1个数大于1,2前面有 2个数大于2,依次类推.

不会的可以去做一下poj 2828 ,经典模型了,在线段树更新的时候就能够完成所有操作了.对于某个数 i ,它前面的数数量为 k ,那么它就在线段树第 k+1 个空位上.假设当前的线段树能够插的空位数量不足 k+1 ,那么就无解了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define N 10005
int a[N],ans[N]; int tree[N<<];
void pushup(int idx){
tree[idx] = tree[idx<<]+tree[idx<<|];
}
void build(int l,int r,int idx){
if(l==r){
tree[idx] = ;
return;
}
int mid = (l+r)>>;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
pushup(idx);
}
void update(int value,int i,int l,int r,int idx){
if(l==r){
ans[l] = i;
tree[idx] = ;
return;
}
int mid = (l+r)>>;
if(tree[idx<<]>=value) update(value,i,l,mid,idx<<); ///插向左子树
else update(value-tree[idx<<],i,mid+,r,idx<<|);
pushup(idx);
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
memset(tree,,sizeof(tree));
memset(ans,,sizeof(ans));
build(,n,);
bool flag = false;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
a[i]++; ///它前面的空格数为a[i] ,自然它应该在第 a[i]+1 个空格位置
if(tree[]<a[i]) flag = true;
if(!flag)
update(a[i],i,,n,);
}
if(flag){
printf("No solution\n");
continue;
}
for(int i=;i<n;i++){
printf("%d ",ans[i]);
}
printf("%d\n",ans[n]);
}
return ;
}

csu 1555(线段树经典插队模型-根据逆序数还原序列)的更多相关文章

  1. hdu 1394 Minimum Inversion Number(线段树之 单点更新求逆序数)

    Minimum Inversion Number                                                                           T ...

  2. codeforces_459D_(线段树,离散化,求逆序数)

    链接:http://codeforces.com/problemset/problem/459/D D. Pashmak and Parmida's problem time limit per te ...

  3. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  4. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

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

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

  6. ZSTU 4241 圣杯战争(线段树+经典)

    题意:CS召唤了n个实验怪兽,第i号怪兽在i这个位置出.并把KI召唤出的第i位从者安排在pos(i)处,总共有m位从者. 第i只怪兽有战斗力atk(i), 而i号从者的体力为AP(i).如果从者想要移 ...

  7. POJ2828 Buy Tickets(线段树之插队问题)

    飞翔 问题是这样的:现在有n个人要买票,但是天黑可以随便插队.依次给出将要买票的n个人的数据信息.包含两项:pos,当前第i号人来了之后他肯定要插入到pos这个位置,如果当前pos无人,那最好了,直接 ...

  8. POJ2155 Matrix二维线段树经典题

    题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...

  9. 【2019雅礼集训】【可持久化线段树】【模型转化】D1T2Permutation

    目录 题意 输入格式 输出格式 思路 代码 题意 给定一个长度为n的序列A[],你需要确定一个长度为n的排列P[],定义当前排列的值为: \[\sum_{i=1}^{n}{A[i]P[i]}\] 现在 ...

随机推荐

  1. Java考试题

    1.     public class GC { 2.     private Object o; 3.     private voiddoSomethingElse(Object obj) { o ...

  2. 【极值问题】【CF33C】 Wonderful Randomized Sum

    传送门 Description 给你一个数列\(A\),你可以选择任意一个前缀和任意一个后缀,前缀后缀可重合.给他们乘\(-1\).求最大能获得的序列和. Input 第一行是一个数\(n\)代表数列 ...

  3. get与post请求简单理解

    一般在浏览器中输入网址访问资源都是通过GET方式:在FORM提交中,可以通过Method指定提交方式为GET或者POST,默认为GET提交 Http定义了与服务器交互的不同方法,最基本的方法有4种,分 ...

  4. 「Django」rest_framework学习系列-版本认证

    1.自己写: class UserView(APIView): versioning_class = ParamVersion def get(self,request,*args,**kwargs) ...

  5. linux中使用随机数

    (1)单纯使用rand重复调用n次,就会得到一个0-RAND_MAX之间的伪随机数,如果需要调整范围,可以得到随机数序列后再进行计算.(2)单纯使用rand来得到伪随机数序列有缺陷,每次执行程序得到的 ...

  6. 2017北京国庆刷题Day7 morning

    期望得分:100+0+100=200 实际得分:100+20+0=120 离散化搞搞 #include<cstdio> #include<iostream> #include& ...

  7. vector的哈希值 Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C

    http://codeforces.com/contest/757/problem/C 题目大意:有n个导管,每个体育馆有k种神奇宝贝,然后所有的n个体育馆中,一共有m中神奇宝贝.可知,每个神奇宝贝中 ...

  8. 2015/9/29 Python基础(20):类的授权

    类的授权 1.包装包装在Python编程世界中时经常会被提到的一个术语.它是一个通用的名字,意思是对一个已存在的对象进行包装,不管它是数据类型,还是一段代码,可以是对一个已存在的对象,增加新的,删除不 ...

  9. CSS 定位相关属性 :position

    我们平时经常用margin来进行布局,但是遇到一些盒子不规律布局时,用margin就有点麻烦了,这个时候我们可以用position. position:参数 参数分析: 一.absolute: 相对父 ...

  10. errno错误号含义

    errno0 : Success errno1 : Operation not permitted errno2 : No such file or directory errno3 : No suc ...