Inversion Sequence

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

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 这题有两种做法,一开始直接用STL里的vector A掉了这个题目,然后基神让我试下用线段树来解这个题目,我就两种方法都写了 先来线段树: 从小到大插入到第k个格子。


/*/
线段树做法:
首先,弄个长度为n的线段树,维护sum,初始值为1
那么,sum[1]等于n,代表着空着的位置的数量
现在要把某个数字插入到p位置,就在线段树找到一个位置x,使得[x]里面的sum等于p;
然后把那个1修改为0,就表示那个位置不是空着的了。 /*/
#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
#include"cmath"
using namespace std;
#define MX 11111
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int sum[MX<<2]; void PushUp(int rt) {
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
} void Build(int l,int r,int rt) {
sum[rt]=1;
if(r==l)return ;
int m=(r+l)>>1;
Build(lson);
Build(rson);
PushUp(rt);
} int Query(int p,int l,int r,int rt) {
if(l==r) { //找到点,插入,返回点的位置值
sum[rt]--;
return l;
}
int m=(l+r)>>1,ret=0;
if(p<=sum[rt<<1]) ret=Query(p,lson);//点在左边,直接向左找
else ret=Query(p-sum[rt<<1],rson);// 减去左边没去找的点
PushUp(rt);
return ret;
}
int a[MX];
int ans[MX];
int main() {
int n;
while(~scanf("%d",&n)) {
Build(1,n,1);
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
}
bool check=1;
for(int i=1; i<=n; i++) {
if(sum[1]<a[i]+1) {
check=0;
break;
} else {
int p= Query(a[i]+1,1,n,1);
ans[p]=i;
}
}
if(!check)printf("No solution\n");
else {
int first=1;
for(int i=1; i<=n; i++) {
if(first)first=0;
else printf(" ");
printf("%d",ans[i]);
}
printf("\n");
}
}
return 0;
}

  

然后是STL:  从大到小插入到第k个格子。

/*/
STL-vector:
从最大的数开始插入队列;
利用vector中的insert()函数去将数字插入相应的位置;
这种方法暴力,简单。。。
但是每一次插入复杂度是O(n);
数据有点放水,还是A了。
/*/
#include"iostream"
#include"cstdio"
#include"cstring"
#include"string"
#include"algorithm"
#include"vector" using namespace std;
#define MX 10050
int num[MX]; int main() {
int n;
while(~scanf("%d",&n)) {
for(int i=0; i<n; i++) {
scanf("%d",&num[i]);
}
int flag=1;
vector<int > v;
v.push_back(0);
for(int i=n-1;i>=0; i--) {
if(v.size()<=num[i]) {
printf("No solution\n");
flag=0;
break;
}
v.insert(v.begin()+num[i],i+1);
}
if(flag) {
int first=1;
vector<int>::iterator it;
for(it=v.begin(); it!=v.end()-1; it++) {
if(first) {
first=0;
printf("%d",(*it));
}
else printf(" %d",(*it));
}
printf("\n");
}
}
return 0;
}

  

ACM: 强化训练-Inversion Sequence-线段树 or STL·vector的更多相关文章

  1. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  2. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  3. HDU-1394 Minimum Inversion Number 线段树+逆序对

    仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/O ...

  4. HDU 6047 Maximum Sequence(线段树)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...

  5. hdu 5828 Rikka with Sequence 线段树

    Rikka with Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...

  6. hdu1394Minimum Inversion Number(线段树,求最小逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  7. hdu1394(Minimum Inversion Number)线段树

    明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time ...

  8. HDU_1394_Minimum Inversion Number_线段树求逆序数

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  9. Wow! Such Sequence!(线段树4893)

    Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...

随机推荐

  1. C#学习笔记----AppDomain应用程序域

    使用.Net建立的可执行程序*.exe,并没有直接承载到进程当中,而是承载到应用程序域(AppDomain)当中.应用程序域是.Net引入的一个新概念,它比进程所占用的资源要少,可以被看做是一个轻量级 ...

  2. jQuery学习笔记---兄弟元素、子元素和父元素的获取

    我们这里主要总结jQuery中对某元素的兄弟元素.子元素和父元素的获取,原声的Javascript代码对这些元素的获取比较麻烦一些,而jQuery正好对这些方法进行封装,让我们更加方便的对这些元素进行 ...

  3. CSS3–1.css3 新增选择器

    1.后代级别选择器 2.同辈级别选择器 3.伪类选择器 4.属性选择器 5.UI伪类选择器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...

  4. JavaWeb学习之JSTL自定义标签库的使用、JSTL自定义函数库(7)

    一.自定义标签,步骤 * 确定需求 * <my:date /> 输出当前系统的时间 yyyy-MM-dd hh:mm:ss:SSS * 编写Java类 新建包名:com.yxl.tag,新 ...

  5. VC++中字符串编码的转换

    在以前VC++6.0中默认的字符集是多字节字符集(MBCS:Multi-Byte Character Set),而VS2005及以后默认的字符集是Unicode,这样导致以前在VC6.0中非常简单实用 ...

  6. 攻城狮在路上(贰) Spring(一)--- 软件环境、参考书目等一览表

    一.软件环境: 二.参考书目: <Spring 3.X 企业应用开发实战> 陈雄华.林开雄著 电子工业出版社出版 三.其他说明: spring 源码地址:https://github.co ...

  7. 简单的redis 性能测试

    C:\Users\luhan.qian\Desktop\Tools\redis C:\Users\luhan.qian\Desktop\Tools\redis $ redis-benchmark.ex ...

  8. PHPCMS V9 栏目列表调用文章点击量及评论数量方法

    很多朋友在用Phpcms做站时,具体需要在列表页.首页调用文章列表调用文章的点击量和评论排行,那么怎么才能做到在Phpcms v9首页.频道页.列表页.推荐位等页面获取文章浏览量和评论统计呢? 原因起 ...

  9. phpcms v9 常用调用标签(全)

    本文介绍phpcms v9中模板标签使用说明. {template ) {==}   {/,,)}     loop是data的时候用{thumb($v[thumb],,)} 分页标签------{$ ...

  10. 强化的单例属性_Effective Java

    Singleton指的是仅仅被实例化一次的类,比如唯一的系统组件等,成为Singleton的类测试起来也比较困难. 常用的方法: 1.公有静态final域+私有构造器 public class Egg ...