[luogu5426]Balancing Inversions
由于交换是相邻交换,所以分为两类:
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的更多相关文章
- 2019-08-25 纪中NOIP模拟A组
T1 [JZOJ6314] Balancing Inversions 题目描述 Bessie 和 Elsie 在一个长为 2N 的布尔数组 A 上玩游戏. Bessie 的分数为 A 的前一半的逆序对 ...
- [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 ...
- 【架构】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 ...
- POJ 1655 Balancing Act 树的重心
Balancing Act Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...
- 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 ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- UVA 12904 Load Balancing 暴力
Load Balancing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vi ...
- Load Balancing 折半枚举大法好啊
Load Balancing 给出每个学生的学分. 将学生按学分分成四组,使得sigma (sumi-n/4)最小. 算法: 折半枚举 #include <iostrea ...
- [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 ...
随机推荐
- 题解 「CTSC2018暴力写挂」
题目传送门 题目大意 给出两个大小为 \(n\) 的树,求出: \[\max\{\text{depth}(x)+\text{depth}(y)-\text{depth}(\text{LCA}(x,y) ...
- LinkedList-常用方法以及双向链表的理解
链表 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的. 链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点包括两 ...
- [Git系列] Git 基本概念
版本控制系统 版本控制系统是一种帮助软件开发者实现团队合作和历史版本维护的软件,一个版本控制系统应具备以下列出的这几个基本功能: 允许开发者并发工作: 不允许一个开发者覆写另一个开发者的修改: 保存所 ...
- [软工顶级理解组] Beta阶段项目展示
目录 团队成员 软件介绍 项目简介 预期典型用户 功能描述 预期目标用户数 用户反馈 团队管理 分工协作 项目管理 取舍平衡 代码管理 程序测试 代码规范 文档撰写 继续开发指导性 用户沟通 需求分析 ...
- linux centos7 修改默认网卡命名规则为eth0脚本
CentOS6之前基于传统的命名方式如:eth1,eth0.... Centos7提供了不同的命名规则,默认是基于固件.拓扑.位置信息来分配.这样做的优点是命名是全自动的.可预知的,缺点是比eth0. ...
- 解决svn异常报错“”cleanup failed to process the following paths …… previous operation has not finished”
参考高票答案https://stackoverflow.com/questions/10128201/subversion-stuck-due-to-previous-operation-has-no ...
- Spark面试题(二)
首发于我的个人博客:Spark面试题(二) 1.Spark有哪两种算子? Transformation(转化)算子和Action(执行)算子. 2.Spark有哪些聚合类的算子,我们应该尽量避免什么类 ...
- Machine learning(1-Introduction)
1.What is machine learning Field of study that gives computers the ability to learn without being ex ...
- 计算机网络传输层之TCP拥塞控制(慢开始与拥塞避免、快重传和快恢复)
文章转自:https://blog.csdn.net/weixin_43914604/article/details/105532044 学习课程:<2019王道考研计算机网络> 学习目的 ...
- Spring:所有依赖项注入的类型
一.前言 Spring文档严格只定义了两种类型的注入:构造函数注入和setter注入.但是,还有更多的方式来注入依赖项,例如字段注入,查找方法注入.下面主要是讲使用Spring框架时可能发生的类型. ...