传送门


题意:有一个长为\(n\)的排列\(p\),设\(S_i=\sum_{j=1}^{i-1}p_j\cdot[p_j<p_i]\),给出\(S\),要求还原出\(p\)。保证有解,\(n\leq 2\times 10^5\)。

考虑倒序将\(S\)还原为全\(0\)的序列,从小到大依次考虑插入每个数的影响。假设在位置\(x\)插入\(i\),显然此时\(S_x=0\),且会使得位置\(x\)右侧的每一个未插入数字的\(S_y\)都减去\(i\)。因此对于第\(i\)个数,唯一合法的位置就是所有\(S_x=0\)的位置中最右侧的那个。由于保证有解,维护最小值,在线段树上二分即可。

Code:

#include <cstdio>
#include <cctype>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm>
#define R register
#define ll long long
using namespace std;
const int N = 210000, K = N << 2;
const ll inf = 1e18; int n, lt[K], rt[K], p[N];
ll a[N], minVal[K], tag[K];
inline void pushdown(int); template <class T> inline void read(T &x) {
x = 0;
char ch = getchar(), w = 0;
while (!isdigit(ch))
w = (ch == '-'), ch = getchar();
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
x = w ? -x : x;
return;
} void build(int k) {
if (lt[k] == rt[k]) {
minVal[k] = a[lt[k]];
return;
}
int mid = (lt[k] + rt[k]) >> 1, l = k << 1, r = (k << 1) + 1;
lt[l] = lt[k], rt[l] = mid, lt[r] = mid + 1, rt[r] = rt[k];
build(l), build(r), minVal[k] = min(minVal[l], minVal[r]);
return;
} void modify(int k, int x, int y, ll w) {
if (lt[k] >= x && rt[k] <= y) {
minVal[k] += w, tag[k] += w;
return;
}
pushdown(k);
int mid = (lt[k] + rt[k]) >> 1, l = k << 1, r = (k << 1) + 1;
if (x <= mid) modify(l, x, y, w);
if (y > mid) modify(r, x, y, w);
minVal[k] = min(minVal[l], minVal[r]);
return;
} int query(int k) {
if (lt[k] == rt[k]) {
minVal[k] = inf;
return lt[k];
}
int l = k << 1, r = (k << 1) + 1, ret;
pushdown(k), ret = query(minVal[r] ? l : r);
minVal[k] = min(minVal[l], minVal[r]);
return ret;
} inline void pushdown(int k) {
int l = k << 1, r = (k << 1) + 1;
if (tag[k]) {
modify(l, lt[k], rt[k], tag[k]);
modify(r, lt[k], rt[k], tag[k]);
tag[k] = 0;
}
return;
} int main() {
read(n);
for (R int i = 1; i <= n; ++i)
read(a[i]);
lt[1] = 1, rt[1] = n, build(1);
for (R int i = 1; i <= n; ++i) {
int pos = query(1);
p[pos] = i, modify(1, pos, n, -i);
}
for (R int i = 1; i <= n; ++i)
printf("%d ", p[i]);
return 0;
}

[CF1208D] Restore Permutation的更多相关文章

  1. D. Restore Permutation(权值线段树)

    D. Restore Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. D. Restore Permutation

    D. Restore Permutation 就是给一个n个数的全排,然后bi记录比ai小且在排在ai前面的数的和,求ai 树状数组维护,二分 #include<bits/stdc++.h> ...

  3. [Codeforces 1208D]Restore Permutation (树状数组)

    [Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...

  4. D. Restore Permutation 树状数组+二分

    D. Restore Permutation 题意:给定n个数a[i],a[ i ]表示在[b[1],b[i-1]]这些数中比 b[i]小的数的和,要你构造这样的b[i]序列 题解:利用树状数组 求比 ...

  5. 线段树维护最后一个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序列. 思路: 首 ...

  6. 1208D Restore Permutation

    题目大意 给你一个序列s 让你求一个1~n的序列 使得对于第i个位置它前面所有小于p[i]的数的和恰好为s[i] 分析 我们可以从后往前确定每一位 每次一二分找到恰好等于s[i]的数 但是我们发现这样 ...

  7. cf1208 D Restore Permutation (二分+树状数组)

    题意 让你构造一个长度为n的序列,记为p1……pn,(这个序列是1~n的全排列的一种) 给你n个数,记为s1……sn,si的值为p1……pi-1中小于pi的数的和. 思路 显然,应该倒着来,也就是从p ...

  8. 一句话CF

    目录 \(\bf {Round \ \#500 \ (Div. \ 1)}\) \(\bf {Round \ \#589 \ (Div. \ 2)}\) \(\bf {Avito \ Cool \ C ...

  9. Manthan Codefest 19 题解

    这套题还是有点质量的吧 -- 题目链接 A. XORinacci 傻叉签到题,因为异或的性质所以这个序列的循环节长度只有 \(3\) -- 查看代码 B. Uniqueness 因为序列长度乃至数的种 ...

随机推荐

  1. OpenStack 实现技术分解 (6) 通用库 — oslo_log

    目录 目录 前文列表 扩展阅读 日志级别 oslolog 初始化设置 DEMO oslolog 的相关配置项 oslolog 的日志级别 oslolog 的使用技巧 推荐使用 LOGdebug 的地方 ...

  2. nginx实现域名跳转

    server { listen 80; server_name www.dd.com www.tt.com; index index.html index.htm index.php; root /u ...

  3. 编译中出现的undefined reference to XXX

    主要是在链接时发现找不到某个函数的实现文件.可以查找是否在makefile里没有增加待编译的.c文件,或者静态库没有引用

  4. TestNG+extentReports+log4j2 完善自动化测试框架——美观的报告和保留日志文件

    1:导入Maven依赖<dependency> <groupId>com.aventstack</groupId> <artifactId>extent ...

  5. linux应用程序启动时加载库错误问题

    ldd text查看依赖库 ln -s /lib64/libpcre.so.0 /usr/local/lib/libpcre.so做软连接

  6. MySQL-快速入门(15)MySQL Replication,主从复制

    1.何为主从复制. 从一个MySQL主服务器(master)将数据复制到另一台或多台MySQL从服务器(slaves)的过程,将主数据库的DDL和DML操作通过二进制日志传到复制服务器上,然后在从服务 ...

  7. CAS单点登录系统--进阶

    2.CAS服务端数据源设置 2.1需求分析 我们现在让用户名密码从我们的优乐选的user表里做验证 2.2配置数据源 (1)修改cas服务端中web-inf下deployerConfigContext ...

  8. 偶遇com组件 .rc 文件 not found .tlb文件问题:

    好久木有弄com组件来,记忆已经模糊了,今天遇见一个编译问题,折腾了半天,mark一下: xxx_x64.rc(273): error RC2135: file not found: xxx.tlb ...

  9. [2019多校联考(Round 6 T3)]脱单计划 (费用流)

    [2019多校联考(Round 6 T3)]脱单计划 (费用流) 题面 你是一家相亲机构的策划总监,在一次相亲活动中,有 n 个小区的若干男士和 n个小区的若干女士报名了这次活动,你需要将这些参与者两 ...

  10. 【LGR-065】洛谷11月月赛 III Div.2

    临近$CSP$...... 下午打了一发月赛,感觉很爽. 非常菜的我只做了前两题......然而听说前两题人均过...... 写法不优秀被卡到$#1067$...... T1:基础字符串练习题: 前缀 ...