CSUOJ 1555 Inversion Sequence
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的更多相关文章
- 1555: Inversion Sequence (通过逆序数复原序列 vector的骚操作!!!)
1555: Inversion Sequence Submit Page Summary Time Limit: 2 Sec Memory Limit: 256 Mb Su ...
- STL or 线段树 --- CSU 1555: Inversion Sequence
Inversion Sequence Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1555 Mean: 给你一 ...
- Inversion Sequence(csu 1555)
Description For sequence i1, i2, i3, … , iN, we set aj to be the number of members in the sequence w ...
- CSUOJ 1271 Brackets Sequence 括号匹配
Description ]. Output For each test case, print how many places there are, into which you insert a ' ...
- csu 1555(线段树经典插队模型-根据逆序数还原序列)
1555: Inversion Sequence Time Limit: 2 Sec Memory Limit: 256 MBSubmit: 469 Solved: 167[Submit][Sta ...
- ACM: 强化训练-Inversion Sequence-线段树 or STL·vector
Inversion Sequence Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%lld & %llu D ...
- Contest2071 - 湖南多校对抗赛(2015.03.28)
Contest2071 - 湖南多校对抗赛(2015.03.28) 本次比赛试题由湖南大学ACM校队原创 http://acm.csu.edu.cn/OnlineJudge/contest.php?c ...
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description The inve ...
随机推荐
- MySQL 以及 Python 实现排名窗体函数
大部分数据库都提供了窗体函数.比方RANK,ROW_NUMBER等等. MySQL 这方面没有直接提供.可是能够变相的实现.我曾经写了row_number 的实现,今天有时间把 rank 的实现贴出来 ...
- poj_3371
一道模拟题,写的有点麻烦 #include<iostream> #include<cstring> #include<cstdio> #include<alg ...
- Sqlite3的安装Windows
- Mysql优化理论知识
参考文章 http://blog.51cto.com/lizhenliang/2095526 ()硬件优化 如果有条件一定要SSD固态硬盘代替SAS机械硬盘,将RAID级别调整为RAID1+,相对于R ...
- 在shell脚本中使用代理
设置所有的代理走socks5 export ALL_PROXY="socks5://127.0.0.1:1080" 取消代理 unset ALL_PROXY
- BZOJ 3110 线段树套线段树
思路: 外围一个权值线段树 里面是个区间线段树 搞一个标记永久化 //By SiriusRen #include <cstdio> #include <cstring> #in ...
- POJ 3667 线段树+标记
自从某次考试写线段树写挂了以后 这是第一次写线段树,,,,,, 这是一个伤心的故事-- 题意: 思路: 标记 维护从左到右的最大值 从右到左的最大值 区间内的最大值-- 然后就一搞 就出来了 //By ...
- Where to Store your JWTs – Cookies vs HTML5 Web Storage--转
原文地址:https://stormpath.com/blog/where-to-store-your-jwts-cookies-vs-html5-web-storage Update 5/12/20 ...
- java9新特性-12-集合工厂方法:快速创建只读集合
1.官方Feature 269: Convenience Factory Methods for Collections 2.产生背景 要创建一个只读.不可改变的集合,必须构造和分配它,然后添加元素, ...
- SparkSQL基础
* SparkSQL基础 起源: 1.在三四年前,Hive可以说是SQL on Hadoop的唯一选择,负责将SQL编译成可扩展的MapReduce作业.鉴于Hive的性能以及与Spark的兼容,Sh ...