题目链接

link

Solution

暴力一眼就可以看出来,枚举分界点,然后左右两边统计答案即可,但复杂度是我们无法接受的

然后我们看我们可以优化哪一部分

\(1^0\) 枚举:这部分没有办法优化

\(2^0\) 统计答案

这里我们看每一个位置上的数字在什么时候会有增加答案

当这种有的位置可以改变答案的时候,我们就要考虑贡献法

由题意,分割点位置不同时,每个位置对于该状态下答案是否贡献是不同的

“是否贡献”还是连续的,直接上线段树维护就好

(这里解释有点玄学,但是用贡献法还是不难理解的)

CODE

#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace yspm{
inline int read()
{
int res=0,f=1; char k;
while(!isdigit(k=getchar())) if(k=='-') f=-1;
while(isdigit(k)) res=res*10+k-'0',k=getchar();
return res*f;
}
const int N=2e5+10;
struct node{
int minn,add,l,r;
#define l(p) t[p].l
#define r(p) t[p].r
#define add(p) t[p].add
#define minn(p) t[p].minn
}t[N<<2];
int sum[N],a[N],val[N],pos[N],n,ans;
inline int min(int x,int y){return x<y?x:y;}
inline int max(int x,int y){return x>y?x:y;}
inline void push_up(int p)
{
return minn(p)=min(minn(p<<1),minn(p<<1|1)),void();
}
inline void spread(int p)
{
if(add(p))
{
minn(p<<1)+=add(p); minn(p<<1|1)+=add(p);
add(p<<1)+=add(p); add(p<<1|1)+=add(p);
} return add(p)=0,void();
}
inline void build(int p,int l,int r)
{
l(p)=l; r(p)=r; if(l==r) return minn(p)=sum[l],void();
int mid=(l+r)>>1;
build(p<<1,l,mid); build(p<<1|1,mid+1,r);
return push_up(p),void();
}
inline void change(int p,int l,int r,int d)
{
if(l(p)>r||r(p)<l) return ;
if(l<=l(p)&&r(p)<=r){minn(p)+=d; add(p)+=d; return ;} spread(p);
int mid=(l(p)+r(p))>>1;
change(p<<1,l,r,d); change(p<<1|1,l,r,d);
return push_up(p),void();
}
signed main()
{
n=read(); for(int i=1;i<=n;++i) a[i]=read(),pos[a[i]]=i;
for(int i=1;i<=n;++i) val[i]=read(),sum[i]=sum[i-1]+val[i];
build(1,1,n-1); ans=minn(1);
for(int i=1;i<=n;++i)
{
change(1,1,pos[i]-1,val[pos[i]]);
change(1,pos[i],n-1,-val[pos[i]]);
ans=min(ans,minn(1));
} return printf("%lld\n",ans),0;
}
}
signed main(){return yspm::main();}

Codeforces 1295E Permutation Separation的更多相关文章

  1. Codeforces 1295E. Permutation Separation (线段树)

    https://codeforces.com/contest/1295/problem/E 建一颗线段树,叶子结点是花费从1到i所需要花费的前缀和,表示前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. codeforces 895A Pizza Separation 枚举

    codeforces 895A Pizza Separation 题目大意: 分成两大部分,使得这两部分的差值最小(注意是圆形,首尾相连) 思路: 分割出来的部分是连续的,开二倍枚举. 注意不要看成0 ...

  4. 贪心 CodeForces 137B Permutation

    题目传送门 /* 贪心:因为可以任意修改,所以答案是没有出现过的数字的个数 */ #include <cstdio> #include <algorithm> #include ...

  5. codeforces B. Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/359/B 题目意思:给定n和k的值,需要构造一条长度为2n(每个元素取值范围只能是[1,2n])且元素各不 ...

  6. Codeforces 818B Permutation Game

    首先看一下题目 B. Permutation Game time limit per test 1 second memory limit per test 256 megabytes input s ...

  7. Codeforces 1158C Permutation recovery

    https://codeforces.com/contest/1158/problem/C 题目 已知 $p_1, p_2, \dots, p_n$ 是 $1$ 到 $n$ 的一个排列. 给出关于这个 ...

  8. Codeforces - 1033C - Permutation Game - 简单dp - 简单数论

    https://codeforces.com/problemset/problem/1033/C 一开始觉得自己的答案会TLE,但是吸取徐州赛区的经验去莽了一发. 其实因为下面这个公式是 $O(nlo ...

  9. Codeforces 1159E Permutation recovery(构造+拓扑)

    这道题其实只要解决了什么时候输出 -1 ,那么此题的构造方法也就解决了.首先我们可以观察这组 3 3 4 和 3 4 4 ,可以算出第二组是不成立的,在观察一组 2 3 4 5 和  3 2 4 5 ...

随机推荐

  1. RCE

    RCE remote command/code execute 远程系统命令/代码执行 系统从设计上需要给用户提供指定的远程命令操作的接口.可以测试一下自动运维平台. 在PHP中,使用system.e ...

  2. PAT Advanced 1151 LCA in a Binary Tree (30) [树的遍历,LCA算法]

    题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...

  3. 一天一个设计模式——Builder建造者模式

    一.模式说明 在现实世界中,当我们要构造一个大型工程时(建一个大楼),通常的做法是先建造工程的每个独立部分,然后再逐步构造完成(先打地基,再搭框架,最后逐层累造).在程序设计领域,构造一个复杂的类时( ...

  4. 洛谷 P1020 导弹拦截

    题目传送门 解题思路: 其实就是求一遍最长不上升子序列和最长上升子序列 AC代码: #include<iostream> #include<cstdio> #include&l ...

  5. JAVA多线程的基础

    线程与进程的区别 1.线程与进程 每个正在系统上运行的程序都是一个进程.每个进程包含一到多个线程.线程是一组指令的集合,或者是程序的特殊段,它可以在程序里独立执行.也可以把它理解为代码运行的上下文.所 ...

  6. Java连载70-冒泡算法、选择算法

    一.冒泡排序 1.也就是依次选出最大的放在最后面 package com.bjpowernode.java_learning; ​ public class D70_1_BubbleSort { pu ...

  7. 吴裕雄--天生自然TensorFlow2教程:数学运算

    import tensorflow as tf b = tf.fill([2, 2], 2.) a = tf.ones([2, 2]) a+b a-b a*b a/b b // a b % a tf. ...

  8. MySQL性能管理及架构设计:第2章 什么影响了MySQL性能

    第2章 什么影响了MySQL性能 2-1 影响性能的几个方面 1.服务器的硬件 2.服务器的操作系统 3.数据库的存储引擎 4.数据库的参数配置 5.数据库表结构设计和SQL语句的编写和优化 2-2 ...

  9. (函数)P1217 [USACO1.5]回文质数 Prime Palindromes

    题解: 第一次: 算法复杂度过高,导致编译超时,需要优化 #include<stdio.h>#include<math.h>int a[100000001] = { 0 };i ...

  10. sourceTree 代码回滚(git 和http)

    近些时候,有遇到提交后代码有误的情况,所以需要回退到前一个版本.因为不常见,所以每次都不是很熟练,记录于此,以备查阅. 一.[将master重置到这次提交] 在sourceTree中选中错误的提交的下 ...