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. 国内外三个领域巨头告诉你Redis怎么用

    随着数据体积的激增,MySQL+memcache已经满足不了大型互联网类应用的需求,许多机构也纷纷选择Redis作为其架构上的补充.这里我们将为大家分享社交巨头新浪微博.传媒巨头Viacom及图片分享 ...

  2. 解题:POI 2012 Cloakroom

    题面 首先,单独处理每个询问复杂度显然不可承受,还是考虑通过排序使得限制更容易达到:按照$a$将物品排序,按照$m$将询问排序,这样肯定是要不断添加物品才能达到要求,顺着做一遍就行了 然后发现$b$的 ...

  3. mysql三-6:完整性约束

    一 介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性 主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KE ...

  4. oracle-DECODE()函数

    DECODE()函数 DECODE(value, if1, then1, if2,then2, if3,then3, . . . else ) 含义解释: DECODE(条件,值1,翻译值1,值2,翻 ...

  5. Struts初探(二)

    总是找不到对应的action,但别的没用到动态方法调用的都没有问题. 报异常:java.lang.reflect.InvocationTargetException - Class: com.open ...

  6. HDU--2363

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2363 分析:最短路+二分. #include<iostream> #include< ...

  7. C++模式设计-多线程下的单例模式

    1 教科书里的单例模式 我们都很清楚一个简单的单例模式该怎样去实现:构造函数声明为private或protect防止被外部函数实例化,内部保存一个private static的类指针保存唯一的实例,实 ...

  8. 题解 P2762 【太空飞行计划问题】

    P2762 太空飞行计划问题 题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,-,Em},和进 ...

  9. 重构改善既有代码设计--重构手法07:Remove Assignments to Parameters (移除对参数的赋值)

    代码对一个 参数赋值.以一个临时变量取代该参数的位置.     int Discount(int inputVal, int quantity, int yearTodate) { if (input ...

  10. 在JavaScript中重写jQuery对象的方法

    jQuery是一个很好的类库,它给我们解决了很多的客户端编程,任何东西都不是万能的,当它不能满足我们的需求时我们需要对它进行重写,同时也不要影响其原有的功能或者修改其原有的功能:我现在的web应用程序 ...