Binary Differences
https://csacademy.com/contest/archive/task/binary-differences
n个数,只有0和1,求所有子区间价值不相同的有多少中,价值是0的个数-1的个数
解法:0的贡献是1,1的贡献是-1,求出贡献的前缀和为s[i],利用上一个区间[l,r]求出当前区间[sum-r,sum-l],同时更新最大范围
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; int a[N];
int main()
{
int n,sum=;
scanf("%d",&n);
int l,r,tel,ter;
l=r=tel=ter=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i])sum--;
else sum++;
l=min(l,sum-ter);
r=max(r,sum-tel);
tel=min(tel,sum);
ter=max(ter,sum);
}
printf("%d\n",r-l+);
return ;
}
/******************** ********************/
Binary Differences的更多相关文章
- Beyond Compare脚本:命令行批量比较文件并生成html格式的差异报告
BComp.exe /silent /closescript /solo @E:\compareTest\BCbatch.txt text-report layout:side-by-side opt ...
- rsa Round #71 (Div. 2 only)
Replace A Time limit: 1000 msMemory limit: 256 MB You are given a string SS containing only letter ...
- 3 differences between Savepoints and Checkpoints in Apache Flink
https://mp.weixin.qq.com/s/nQOxsZUZSiPi7Sx40mgwsA 20181104 3 differences between Savepoints and Chec ...
- [Algorithms] Build a Binary Tree in JavaScript and Several Traversal Algorithms
A binary tree is a tree where each node may only have up to two children. These children are stored ...
- Sum of bit differences among all pairs
This article was found from Geeksforgeeks.org. Click here to see the original article. Given an inte ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
随机推荐
- unix_timestamp 和 from_unixtime 时间戳函数 区别
1.unix_timestamp 将时间转化为时间戳.(date 类型数据转换成 timestamp 形式整数) 没传时间参数则取当前时间的时间戳 mysql> select unix_time ...
- slurm使用
官方文档:https://slurm.schedmd.com/ 用户命令cheatsheet:https://slurm.schedmd.com/pdfs/summary.pdf 占用GPU sall ...
- Python 去掉文本中空行
pandas 操作csv文件时,一直报错,排查后发现csv文本中存在很多“空行”: So 需要把空行全部去掉: def clearBlankLine(): file1 = open('text1.tx ...
- JS-排序详解:冒泡排序、选择排序和快速排序
JS-排序详解-冒泡排序 说明 时间复杂度指的是一个算法执行所耗费的时间 空间复杂度指运行完一个程序所需内存的大小 稳定指,如果a=b,a在b的前面,排序后a仍然在b的前面 不稳定指,如果a=b,a在 ...
- Web Service简单demo
最近开发因需求要求需要提供Web Service接口供外部调用,由于之前没有研究过该技术,故查阅资料研究了一番,所以写下来记录一下,方便后续使用. 这个demo采用CXF框架进行开发,后续所提到的We ...
- Angular 学习笔记 :初识 $digest , $watch , $apply,浅析用法 。
传统的浏览器事件循环 :浏览器本身一直在等待事件,并作出响应.如果你点击一个button或者在input 中输入字符,我们在 JS 中 监听这些事件并设定了回调函数,那么这些事件被触发以后,回调函数就 ...
- Djngo Rest Framework
目录: 认证和授权 用户访问次数/频率 版本 解析器 序列化 分页 路由系统 视图 渲染器 一. 什么是Restful REST与技术无关,代表的是一种软件架构风格,REST是Representati ...
- day3-python-文件操作(2)
本文内容涉及python中的os模块和os.path模块的常用操作,这两个模块提供了与平台和操作系统无关的文件系统访问方法.os模块负责大部分的文件系统操作,包括:删除文件.重命名文件.遍历目录树等: ...
- change the color of a disabled TEdit?
change the color of a disabled TEdit? Author: P. Below Category: VCL {Question:How can I change the ...
- Delphi 正则表达式语法(7): 匹配转义字符
Delphi 正则表达式语法(7): 匹配转义字符 // ? 号的意义是匹配 0-1 次, 如果需要匹配 ? 怎么办 var reg: TPerlRegEx; begin reg := TPe ...