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. .NET 框架简单介绍

    初学.NET肯定会有一系列的疑问,比方(下面为自己的疑问): 1) 何为. NET框架.它都包括哪些东西? 2) 程序集是什么.它是怎样在CLR(通用语言执行时)中执行的? 3) C#与VB.NET同 ...

  2. 用html语言写一个功课表

    今天在网上看了一个关于html的教程,主要是讲表格,看完之后认为有必要上机试试.于是就写了以下的一段代码. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb ...

  3. Session、Cookie总结

    什么是sessnion,session存在哪,能存多久.怎么设置他的存储时间 一.什么是session 1.session 被翻译为会话.当client(一般都是浏览器作为client)訪问serve ...

  4. 封装一个ViewPager真正的实现图片无限循环滚动带导航点

    效果图: 大家在写项目的过程中常常会碰到须要实现Viewpager里面载入几张图片来循环自己主动轮播的效果,假设不封装一下的话代码分散在activity里面会显得非常乱.并且也不利于我们下次复用,所以 ...

  5. codeforces#281 A

    脑子有点秀逗 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm ...

  6. Copying lists

    When you assign an object to a variable, Python copies the reference to the object. In this case a a ...

  7. pyton写购物车

    pyton写购物车 基本要求: 用户输入工资,然后打印购物菜单用户可以不断的购买商品,直到余额不够为止退出时打印用户已购买的商品和剩余金额.. 1.这个程序功能不完整,bug很多,练手之作. good ...

  8. RecyclerView让列表嵌套如此简单

    平常开发时,相信像这样的页面,大家一定是遇到过的.这里比较坑爹的地方在于呢:列表嵌套.订单列表中的每一项,都包含一个商品列表.像这种需求,大家会如何实现呢? 这里呢,说一下我自己的思路,我没有使用列表 ...

  9. DAG-背包九解-01背包

    饭卡:   电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够 ...

  10. 常见bug分析

    变量类型不匹配,形参和实参类型不匹配,隐式类型转换,变量类型赋值不匹配, 工具不熟悉,导致逻辑错误,查看代码,测试驱动开发,完整的测试用例,覆盖所有分支, 变量超出范围,对于大的数据要特别注意, 工具 ...