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. bzoj1831: [AHOI2008]逆序对(DP+双精bzoj1786)

    1831: [AHOI2008]逆序对 Description 小可可和小卡卡想到Y岛上旅游,但是他们不知道Y岛有多远.好在,他们找到一本古老的书,上面是这样说的: 下面是N个正整数,每个都在1~K之 ...

  2. spring boot 的常用注解使用 总结

    附:Spring Boot 官方文档学习(一)入门及使用见https://www.cnblogs.com/larryzeal/p/5799195.html @RestController和@Reque ...

  3. java动态导出PDF(利用itext)

    项目基于ssm框架,使用itext动态导出pdf文件: 1.引入两个jar包:itextpdf-5.5.5.jar.itext-asian-5.2.0.jar 说明: 1.itextpdf-5.5.5 ...

  4. 08:Challenge 1

    总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  262144kB 描述 给一个长为N的数列,有M次操作,每次操作是以下两种之一: (1)修改数列中的一个数 (2)求 ...

  5. 【基础篇】Android中获取Drawable的方法

    public static Drawable getDrawable(Context context,String filename) { BitmapDrawable drawable=null; ...

  6. ListView有Header时的position情况

     问题: headerView 为第0个view,item 的 pos会从1开始. 解决方式: position减去 listView.getHeaderViewsCount().例如我想得到list ...

  7. vmware workstation虚拟机克隆后不能上网(桥接模式下)

    (CentOS6.8下) 重启新克隆的虚拟机,输入用户名密码,进入系统. 1.修改网卡配置,输入 vi /etc/sysconfig/network-scripts/ifcfg-eth0 出现类似如下 ...

  8. gym 100971 J Robots at Warehouse

    Vitaly works at the warehouse. The warehouse can be represented as a grid of n × m cells, each of wh ...

  9. 日常基础—————echo,print,print_r,var_dump的区别

    1.echo   函数输出一个或多个字符串. 代码: header("Content-Type:text/html; charset=utf-8"); echo "你好” ...

  10. phpstorm10安装并汉化

    一.下载phpstorm 下载地址:https://pan.baidu.com/s/1R64ZROVP1ljGbYfCwWjwxA 二.一直点击下一步安装即可 注意:第3步的时候选择一下支持的后缀 三 ...