CSU 1555(逆序数复原)
2015-12-11 00:01
369 查看
I - I
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%lld
& %llu
Submit Status Practice CSU
1555
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
Sample Output
题解:找规律,查了题解才知道规律,插入第i个数字,那么就去查找有没有a[i]+1个空位,如果有那么就插入到那个位置,否则就无解,使用线段树维护空位的个数,即区间和查询
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 20000
struct point
{
int lc,rc;
int sum;
}tree[N<<2];
int a
,ans
;
void Pushup(int rt)
{
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void build(int L,int R,int rt)
{
tree[rt].lc=L;
tree[rt].rc=R;
if(L==R)
{
tree[rt].sum=1;
return ;
}
int mid=(L+R)>>1;
build(L,mid,rt<<1);
build(mid+1,R,rt<<1|1);
Pushup(rt);
}
int query(int rt,int x)
{
if(tree[rt].lc==tree[rt].rc)
{
tree[rt].sum=0;
return tree[rt].lc;
}
int ret;
if(tree[rt<<1].sum>=x)
{
ret= query(rt<<1,x);
}
else
{
ret=query((rt<<1|1),(x-tree[rt<<1].sum));
}
Pushup(rt);
return ret;
}
int main()
{
#ifdef CDZSC
freopen("i.txt","r",stdin);
#endif
int n;
while(~scanf("%d",&n))
{
int ok=1;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,n,1);
for(int i=1;i<=n;i++)
{
if(tree[1].sum<a[i]+1)
{
ok=0;
break;
}
else
{
ans[query(1,a[i]+1)]=i;
}
}
if(!ok)
{
puts("No solution");
}
else
{
for(int i=1;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans
);
}
}
return 0;
}
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%lld
& %llu
Submit Status Practice CSU
1555
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
题解:找规律,查了题解才知道规律,插入第i个数字,那么就去查找有没有a[i]+1个空位,如果有那么就插入到那个位置,否则就无解,使用线段树维护空位的个数,即区间和查询
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 20000
struct point
{
int lc,rc;
int sum;
}tree[N<<2];
int a
,ans
;
void Pushup(int rt)
{
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void build(int L,int R,int rt)
{
tree[rt].lc=L;
tree[rt].rc=R;
if(L==R)
{
tree[rt].sum=1;
return ;
}
int mid=(L+R)>>1;
build(L,mid,rt<<1);
build(mid+1,R,rt<<1|1);
Pushup(rt);
}
int query(int rt,int x)
{
if(tree[rt].lc==tree[rt].rc)
{
tree[rt].sum=0;
return tree[rt].lc;
}
int ret;
if(tree[rt<<1].sum>=x)
{
ret= query(rt<<1,x);
}
else
{
ret=query((rt<<1|1),(x-tree[rt<<1].sum));
}
Pushup(rt);
return ret;
}
int main()
{
#ifdef CDZSC
freopen("i.txt","r",stdin);
#endif
int n;
while(~scanf("%d",&n))
{
int ok=1;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,n,1);
for(int i=1;i<=n;i++)
{
if(tree[1].sum<a[i]+1)
{
ok=0;
break;
}
else
{
ans[query(1,a[i]+1)]=i;
}
}
if(!ok)
{
puts("No solution");
}
else
{
for(int i=1;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans
);
}
}
return 0;
}
相关文章推荐
- animate.css简介
- //监测网络状态(AFNetworking) 服务端 客户端
- 程序员的小小世界应该懂得事儿
- JavaScript的伪协议
- java项目命名规则
- jsp的标签和EL表达式
- String(字符串API的处理)
- <a>标签调用js方法的方式
- 关于开源中国社区(oschina)发表博客被屏蔽敏感字体
- 怎样更改MindMapper中的边界格式
- 自带的扫码 扫描区域
- eclipse classic添加html之类的web插件
- Error: ShouldNotReachHere()
- ADT创建项目时出现appcompat_v7的问题
- 不提示直接关闭网页窗口的JS代码
- Eclipse中提高Android SDK Manager下载速度方法
- OGNL和Struts2标签
- npm 命令安装模块
- 实现了实时消息推送后的一点感悟
- tomcat-maven-plugin的使用