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. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

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

  2. 阿里云ECS环境部署 centos 6.5

    阿里云ESC服务器1 先挂载磁盘 参考:http://help.aliyun.com/view/11108189_13491193.html?spm=5176.2020520101.121.2.1wc ...

  3. Web之CGIC的介绍与使用20171229

    一.CGIC简介 1.CGI简介 CGI(Common Gateway Interface)是外部应用扩展应用程序与WWW服务器交互的一个标准接口.按照CGI标准编写的外部扩展应用程序可以处理客户端浏 ...

  4. phpstorm 自定义属性没语法提示的问题

    在右侧Options勾选上Custom HTML tag attributes,并且在下面的输入框输入自定义属性列表,逗号分隔.

  5. bzoj 1510 [POI2006]Kra-The Disks 二分

    1510: [POI2006]Kra-The Disks Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 466  Solved: 272[Submit][ ...

  6. Kruskal-Wallis test

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  7. (转)select、poll、epoll之间的区别

    本文来自:https://www.cnblogs.com/aspirant/p/9166944.html (1)select==>时间复杂度O(n) 它仅仅知道了,有I/O事件发生了,却并不知道 ...

  8. 远程调试openstack

    之前一直没有找到方法调试openstack的horizon代码,现在终于找到方法了,特别感谢下面这篇博客,讲解非常清晰: http://blog.csdn.net/tantexian/article/ ...

  9. string的内存本质

    虽然没有研究过string的源代码,不过可以确定的是string的内存空间是在堆上开辟的,它自己负责释放空间,不用我们关系. 我们用一个动态分配的字符串指针初始化一个string对象retStr,它会 ...

  10. JAVA Thread Dumps 三部曲

    一.windows环境下方法 1:cmd下找到运行服务器容器的PID jps -v 例: C:\Users\Administrator>jps -v4856 Bootstrap -Djdk.tl ...