You are given a binary array† of length n. You are allowed to perform one operation on it at most once. In an operation, you can choose any element and flip it: turn a 0 into a 1 or vice-versa.

What is the maximum number of inversions‡ the array can have after performing at most one operation?

† A binary array is an array that contains only zeroes and ones.

‡ The number of inversions in an array is the number of pairs of indices i,j such that iaj.

Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer n (1≤n≤2⋅105) — the length of the array.

The following line contains n space-separated positive integers a1, a2,…, an (0≤ai≤1) — the elements of the array.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, output a single integer — the maximum number of inversions the array can have after performing at most one operation.

Example
inputCopy
5
4
1 0 1 0
6
0 1 0 0 1 0
2
0 0
8
1 0 1 1 0 0 0 1
3
1 1 1
outputCopy
3
7
1
13
2
Note
For the first test case, the inversions are initially formed by the pairs of indices (1,2), (1,4), (3,4), being a total of 3, which already is the maximum possible.

For the second test case, the inversions are initially formed by the pairs of indices (2,3), (2,4), (2,6), (5,6), being a total of four. But, by flipping the first element, the array becomes 1,1,0,0,1,0, which has the inversions formed by the pairs of indices (1,3), (1,4), (1,6), (2,3), (2,4), (2,6), (5,6) which total to 7 inversions which is the maximum possible.

分析

  1. 题意:找一个序列中的1 0个数(可以不连续),序列每次可以翻转一个数,然后找拥有最大的 1 0个数,当然也可以不翻转,只要1 0 个数所有情况最大即可;
  2. 一开始直接暴力TLE了,然后看网上题解用前缀、后缀来解决;前缀数组fro存每个位置的数之前的所有0、1的个数,后缀数组back存储每个位置的数之后的所有0、1的个数;
  3. 先找出来不进行翻转的答案数ans,然后逐个枚举,分别去判断翻转一个数的情况;
  • 当前要翻转的数原来为1 (a[i]=1) 时,那么需要减去其位置后面的0的个数,因为1将要走了,后面的0和1组成的cp要分开了,需要加上其位置前面1的个数,因为a[i]=1翻转成0,使前面的1可以和其组cp;
  • 当前要翻转的数原来是0时(将要变为1),那么需要加上其位置后面0的个数,因为新的1来了,能组cp了,需要减去其位置前面1的个数,因为0已经走了(0将翻转为1),所以这对cp要分开了;
  1. 细节:需要注意翻转时,是对原方案数的操作,所以需要一个临时变量存刚开始一个数都没被翻转的ans,后面翻转后需要减、加操作都是对temp操作的,而不是ans(因为ans在变,翻转后可能使ans增大),以及结果需要用long long存储;
  2. 这道题有的地方卡了好久,思维真的不太行啊,还带多练啊,向浩哥看齐;
#include<bits/stdc++.h>using namespace std;typedef long long ll;int t, n;
int a[200005];
int fro[2][200005];//前缀
int back[2][200005];//后缀int main() {cin >> t;while (t--) {cin >> n;for (int i = 1; i <= n; ++i) {cin >> a[i];}//求前缀,求i前面1的个数和0的个数int one = 0, zero = 0;for (int i = 1; i <= n; ++i) {fro[1][i] = one;fro[0][i] = zero;if (a[i])one++;elsezero++;}//求后缀,每个位置后面的1和0的个数one = 0, zero = 0;for (int i = n; i >= 1; --i) {back[1][i] = one;back[0][i] = zero;if (a[i])one++;elsezero++;}ll ans = 0;//不翻转时候for (int i = 1; i <= n; ++i) {if (a[i]) {ans += back[0][i];}}//逐个翻转判断ll temp = ans;//不翻转的方案数for (int i = 1; i <= n; ++i) {//把1变为0,方案数temp 减1后面是0的个数,加1前面是1的个数if (a[i]) {ans = max(ans, (temp - back[0][i] + fro[1][i]));//这里是对temp操作的,在他基础上变化方案数} else {//0变为1,方案数 减0前面1的个数,加0后面0的个数ans = max(ans, (temp - fro[1][i] + back[0][i]));}}cout << ans << endl;}return 0;
}

