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

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

 

转载于:https://www.cnblogs.com/liyinggang/p/5793498.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的影城管理系统,源码+数据库+论文答辩+毕业论文+视频演示 随着现在网络的快速发展,网上管理系统也逐渐快速发展起来,网上管理模式很快融入到了许多生活之中,随之就产生了“小徐影城管理系统”,这样就让小徐影城管理系统更加方便简单。 对于本小徐影城管理系统的设计来说,系统开发主要是采用java语言技术,在整个系统的设计中应用MySQL数据库来完成数据存储,具体根据小徐影城管理系统的现状来进行开发的,具体根据现实的需求来实现小徐影城管理系统网络化的管理,各类信息有序地进行存储,进入小徐影城管理系统页面之后,方可开始操作主控界面,主要功能包括管理员:首页、个人中心、用户管理、电影类型管理、放映厅管理、电影信息管理、购票统计管理、系统管理、订单管理,用户前台;首页、电影信息、电影资讯、个人中心、后台管理、在线客服等功能。 本论文主要讲述了小徐影城管理系统开发背景,该系统它主要是对需求分析和功能需求做了介绍,并且对系统做了详细的测试和总结。具体从业务流程、数据库设计和系统结构等多方面的问题。望能利用先进的计算机技术和网络技术来改变目前的小徐影城管理系统状况,提高管理效率。 关键词:小徐影城管理系统;Spring Boot框架,MySQL数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值