博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF1208D Restore Permutation
阅读量:4701 次
发布时间:2019-06-09

本文共 1120 字,大约阅读时间需要 3 分钟。

显然发现可以从右往左依次确定。

考虑求出前缀和,每次查询当前位置上的值,然后删掉当前数,动态维护一下前缀和就好了

对于查询值可以树状数组+二分\(O(n\log^2n)\),也可以线段树\(O(n\log n)\)

貌似树状数组+二分比线段树跑的要快

代码:

#include
#include
#include
#include
using namespace std;#define rg registervoid read(long long &x){ char ch;bool ok; for(ok=0,ch=getchar();!isdigit(ch);ch=getchar())if(ch=='-')ok=1; for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());if(ok)x=-x;}const int maxn=2e5+10;int n,m,ans[maxn];long long f[maxn],a[maxn];#define lowbit(i) (i&(-i))void add(int x,int y){for(rg int i=x;i<=n;i+=lowbit(i))f[i]+=y;}long long get(int x){long long ans=0;for(rg int i=x;i;i-=lowbit(i))ans+=f[i];return ans;}bool check(int x,long long y){ long long now=get(x); return now<=y;}int main(){ scanf("%d",&n); for(rg int i=1;i<=n;i++)read(a[i]),add(i,i); for(rg int i=n;i;i--){ int l=1,r=n; while(l<=r){ int mid=(l+r)>>1; if(check(mid,a[i]))l=mid+1; else r=mid-1; } ans[i]=l;add(l,-l); } for(rg int i=1;i<=n;i++)printf("%d ",ans[i]);}

转载于:https://www.cnblogs.com/lcxer/p/11505886.html

你可能感兴趣的文章
算法与设计模式
查看>>
(4)理解 neutron ml2---port创建流程代码解析
查看>>
python List
查看>>
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.
查看>>
免费资源:Polaris UI套件 + Linecons图标集(AI, PDF, PNG, PSD, SVG)
查看>>
http响应状态码大全
查看>>
C# winform 使用DsoFramer 创建 显示office 文档
查看>>
找工作的一些感悟——前端小菜的成长
查看>>
C#委托和事件的应用Observer模式实例
查看>>
codevs1018 单词接龙(DFS)
查看>>
内容分发系统MediaEW:助新闻媒体转投HTML5
查看>>
HTML5 Canvas ( 径向渐变, 升级版的星空 ) fillStyle, createRadialGradient
查看>>
Stanford Local Programming Contest 2011
查看>>
多线程中,NSOperationQueue和GCD的区别
查看>>
python生成.exe文件
查看>>
PHP面向对象(OOP)----分页类
查看>>
监听SD卡状态
查看>>
vs2017 EFCore 迁移数据库命令
查看>>
serialVersionUID的作用
查看>>
liunx trac 插件使用之GanttCalendarPlugin
查看>>