1555: Inversion Sequence

Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: 107  Solved: 34

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

 

Source

解题:这个题目啊。。难读啊!

还是看例子吧

1 2 0 1 0

这个表示在1-N的排列中,存在这种排列,数字1前面只有1个数比他大,数字2前面只有2个比他大,数字3前面只有0个比他大,数字4前面只有1个比他大,数字5前面0个比他大。

所以答案 3 1 5 2 4
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
vector<int>v;
int d[maxn],n;
int main(){
while(~scanf("%d",&n)){
for(int i = ; i <= n; ++i)
scanf("%d",d+i);
v.clear();
bool flag = true;
for(int i = n; i > ; --i){
if(v.size() < d[i]){
flag = false;
break;
}
v.insert(v.begin()+d[i],i);
}
if(flag){
flag = false;
for(int i = ; i < v.size(); ++i){
if(flag) putchar(' ');
printf("%d",v[i]);
flag = true;
}
putchar('\n');
}else puts("No solution");
}
return ;
}

线段树找空位置插入。。

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct node {
int lt,rt,pos;
} tree[maxn<<];
int d[maxn],ans[maxn],n;
bool flag;
void build(int lt,int rt,int v) {
tree[v].lt = lt;
tree[v].rt = rt;
if(lt == rt) {
tree[v].pos = ;
return;
}
int mid = (lt + rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
tree[v].pos = tree[v<<].pos + tree[v<<|].pos;
}
void update(int s,int v,int value) {
if(tree[v].lt == tree[v].rt) {
tree[v].pos = ;
ans[tree[v].lt] = value;
return;
}
if(tree[v<<].pos >= s) update(s,v<<,value);
else if(tree[v<<|].pos >= s - tree[v<<].pos) update(s-tree[v<<].pos,v<<|,value);
else flag = false;
tree[v].pos = tree[v<<].pos + tree[v<<|].pos;
}
int main() {
while(~scanf("%d",&n)) {
build(,n,);
flag = true;
for(int i = ; i <= n; ++i) {
scanf("%d",d+i);
if(flag) update(d[i]+,,i);
}
if(flag) for(int i = ; i <= n; ++i)
printf("%d%c",ans[i],i == n?'\n':' ');
else puts("No solution");
}
return ;
}

CSUOJ 1555 Inversion Sequence的更多相关文章

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

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

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

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

  3. Inversion Sequence(csu 1555)

    Description For sequence i1, i2, i3, … , iN, we set aj to be the number of members in the sequence w ...

  4. CSUOJ 1271 Brackets Sequence 括号匹配

    Description ]. Output For each test case, print how many places there are, into which you insert a ' ...

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

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

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

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

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

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

  8. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  9. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

随机推荐

  1. Linux 6.3下安装Oracle Enterprise Cloud Control 12c

    Oracle enterprise cloud control 12c的安装是一个比較复杂的过程,由于他须要依赖于Oracel database以及Oracle Weblogic. 如今Oracle已 ...

  2. &lt;九度 OJ&gt;题目1545:奇怪的连通图

    题目描写叙述: 已知一个无向带权图,求最小整数k.使仅使用权值小于等于k的边,节点1能够与节点n连通. 输入: 输入包括多组測试用例.每组測试用例的开头为一个整数n(1 <= n <= 1 ...

  3. Android学习笔记(9):使用XML文件和Java代码控制UI界面

    Android推荐使用XML文件设置UI界面.然后用Java代码控制逻辑部分,这体现了MVC思想. MVC全名是Model View Controller.是模型(model)-视图(view)-控制 ...

  4. Linux线程相互排斥量--进程共享属性

    多线程中.在相互排斥量和 读写锁的 属性中.都有一个叫 进程共享属性 . 对于相互排斥量,查询和设置这个属性的方法为: pthread_mutexattr_getpshared pthread_mut ...

  5. Java排序之直接选择排序

    public class SelectSort { public static void selectSort(int [] a){ int min; int temp; if(a==null || ...

  6. Struts2标签库整理【完整】

    转自:https://blog.csdn.net/chen_zw/article/details/8161230   Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,str ...

  7. Copying

    Aliasing can make program difficult to read because changes made in one place might have unexpected ...

  8. 34.share_ptr智能指针共享内存,引用计数

    #include <iostream> #include <memory> #include <string> #include <vector> us ...

  9. Failed to start metasploit.service: Unit metasploit.service not found的解释

    不多说,直接上干货! root@kali:~# service metasploit start Failed to start metasploit.service: Unit metasploit ...

  10. monitoring_db

    #!/bin/bash# Program: # Automatic inspection operation system and oracle database.# History:# 2016/0 ...