预处理把左集划分为大小为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. bootstrap-datatimepicker插件的使用

    1.引入相关的css.js文件(因为是bootstrap的,首先应该引入bootstrap.css和bootstrap.js): <link rel="stylesheet" ...

  2. [Java IO]05_JSON操作

    目录 6.1 JSON 知识背景  6.1.1 JSON 简介  6.1.2 JSON 语法  6.1.3 JSON 的数据结构6.2 Java 中操作 JSON 数据  6.2.1 Jar包下载   ...

  3. 一文带你看清HTTP所有概念(转)

    一文带你看清HTTP所有概念   上一篇文章我们大致讲解了一下 HTTP 的基本特征和使用,大家反响很不错,那么本篇文章我们就来深究一下 HTTP 的特性.我们接着上篇文章没有说完的 HTTP 标头继 ...

  4. OpenCV3.0 + VS2015出现“ACCESS_MASK不明确”错误

    问题:Vs 使用openCV 3.0+ 出错error C2872: “ACCESS_MASK”: 不明确的符号 环境: 系统:Win7 环境:VS2015 64bit 原因: 是因为我项目中的其中一 ...

  5. Docker(二)Image 与网络

    Docker Image 我们介绍一下如何构造一个自定义的 Docker  Image.在Docker 中,我们使用Dokcerfile 构建一个docker的描述. 首先我们定义一下需要启动一个什么 ...

  6. make && make install

    ./configure  --prefix= /usr/local/python3.6.6 make &&  make  install  prefix=/usr/local/pyth ...

  7. Leetcode 面试题 01.01. 判定字符是否唯一

    实现一个算法,确定一个字符串 s 的所有字符是否全都不同. 示例 1: 输入: s = "leetcode"输出: false 示例 2: 输入: s = "abc&qu ...

  8. Python MonkeyRunner 连接设备总是返回连接成功问题

    device = mr.waitForConnection(1,deviceName) 当使用waitForConnection时,不管设备是否连接,device总是返回一个对象,所以没有办法通过 i ...

  9. 代码反向生成数据库注释更新sql

    原理 通过反射实体所在程序集,得到枚举值列表,再通过sql获取数据库表结构,两者拼接成sql. 规范 实体枚举字段最好也加上Description特性,方便多次更新: 代码 实体定义 public p ...

  10. Ninject 2.x细说---2.绑定和作用域

    Ninject 2.x细说---2.绑定和作用域 转载weixin_33725272 最后发布于2011-11-06 00:03:00 阅读数 9  收藏   Ninject中提供多种接口和实现类的绑 ...