Educational Codeforces Round 81 (Rated for Div. 2)E. Permutation Separation(n个数分成两个区间,左区间小于右区间)

E. Permutation Separation

You are given a permutation p1,p2,…,pnp1,p2,…,pn (an array where each integer from 11 to nn appears exactly once). The weight of the ii-th element of this permutation is aiai.

At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p1,p2,…,pkp1,p2,…,pk, the second — pk+1,pk+2,…,pnpk+1,pk+2,…,pn, where 1≤k<n1≤k<n.

After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay aiai dollars to move the element pipi.

Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met.

For example, if p=[3,1,2]p=[3,1,2] and a=[7,1,4]a=[7,1,4], then the optimal strategy is: separate pp into two parts [3,1][3,1] and [2][2] and then move the 22-element into first set (it costs 44). And if p=[3,5,1,6,2,4]p=[3,5,1,6,2,4], a=[9,1,9,9,1,9]a=[9,1,9,9,1,9], then the optimal strategy is: separate pp into two parts [3,5,1][3,5,1] and [6,2,4][6,2,4], and then move the 22-element into first set (it costs 11), and 55-element into second set (it also costs 11).

Calculate the minimum number of dollars you have to spend.

Input

The first line contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the length of permutation.

The second line contains nn integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n). It's guaranteed that this sequence contains each element from 11 to nn exactly once.

The third line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

Output

Print one integer — the minimum number of dollars you have to spend.

链接:http://codeforces.com/problemset/problem/1295/E

题意:n个数刚好是1~n,分成两个区间,左区间每个数<右区间每个数,两个区间其中一个为空也可以。求最小移动代价。

题解:线段树维护区间最小移动代价。

/*
1.区间[1,n-1]枚举切割位置
2.每往右枚举移动一次,从右区间往左区间移动一个数 x
(枚举向右移动时,第i次移动是将i从右区间移动到左区间,) 
3.前缀和作用:sum[i]  下标1~i所有的数移动到右区间需要的 价值 
4.对于第i个位置,找到i在p中的位置 id ,将区间[1,id-1] 减去 a[id],区间[id,n-1] 加上a[id]
*/ 
#include <iostream>
using namespace std;
int n;
int p[200005];
int id[200005];
long long a[200005];
long long sum[200005];
struct node
{
	long long lazy;
	long long v;
}tree[200005*4];
void pushup(int i)
{
	tree[i].v=min(tree[i+i].v,tree[i+i+1].v);
}
void pushdown(int i)
{
	tree[i+i].lazy+=tree[i].lazy;
	tree[i+i+1].lazy+=tree[i].lazy;
	tree[i+i].v+=tree[i].lazy;
	tree[i+i+1].v+=tree[i].lazy;
	tree[i].lazy=0ll;
}
void build(int i,int l,int r)
{
	if(l==r)
	{
		tree[i].lazy=0ll;
		tree[i].v=sum[l];
		return;
	}
	int mid=(l+r)/2;
	build(i+i,l,mid);
	build(i+i+1,mid+1,r);
	pushup(i); 
}

void updata(int i,int ll,int rr,long long x,int l,int r)
{
	if(tree[i].lazy&&ll!=rr)
	pushdown(i);
	if(l<=ll&&rr<=r)
	{
		tree[i].lazy+=x;
		tree[i].v+=x;
		return;
	}
	
	int mid=(ll+rr)/2;
	if(mid>=l)
	updata(i+i,ll,mid,x,l,r);
	if(mid<r)
	updata(i+i+1,mid+1,rr,x,l,r);
	pushup(i);
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	scanf("%d",&p[i]),id[p[i]] = i;
	sum[0]=0ll;
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&a[i]);
		sum[i]=sum[i-1]+a[i];
	}
	build(1,1,n-1);
	long long minans=min(tree[1].v,a[n]);
	for(int i=1;i<=n-1;i++)
	{
		int top=id[i];
		if(top!=1)
		{
			updata(1,1,n-1,a[top],1,top-1);
		}
		if(top!=n)
		{
			updata(1,1,n-1,-a[top],top,n-1);
		}
		minans=min(tree[1].v,minans);
	}
	printf("%lld\n",minans);
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值