Educational Codeforces Round 81 (Rated for Div. 2)E(线段树)
预处理把左集划分为大小为1~i-1时,把全部元素都移动到右集的代价,记作sum[i]。
然后枚举终态时左集的大小,更新把元素i 留在/移动到 左集的代价。
树状数组/线段树处理区间修改/区间查询
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+;
struct Tree{
ll minn,lazy;
}tree[N<<];
ll sum[N];//前缀和
inline void build(int root,int l,int r){
if(l==r){
tree[root].minn=sum[l];//1~l的a[i]之和
tree[root].lazy=;
return;
}
int mid=(l+r)>>;
build((root<<),l,mid);
build((root<<|),mid+,r);
tree[root].minn=min(tree[(root<<)].minn,tree[(root<<|)].minn);//up
return;
}
inline void pushdown(int root){
if(!tree[root].lazy)
return;
tree[(root<<)].minn+=tree[root].lazy;
tree[(root<<|)].minn+=tree[root].lazy;
tree[(root<<)].lazy+=tree[root].lazy;
tree[(root<<|)].lazy+=tree[root].lazy;
tree[root].lazy=;
return;
}
inline void change(int root,int l,int r,int x,int y,int val){
if(r<x||l>y)
return;
if(x<=l&&r<=y){
tree[root].minn+=val;
tree[root].lazy+=val;
return;
}
int mid=(l+r)>>;
pushdown(root);
change((root<<),l,mid,x,y,val);
change((root<<|),mid+,r,x,y,val);
tree[root].minn=min(tree[(root<<)].minn,tree[(root<<|)].minn);//up
return;
}
int n,p[N],a[N],pos[N];
ll ans;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
for(int i=;i<=n;++i){
cin>>p[i];
pos[p[i]]=i;//数字p[i]出现的位置为i
}
for(int i=;i<=n;++i){
cin>>a[i];
sum[i]=sum[i-]+a[i];//sum[i]为左集合大小为i,把左集合所有元素都移动到右集合的花费
}
build(,,n-);
ans=min(a[],a[n]);//a[1]为左集为空,a[n]为右集为空
for(int i=;i<n;++i){//枚举左集大小,定下大小后,集合内元素也被定为1~i
change(,,n-,,pos[i]-,a[pos[i]]);//找到元素i出现的位置,在它出现位置左边的sum[i]分别加上把元素i从右集合移动到左集合的代价(原本的sum[1~i-1]为把原本处于位置1~i-1的元素都移动到右边,此时加上元素1~i从右移动到左的代价)
change(,,n-,pos[i],n,-a[pos[i]]);//在它出现位置及其右边的sum[i]分别减去把元素i从左集合移动到右集合的代价(元素i无需移动,可是移动的代价事先已经加到sum[i~n]里了)
ans=min(ans,tree[].minn);//如果左集大小为i的代价最小就更新最小值
}
cout<<ans;
return ;
}
Educational Codeforces Round 81 (Rated for Div. 2)E(线段树)的更多相关文章
- Educational Codeforces Round 61 (Rated for Div. 2) G(线段树,单调栈)
#include<bits/stdc++.h>using namespace std;int st[1000007];int top;int s[1000007],t[1000007];i ...
- [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)
[Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...
- Educational Codeforces Round 81 (Rated for Div. 2) A-E简要题解
链接:https://codeforces.com/contest/1295 A. Display The Number 贪心思路,尽可能放置更多位,如果n为奇数,消耗3去放置一个7,剩下的放1 AC ...
- Educational Codeforces Round 81 (Rated for Div. 2) B. Infinite Prefixes
题目链接:http://codeforces.com/contest/1295/problem/B 题目:给定由0,1组成的字符串s,长度为n,定义t = sssssss.....一个无限长的字符串. ...
- Educational Codeforces Round 81 (Rated for Div. 2) C. Obtain The String
题目链接:http://codeforces.com/contest/1295/problem/C 题目:给定字符串s,t. 给定一个空串z,需要按照规则把z构造成 string z == stri ...
- Educational Codeforces Round 81 (Rated for Div. 2)
A 0~9需要多少笔画,自取7和1,判奇偶 #include<bits/stdc++.h> using namespace std; #define ll long long #defin ...
- Educational Codeforces Round 81 (Rated for Div. 2) 题解
过了n天补的题解:D AB就不用说了 C. Obtain The String 思路挺简单的,就是贪心,但是直接贪心的复杂度是O(|s|*|t|),会超时,所以需要用到序列自动机 虽然名字很高端但是就 ...
- Educational Codeforces Round 81 (Rated for Div. 2) - D. Same GCDs(数学)
题目链接:Same GCDs 题意:给你两个数$a$,$m(1 \leq a < m \leq 10^{10})$,求有多少个$x$满足:$0 \leq x < m$且$gcd(a,m)= ...
- Educational Codeforces Round 80 (Rated for Div. 2)E(树状数组,模拟,思维)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],mx[],a[],pos[],sum ...
随机推荐
- SpringMVC-简单参数绑定
SpringMVC-简单参数绑定 众所周知,springmvc是用来处理页面的一些请求,然后将数据再通过视图返回给用户的,前面的几篇博文中使用的都是静态数据,为了能快速入门springmvc,在 ...
- SpringBoot学习- 7、问题Could not autowire. No beans of 'xxxx' type found处理
SpringBoot学习足迹 这个问题网上有好多同学都提到这个问题,代码可以运行,但是就是有红线,强迫症不能忍 自己试验下 1.增加一个final编译一下,再删掉就不会出红线了 public clas ...
- python:运行command
#!/usr/bin/python# -*- coding:utf-8 -*- import os os.system('cocos jscompile -s ./dir1 -d ./dir2')
- Java类、方法、属性等
java是面向对象的编程语言 Object,就是指面向对象的对象,对象就是类的具体实例. 在java里,对象是类的一个具体实例.就像:人,指一个类.张三.李四.王五等则是一个个具体的实例,也就是jav ...
- turtleh海龟库
Turtle海龟库 导入 import turtle turtle.setup(width,height,startx,starty) -setup():设置窗体的位置和大小 相对于桌面的起点的坐标以 ...
- yolov3 讲解
参考博客:https://blog.csdn.net/litt1e/article/details/88907542
- jQuery - 下拉框
jQuery - option的值 获取值 $("#equip_show").change(function(){ //获取文本 var checkText = $("# ...
- 复制文件或目录命令 - cp
1) 命令名称:cp 2) 英文原意:copy 3) 命令所在路径:/bin/cp 4) 执行权限:所有用户 5) 功能描述:复制文件或目录 语法: cp -rp [原文件或目录][目标目录] -r ...
- js 简单粗暴深拷贝
var obj = {} var newObj = JSON.parse(JSON.stringify(obj))
- JMeter压力测试,http请求压测,5分钟让你学会如何压测接口!
JMeter压力测试 官网:https://jmeter.apache.org 最新款的jmeter需要java8的支持,所以请自行安装jdk8.这里就不啰嗦了. 可以根据自己的系统下载zip或者是t ...