题目链接

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. java课程之团队开发冲刺阶段2.5

    总结昨天进度: 1.昨天对课前提醒的基本框架已经搭好,剩下的就是对如何提醒进行设置 遇到的困难: 1.在一些细节地方,代码有点犯浑,将Preferences的文件写错了,导致在用switch开关得到时 ...

  2. pip2 install protobuf==2.6.1

    [libprotobuf FATAL google/protobuf/stubs/common.cc:61] This program requires version 3.5.0 of the Pr ...

  3. loback.xml 在idea中代码自动完成

    1.下载xsd文件 2.idea添加xsd文件 URI: http://ch.qos.logback/xml/ns/logback File: D:\env\plugins\logback\logba ...

  4. 【2017西安邀请赛:A】XOR(线段树+线性基)

    前言:虽然已经有很多题解了,但是还是想按自己的理解写一篇. 思路:首先分析题目 一.区间操作 —— 线段树 二.异或操作 —— 线性基 这个两个不难想,关键是下一步的技巧 “或”运算 就是两个数的二进 ...

  5. C语言笔记 15_标准库&locale&math&setjmp&signal&stdarg&stddef

    <locale.h> 简介 locale.h 头文件定义了特定地域的设置,比如日期格式和货币符号.接下来我们将介绍一些宏,以及一个重要的结构 struct lconv 和两个重要的函数. ...

  6. 网页嵌 activeXForm 中显示fastReport

  7. ArrayList 和 LinkedList 比较

    是否保证线程安全? ArrayList 和 LinkedList 都是不同步的,也就是不保证线程安全. 底层数据结构区别? Arraylist 底层使用的是Object数组:LinkedList 底层 ...

  8. tomcat8.5的安装、卸载、配置和部署

    安装和卸载 下载 http://tomcat.apache.org/ 环境变量 1.点击此电脑 右键—>属性. 2.创建变量名为CATALINA_HOME,的值为所解压安装tomcat的本地目录 ...

  9. 解决 urxvt “unknown terminal type.”

    登录到远程服务器上后,有时执行某些命令会提示unknown terminal type. 这是因为远程ssh不支持urxvt,执行 export TERM=xterm-256color 或者在远程主机 ...

  10. Linux--CentOS7启用网卡

    参考 http://www.centoscn.com/CentosBug/softbug/2015/0823/6039.html Centos7默认是不启用有线网卡的,需要手动开启. 改完保存退出