预处理把左集划分为大小为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(线段树)的更多相关文章

  1. 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 ...

  2. [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)

    [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...

  3. Educational Codeforces Round 81 (Rated for Div. 2) A-E简要题解

    链接:https://codeforces.com/contest/1295 A. Display The Number 贪心思路,尽可能放置更多位,如果n为奇数,消耗3去放置一个7,剩下的放1 AC ...

  4. Educational Codeforces Round 81 (Rated for Div. 2) B. Infinite Prefixes

    题目链接:http://codeforces.com/contest/1295/problem/B 题目:给定由0,1组成的字符串s,长度为n,定义t = sssssss.....一个无限长的字符串. ...

  5. 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 ...

  6. 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 ...

  7. Educational Codeforces Round 81 (Rated for Div. 2) 题解

    过了n天补的题解:D AB就不用说了 C. Obtain The String 思路挺简单的,就是贪心,但是直接贪心的复杂度是O(|s|*|t|),会超时,所以需要用到序列自动机 虽然名字很高端但是就 ...

  8. 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)= ...

  9. Educational Codeforces Round 80 (Rated for Div. 2)E(树状数组,模拟,思维)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],mx[],a[],pos[],sum ...

随机推荐

  1. JUC-JUC是什么?

    一.JUC是什么? java.util.concurrent在并发编程中使用的工具类 进程/线程回顾 1.进程/线程是什么? 进程:进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动.它是 ...

  2. 论Mac与windows的STS下的路径问题

    mac下的 <!-- javaBean生成在哪里 --> <javaModelGenerator targetPackage="com.atcrowdfunding.bea ...

  3. 《NVM-Express-1_4-2019.06.10-Ratified》学习笔记(8.8)-- Reservations

    8.8 Reservations 预订 NVMe的reservation预订功能,用于让两个或多个主机能够协调配合的访问共享namespace.使用这些功能的协议和方式超出了本规格说明书的范围.对这些 ...

  4. eclipse的一些使用

    1.恢复默认视图 window->perspective->open perspective ->open java 2.打开其他的一些视图,比如server(tomcat,目前使用 ...

  5. 金蝶云星空使用WebAPI来新增单据

    有很多客户需求在后台自动生成某张单据,金蝶云星空提供了WebApi,包含了保存,提交,审核,删除单据的接口,下面以生产订单的保存,提交,审核为例,说明一下应用WebApi后台自动生成生产订单的功能,下 ...

  6. K-NN graph

    tasks: 1. unsupervised knn https://scikit-learn.org/stable/modules/neighbors.html#unsupervised-neigh ...

  7. ORA-01789: 查询块具有不正确的结果列数

    问题描述 ORA-01789: 查询块具有不正确的结果列数 问题原因 sql语句用union时的 两个语句查询的字段不一致,好像顺序也要保持一致才行

  8. 后台异常 - org.apache.jasper.JasperException

    问题描述 org.apache.jasper.JasperException: Unable to compile class for JSP 问题原因 tomcat6不支持jdk1.8版本

  9. dyt说反话(注意字符串输入)

    题目内容: dyt喜欢对lrh说的话说反话,现给出lrh说的k句话,输出dyt所说的反话. 输入格式 第一行是样例个数k(k<10) 接下来k行,每行包含lrh说的一句话(每句话长度不超过50, ...

  10. APP 安全测试

    http://www.cnblogs.com/wetest/p/6694529.html