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 ...
随机推荐
- Learn How To Cross Over The Wall
1.一个proxy的实现 http://blog.codingnow.com/2011/05/xtunnel.html 2.SOCK5 RFC http://www.faqs.org/rfcs/rfc ...
- this的思考
问题:JS中为什么要用this? 回答:因为this采用隐式“传递”一个对象的引用,所以可以将API设计得更加简洁和可复用 问题:JS中的this是什么? 背景:this是在运行时绑定的,this的上 ...
- git 遇到的问题
1.error: RPC failed; curl transfer closed with outstanding read data remaining 解决办法: git clone https ...
- LINQ不包含列表
var query=lista.Where(p=>!listb.Any(g=>p.id==g.id && p.no==g.no))
- git--公司中暂时用到的命令操作
使用的版本控制工具 git + phabricator arc Phabricator是软件开发管理工具,集成了code review, task tracking, version control ...
- gc摘要
1. Sun JDK 1.6 GC(Garbage Collector) http://bluedavy.com2010-05-13 V0.2 2010-05-19 V0.52010-06-01 V0 ...
- Linux命令(6/28)——declare/typeset命令
declare 与 typeset 命令是bash的内建命令,两者是完全一样的,用来声明shell变量,设置变量的属性. declare命令(别名typeset)属shell内建命令,用于申明shel ...
- linux比较两个文件的不同(6/21)
cmp 命令:比较任意两个类型的文件,且吧结果输出到标准输出,默认文件相同不输出,不同的文件输出差异 必要参数 -c 显示不同的信息-l 列出所有的不同信息-s 错误信息不提示 选择参数 -i< ...
- C# 中与等于 &= 操作符
MSDN说,x&=y等价于 x=x&y. 整型时&运算符,进行位运算. bool类型时,&运算符,当两边结果都为ture时,结果才为true.
- SVN使用—常用命令及避免冲突的方法
一.SVN启动 [root@localhost ~]# mkdir /data/svn [root@localhost ~]# svnadmin create /data/svn/test [root ...