Codeforces Round #353 (Div. 2) C. Money Transfers 数学
C. Money Transfers
题目连接:
http://www.codeforces.com/contest/675/problem/C
Description
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers ai ( - 109 ≤ ai ≤ 109), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all ai is equal to 0.
Output
Print the minimum number of operations required to change balance in each bank to zero.
Sample Input
3
5 0 -5
Sample Output
1
Hint
题意
现在有n个银行,每个银行有a[i]元,可以像周围的银行转移钱
你的目标是使得所有银行钱的个数都是0,问你最少操作多少次
题解:
原题,hdu 2590
考虑一个连续的段,长度为l,如果该段的区间和为0的话,那么就操作l-1次就够了
现在有n个数,你分成了k段,那么你就需要操作n-k次
那么只要k最大就好了
然后暴力枚举前缀和相等的,取最大就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int a[maxn];
map<long long,long long>cnt;
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
long long sum=0,mx=0;
for(int i=1;i<=n;i++)
{
sum+=a[i];
mx=max(mx,++cnt[sum]);
}
cout<<n-mx<<endl;
}
Codeforces Round #353 (Div. 2) C. Money Transfers 数学的更多相关文章
- Codeforces Round #353 (Div. 2) C Money Transfers
题目链接: http://www.codeforces.com/contest/675/problem/C 题意: 给一个数组,每个数与他相邻的数相连,第一个与最后一个相连,每个数的数值可以左右移动, ...
- Codeforces Round #353 (Div. 2) C. Money Transfers (思维题)
题目链接:http://codeforces.com/contest/675/problem/C 给你n个bank,1~n形成一个环,每个bank有一个值,但是保证所有值的和为0.有一个操作是每个相邻 ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- Codeforces Round #353 (Div. 2)
数学 A - Infinite Sequence 等差数列,公差是0的时候特判 #include <bits/stdc++.h> typedef long long ll; const i ...
- Codeforces Round #353 (Div. 2) E. Trains and Statistic 线段树+dp
题目链接: http://www.codeforces.com/contest/675/problem/E 题意: 对于第i个站,它与i+1到a[i]的站有路相连,先在求所有站点i到站点j的最短距离之 ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)
题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...
- 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E
http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...
- Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心
E. Trains and Statistic 题目连接: http://www.codeforces.com/contest/675/problem/E Description Vasya comm ...
随机推荐
- Python标准库笔记(8) — pprint模块
struct模块提供了用于在字节字符串和Python原生数据类型之间转换函数,比如数字和字符串. Python版本: 2.x & 3.x 该模块作用是完成Python数值和C语言结构体的Pyt ...
- 解决修改表结构,添加外键时出现“约束冲突”的错误
由于表结构更改,使用新建表,现有部分表需要更改外键,将引用更改到新建表的相应字段.在更改过程中,部分表出现如下错误提示: ALTER TABLE 语句与 COLUMN FOREIGN KEY 约束 ' ...
- DenseNet笔记
一.DenseNet的优点 减轻梯度消失问题 加强特征的传递 充分利用特征 减少了参数量 二.网络结构公式 对于每一个DenseBlock中的每一个层, [x0,x1,…,xl-1]表示将0到l-1层 ...
- 一文看懂python主要应用领域或应用场景
Python简介 Python(英国发音:/ˈpaɪθən/美国发音:/ˈpaɪθɑːn/),是一种面向对象的解释型计算机程序设计语言,由荷兰人GuidovanRossum于1989年发明,第一个公开 ...
- Python匿名函数详解
python 使用 lambda 来创建匿名函数. lambda这个名称来自于LISP,而LISP则是从lambda calculus(一种符号逻辑形式)取这个名称的. 在Python中,lambda ...
- OSI & TCP/IP 参考模型
OSI参考模型的结构 OSI划分七层结构的基本原则 网中各结点都具有相同的层次: 不同结点的同等层具有相同的功能: 同一结点内相邻层之间通过接口通信: 每一层可以使用下层提供的服务,并向其上层提供服务 ...
- hdu 4559 涂色游戏(SG)
在一个2*N的格子上,Alice和Bob又开始了新游戏之旅. 这些格子中的一些已经被涂过色,Alice和Bob轮流在这些格子里进行涂色操作,使用两种涂色工具,第一种可以涂色任意一个格子,第二种可以涂色 ...
- javascript大神修炼记(6)——OOP思想(继承)
读者朋友们大家好,我们今天这一讲就接着前面的封装继续讲解,今天就是在前面内容上面的升级,OOP思想中的继承,我们就先来解释一下继承到底是什么意思,我们在什么地方会用到继续. 继承就是,后代继续祖先的一 ...
- USACO 6.3 Cowcycles
CowcyclesOriginally by Don Gillies [International readers should note that some words are puns on co ...
- PLSQL Developer个性化设置
1)代码自动完成 和讨厌的.才后出现提示说88,我用快捷键任意呼唤. Tools->Preferences->User Interface->Key Configuration.找到 ...