E. Binary Inversions——前缀+后缀相关推荐

  1. 【POJ - 2752】Seek the Name, Seek the Fame (KMP,公共前缀后缀长度及个数)

    题干: The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked t ...

  2. java中获得词根的方法_分析在各大考纲词汇中同时拥有前缀后缀和词根的词(一)...

    CET4.CET6.GRE.IELTS.TOEFL.考研英语总的词汇量为14055,有11544个单词含有词根信息,有5526个单词含有前缀信息,有9525个单词含有后缀信息.那么有多少个单词同时拥有 ...

  3. Rvit中添加尺寸标注的前缀后缀和【一键尺寸定位标注】

    一.Rvit中如何添加尺寸标注的前缀后缀 在进行尺寸标注尤其是多段连续的尺寸标注时常会用到诸如:3x2000=6000的标注样式,在Revt中应如何实现? 我们可以采用以下方法来解决: 1.如图1所示 ...

  4. 字符串最长公共前缀后缀长度

    首先,要了解两个概念:"前缀"和"后缀". "前缀"指除了最后一个字符以外,一个字符串的全部头部组合:"后缀"指除了第一 ...

  5. 我的世界服务器世界前缀修改,大佬教你如何给玩家添加名字前缀后缀

    下面小编为大家带来我的世界添加名字前缀后缀攻略,相信好多玩家都非常的想知道,那么想知道的玩家一定要看下面的内容! 作者:Msure 很多玩家见过有些地图中有玩家名字可以获得前缀,如图: 那么下面给出详 ...

  6. 【MySQL】利用Concat()函数在某一列字段批量添加前缀后缀

    一.MySQL中的Concat()函数 1. 含义: 将多个字符串连接成一个字符串. 2. 语法: concat(str1, str2,-) 返回结果为连接参数产生的字符串,如果有任何一个参数为nul ...

  7. jQuery EasyUI/TopJUI基本的数字输入框(保留两位小数,带前缀后缀...)

    jQuery EasyUI/TopJUI基本的数字输入框(保留两位小数,带前缀后缀...) numberbox(数值输入框) HTML required:必填字段,默认为false:prompt:显示 ...

  8. 批量处理文件名前缀后缀

    准备工作 使用python编程语言. 使用subtextline编写并运行python. 需求 文件名有相同命名的前缀后缀或中间字符,需要去除相应的字符. 我是从公司内网导出数据,经去水印处理,每个文 ...

  9. c语言中缀表达式求值_数据结构考研笔记之栈与队列(四)栈与队列应用括号匹配、中缀表达式转前缀后缀问题...

    文字:独木 排版:独木 图片:独木 栈与队列 1.括号匹配问题 栈 例题1 例题2-----不匹配例题1 例题3-----不匹配例题2 2. 表达式求值问题 例题 1.中缀表达式转前缀表达式 2.中缀 ...

最新文章

  1. 【编程】char unsigned int float double long 字节数
  2. Ubuntu18.04安装Docker
  3. elenium使用IE浏览器的配置方法
  4. cesium three性能比较_mapboxgl + three 动画 — 网格热图
  5. LeetCode 277. 搜寻名人(思维题)
  6. 4.安卓基础之Activity跳转动画
  7. mysql-优化班学习-8-20170606-MySQL索引
  8. ajax get post
  9. Matlab实现批量修改文件名
  10. docker 权限问题 Got permission denied while trying to connect to the Docker daemon socket at 。。。
  11. C语言实现顺序表(数据结构)
  12. 中海达数据怎么转rinex_GPS_OEM原始数据向Rinex格式转换的方法[1]
  13. c语言台阶,关于C语言跳台阶问题的解决方法
  14. 树莓派安装第三方操作系统
  15. setuptools-scm was unable to detect version for‘…/…/某git包‘
  16. 瀑布流网站数据如何采集解决思路方法
  17. YourBatman表白了,在Java 27岁生日这天
  18. 最高百万年薪,全国多家知名互联网/游戏公司热招 Cocos 人才丨9月岗位
  19. 配置多SessionFactory
  20. php 事件,php的事件处理机制(回调函数)

热门文章

  1. 计算机文件丢失系统无法启动,文件损坏或丢失windows无法启动_windows无法启动文件损坏解决方法...
  2. 什么是tuscany
  3. Windows 10 右键 在此处打开 CMD
  4. 华师大 OJ 3053
  5. python参考书推荐--父与子的编程之旅
  6. setMouseTracking(true)
  7. 农场(JQuery版)
  8. WIN10阻止OA附件打开
  9. Dorabot蓝胖子招聘 薪资Open谈|3D视觉、机器人软件、SLAM算法工程师等岗位
  10. win10 搜索框无法使用