[CF1208D] Restore Permutation
题意:有一个长为\(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的更多相关文章
- D. Restore Permutation(权值线段树)
D. Restore Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- D. Restore Permutation
D. Restore Permutation 就是给一个n个数的全排,然后bi记录比ai小且在排在ai前面的数的和,求ai 树状数组维护,二分 #include<bits/stdc++.h> ...
- [Codeforces 1208D]Restore Permutation (树状数组)
[Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...
- D. Restore Permutation 树状数组+二分
D. Restore Permutation 题意:给定n个数a[i],a[ i ]表示在[b[1],b[i-1]]这些数中比 b[i]小的数的和,要你构造这样的b[i]序列 题解:利用树状数组 求比 ...
- 线段树维护最后一个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序列. 思路: 首 ...
- 1208D Restore Permutation
题目大意 给你一个序列s 让你求一个1~n的序列 使得对于第i个位置它前面所有小于p[i]的数的和恰好为s[i] 分析 我们可以从后往前确定每一位 每次一二分找到恰好等于s[i]的数 但是我们发现这样 ...
- cf1208 D Restore Permutation (二分+树状数组)
题意 让你构造一个长度为n的序列,记为p1……pn,(这个序列是1~n的全排列的一种) 给你n个数,记为s1……sn,si的值为p1……pi-1中小于pi的数的和. 思路 显然,应该倒着来,也就是从p ...
- 一句话CF
目录 \(\bf {Round \ \#500 \ (Div. \ 1)}\) \(\bf {Round \ \#589 \ (Div. \ 2)}\) \(\bf {Avito \ Cool \ C ...
- Manthan Codefest 19 题解
这套题还是有点质量的吧 -- 题目链接 A. XORinacci 傻叉签到题,因为异或的性质所以这个序列的循环节长度只有 \(3\) -- 查看代码 B. Uniqueness 因为序列长度乃至数的种 ...
随机推荐
- Mybatis使用时 resultMap与resultType、parameterMap与 parameterType的区别
Map:映射:Type:Java类型 resultMap 与 resultType.parameterMap 与 parameterType的区别在面试的时候被问到的几率非常高,出现的次数到了令人 ...
- 【BZOJ2622】[2012国家集训队测试]深入虎穴
虎是中国传统文化中一个独特的意象.我们既会把老虎的形象用到喜庆的节日装饰画上,也可能把它视作一种邪恶的可怕的动物,例如“武松打虎”或者“三人成虎”.“不入虎穴焉得虎子”是一个对虎的威猛的形象的极好体现 ...
- 【linux开发】Linux下配置java环境 安装eclipse
配置JDK环境 本文转自:http://www.cnblogs.com/fnng/archive/2013/01/30/2883815.html,有修改 下载 登录oracle的网站去下载JDK1.8 ...
- librdkafka 安装
1,Git clone git clone https://github.com/edenhill/librdkafka.git 2,cd librdkafka/ 3,./configure 4,ma ...
- 用adb logcat抓取log
实时打印的主要有:logcat main,logcat radio,logcat events,tcpdump,还有高通平台的还会有QXDM日志 状态信息的有:adb shell dmesg, ...
- windows上zeal安装和使用--离线API文档
1.官网:https://zealdocs.org/download.html#windows 2.github:https://github.com/zealdocs/zeal 3.下载:可下载安装 ...
- mysql数据库问题———登录进去无法操作显示You must reset your password using ALTER USER statement before executing this statement
linux操作mysql数据库,可以登陆进去,但是操作所有命令都显示You must reset your password using ALTER USER statement before exe ...
- python中,a=10.0 b=10.0 a is b 为什么输出是false
>>>a=10.0>>>b=10.0>>>a is bFalse为什么当a=10,b=10时,a is b输出的是True呢? >>& ...
- CodeForces - 714E + POJ - 3666 (dp严格单调递增与非严格单调递增)
左偏树 炒鸡棒的论文<左偏树的特点及其应用> 虽然题目要求比论文多了一个条件,但是……只需要求非递减就可以AC……数据好弱…… 虽然还没想明白为什么,但是应该觉得应该是这样——求非递减用大 ...
- Linux部署项目 shell脚本启动 及 Centos7开放指定端口
我们首先要在linux上安装好jdk tomcat mysql 这些基本环境,这些可以在楼主的 Linux入门 里面找到. linux部署spring项目 1. 右击项目,maven ...