由于交换是相邻交换,所以分为两类:
1.左右区间内部交换,那么一定会让逆序对数量$\pm 1$,也就是说如果没有左右区间之间交换,那么答案就是$|ansL-ansR|$(ans表示逆序对数量)
2.左右区间之间交换,考虑枚举左边最终有多少个1,不妨假设比原来多(原来少一样,但不能都异或1之后重复一遍,会错的),首先一定尽量交换左边的最右边的若干个0和右边最左边的若干个1,然后快速的去维护两边的逆序对数量
维护方式很简单,由于假如左边如果改变了一个点,说明它的右边一定都是同样的数字,所以不用线段树,只需要维护左边的前缀和即可(右边同理)

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 200005
4 int n,a[N],sum[N];
5 long long s,ans1,ans2,ans;
6 int pre(int k){
7 k--;
8 while ((k)&&(!a[k]))k--;
9 return k;
10 }
11 int nex(int k){
12 k++;
13 while ((k<=2*n)&&(a[k]))k++;
14 return k;
15 }
16 void calc1(){
17 s=ans1=ans2=0;
18 for(int i=1;i<=n;i++)
19 if (!a[i])ans1+=sum[i-1];
20 for(int i=2*n;i>n;i--)
21 if (a[i])ans2+=sum[i+1];
22 ans=min(ans,abs(ans1-ans2));
23 for(int x=pre(n+1),y=nex(n);(x)&&(y<=2*n);x=pre(x),y=nex(y)){
24 s+=y-x;
25 ans1+=sum[x];
26 ans2+=sum[y];
27 ans=min(ans,s+abs(ans1-ans2));
28 }
29 }
30 void calc2(){
31 s=ans1=ans2=0;
32 for(int i=1;i<=n;i++)
33 if (a[i])ans1+=sum[i-1];
34 for(int i=2*n;i>n;i--)
35 if (!a[i])ans2+=sum[i+1];
36 for(int x=pre(n+1),y=nex(n);(x)&&(y<=2*n);x=pre(x),y=nex(y)){
37 s+=y-x;
38 ans1-=sum[x];
39 ans2-=sum[y];
40 ans=min(ans,s+abs(ans1-ans2));
41 }
42 }
43 int main(){
44 scanf("%d",&n);
45 for(int i=1;i<=2*n;i++)scanf("%d",&a[i]);
46 for(int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i];
47 for(int i=2*n;i>n;i--)sum[i]=sum[i+1]+(a[i]^1);
48 ans=1LL*n*n;
49 calc1();
50 for(int i=1;i<=2*n;i++)a[i]^=1;
51 calc2();
52 printf("%lld",ans);
53 }

[luogu5426]Balancing Inversions的更多相关文章

  1. 2019-08-25 纪中NOIP模拟A组

    T1 [JZOJ6314] Balancing Inversions 题目描述 Bessie 和 Elsie 在一个长为 2N 的布尔数组 A 上玩游戏. Bessie 的分数为 A 的前一半的逆序对 ...

  2. [UCSD白板题] Number of Inversions

    Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...

  3. 【架构】How To Use HAProxy to Set Up MySQL Load Balancing

    How To Use HAProxy to Set Up MySQL Load Balancing Dec  2, 2013 MySQL, Scaling, Server Optimization U ...

  4. POJ 1655 Balancing Act 树的重心

    Balancing Act   Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...

  5. CF# Educational Codeforces Round 3 C. Load Balancing

    C. Load Balancing time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心

    C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...

  7. UVA 12904 Load Balancing 暴力

    Load Balancing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vi ...

  8. Load Balancing 折半枚举大法好啊

    Load Balancing 给出每个学生的学分.   将学生按学分分成四组,使得sigma (sumi-n/4)最小.         算法:   折半枚举 #include <iostrea ...

  9. [zz] pgpool-II load balancing from FAQ

    It seems my pgpool-II does not do load balancing. Why? First of all, pgpool-II' load balancing is &q ...

随机推荐

  1. Serverless 是一种思想状态

    来源 | Serverless 公众号:作者 | Ben Kehoe:译者 | donghui 函数不是重点 如果你因为喜欢 Lambda 而选择 Serverless,你这样做的原因是错误的.如果你 ...

  2. 题解 Weak in the Middle

    题目传送门 Description 有一个长度为 \(n\) 的序列 \(a_{1,2,...,n}\) ,每次可以删掉 \(a_i\),当 \(\min(a_{i-1},a_{i+1})>a_ ...

  3. vue 2.0源码学习笔记—new Vue ( Vue 初始化过程 )

    new Vue(Vue 初始化) 一个vue实例化到底经历了什么?已下是博主自己的总结,不正确的地方请指出,谢谢~ 一.简述 从使用角度来看,挂载的顺序如下 1. $slots 2. $scopedS ...

  4. 力扣 - 剑指 Offer 45. 把数组排成最小的数

    题目 剑指 Offer 45. 把数组排成最小的数 思路1 将整数数组转化成字符串数组 然后使用Arrays工具类的sort方法帮助我们排序 代码 class Solution { public St ...

  5. 【二食堂】Beta - 发布声明

    Beta - 发布声明 新功能 在Beta阶段,图谱方面的新功能有:自定义关系的添加与删除.实体查找.实体名称的修改.实体之间关系的修改.新增了项目创建与删除功能,此外还增加了好友系统,可以实现好友的 ...

  6. AlertManager集群搭建

    AlertManager集群搭建 一.AlertManager集群搭建 1.背景 2.机器 3.集群可用配置 4.alertmanager启动脚本 1.127.0.0.1:9083 机器启动脚本 2. ...

  7. 莱特币(LTC)开发环境搭建

    Linux系统下搭建莱特币LTC开发环境 1.简介 2.LTC 客户端下载 3.解压到一个固定的目录中 4.启动客户端 4.1.参数解释: 4.2.更多详细的配置 5.执行命令测试一下 6.获取莱特币 ...

  8. Zabbix 5.0:监控阿里云RDS

    Blog:博客园 个人 由于近期压测,需要频繁登录阿里云查看RDS监控,每次登录查看监控步骤较为繁琐,故将监控接入到zabbix. 概述 由于阿里云已做了RDS的监控,我们只需要通过阿里云SDK把这些 ...

  9. Noip模拟37 2021.8.12

    T1 数列 真是考场上不是数学的乱推柿子,想定理,是数学的没想出来.. 比较悲伤... 列柿子不用动脑子,就是没有想出来$EXgcd$解不定方程,淦.. 解处一组解后利用比较显然的性质: $x+\fr ...

  10. 万能构造解决Rolle中值问题

    只要原函数是两个函数的乘积形式,皆可此构造.