Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组


【Problem Description】

​ 给你一个长度为\(n\)的数组,第\(i\)个元素\(s_i\)表示一个排列中第\(i\)个元素之前,并且小于\(p_i\)的元素的和。求出满足此条件的排列。

【Solution】

​ 假设\(n=5\),\(s[]=\{0,0,3,7,3\}\)。从后往前看,最后一个值为\(3\),表示存在一个数\(x\),使得\(1+2+\dots+x=3\)。易知\(x=2\),所以\(p_5=x+1=3\)。然后第二个值为\(7\),表示存在一个数\(x\),使得\(1+2+\dots +x-3=7\)。减\(3\)是因为\(p_5=3\),不可能出现在\(p_4\)之前。所以可知\(x=4\),所以\(p_4=x+1=5\)。同理利用此方法可以求出此排列。

​ 上述方法其实就是在找一个最大的\(x\),使得将所有满足\(1\le y\le x\),并且未出现过的\(y\)值相加使其等于\(s_i\),则\(p_i=x+1\)。可以想到用树状数组维护前缀和,用二分查找最大的满足条件的\(x\)。每求得一个数,就将此数清零即可。

​ 但其实用树状数组中数组的特性,有更巧妙的方法。我们知道在树状数组中,对于数组\(tree[i]\),它所维护的区间为\([i-lowbit(i)+1,i]\),所以对于\(tree[2^i]\),它所维护的区间就为\([1,2^i]\)。所以就可以利用此特性加上树状数组的操作,维护一个类似倍增方法,并且支持在线修改操作。

​ 例如求\(sum[1+2+\dots10]=tree[2^3]+tree[2^3+2^1]\)


【Code】

/*
* @Author: Simon
* @Date: 2019-08-26 18:14:20
* @Last Modified by: Simon
* @Last Modified time: 2019-08-26 20:12:53
*/
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 200005
int a[maxn],tree[maxn],ans[maxn];
inline int lowbit(int x){
return x&(-x);
}
inline void update(int x,int val){
for(int i=x;i<maxn;i+=lowbit(i)){
tree[i]+=val;
}
}
inline int query(int x){
int ans=0;
for(int i=x;i>0;i-=lowbit(i)){
ans+=tree[i];
}
return ans;
}
int solve(int k,int n){
int num=0,sum=0;
for(int i=21;i>=0;i--){
if(num+(1<<i)<=n&&sum+tree[num+(1<<i)]<=k){//求一个最大num,使得sum[1+2+...+num]=k
num+=1<<i;
sum+=tree[num];
}
}
return num+1;
}
Int main(){
#ifndef ONLINE_JUDGE
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
for(int i=1;i<=n;i++){
update(i,i)/*初始所有值都存在*/;cin>>a[i];
}
for(int i=n;i>=1;i--){
ans[i]=solve(a[i],n);
update(ans[i],-ans[i]);//将ans[i]清零。
}
for(int i=1;i<=n;i++) cout<<ans[i]<<' ';
cout<<endl;
#ifndef ONLINE_JUDGE
system("pause");
#endif
return 0;
}

Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组的更多相关文章

  1. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造

    Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造 [Problem Descripti ...

  2. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-E. Let Them Slide-思维+数据结构

    Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-E. Let Them Slide-思维+数据结构 [Problem ...

  3. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组

    题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...

  4. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)

    转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...

  5. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) F. Bits And Pieces sosdp

    F. Bits And Pieces 题面 You are given an array

  6. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) G. Polygons 数论

    G. Polygons Description You are given two integers

  7. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) (1208F,1208G,1208H)

    1208 F 大意:  给定序列$a$, 求$\text{$a_i$|$a_j$&$a_k$}(i<j<k)$的最大值 枚举$i$, 从高位到低位贪心, 那么问题就转化为给定$x$ ...

  8. RMQ+差分处理(Let Them Slide)Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)

    题意:https://codeforc.es/contest/1208/problem/E 现有n行w列的墙,每行有一排连续方块,一排方块可以左右连续滑动,且每个方块都有一个价值,第i 列的价值定义为 ...

  9. 线段树维护最后一个0的位置(Restore Permutation)Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)

    题意:https://codeforc.es/contest/1208/problem/D 给你长度为n的序列,s[i]的值为p[1]到p[i-1]中比p[i]小的数的和,让你求出p序列. 思路: 首 ...

随机推荐

  1. .NET ftp文件上传和下载

    文章参考来源地址:https://blog.csdn.net/wybshyy/article/details/52095542 本次对代码进行了一点扩展:将文件上传到ftp指定目录下,若目录不存在则创 ...

  2. elk配置路径

    elk/usr/local/etc/elasticsearch-6.5.1/usr/local/Cellar/logstash/6.5.1/./logstash -f/usr/local/Cellar ...

  3. RabbitMQ 从入门到精通 (一)

    目录 1. 初识RabbitMQ 2. AMQP 3.RabbitMQ的极速入门 4. Exchange(交换机)详解 4.1 Direct Exchange 4.2 Topic Exchange 4 ...

  4. 申请Google Adsense通过终审的关键点

    一.申请Google Adsense前网站数据 申请Google Adsense广告的前提条件及网站环境如下: 60+篇文章. 一天10个ip左右. 谷歌索引200左右,当然包括标签分页等索引. 经过 ...

  5. CMDB资产采集的四种方式

    转 https://www.cnblogs.com/guotianbao/p/7703921.html 资产采集的概念 资产采集的四种方式:Agent.SSH.saltstack.puppet 资产采 ...

  6. 搭建mqtt服务器apollo

    使用的apollo,官网太慢,附上百度云下载地址: 链接:https://pan.baidu.com/s/1NIq6R71hlyPuaUBwPoMPNg 提取码:36vw 原文链接:https://b ...

  7. pytest_05_fixture之conftest.py

    前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录,用例3需要先登录.很显然 ...

  8. Map 集合按字母排序方法

    @Testpublic void testMapSort() { Map<String, String> map = new HashMap<>(); map.put(&quo ...

  9. 如何监视 WPF 中的所有窗口,在所有窗口中订阅事件或者附加 UI

    原文:如何监视 WPF 中的所有窗口,在所有窗口中订阅事件或者附加 UI 由于 WPF 路由事件(主要是隧道和冒泡)的存在,我们很容易能够通过只监听窗口中的某些事件使得整个窗口中所有控件发生的事件都被 ...

  10. mybatis中参数为list集合时使用 mybatis in查询

    mybatis中参数为list集合时使用 mybatis in查询 一.问题描述mybatis sql查询时,若遇到多个条件匹配一个字段,sql 如: select * from user where ...