Codeforces Round #353 (Div. 2) C. Money Transfers 数学
2016-05-17 21:46
399 查看
C. Money Transfers
题目连接:
http://www.codeforces.com/contest/675/problem/CDescription
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
35 0 -5
Sample Output
1Hint
题意
现在有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; }
相关文章推荐
- caffe 学习(2)——基本原理
- 二维树状数组 BZOJ 1452 [JSOI2009]Count
- 剑指offer—第二章算法之快速排序
- 用ListView来显示解析jsonstr数组后的数据
- 树的计数 + prufer序列与Cayley公式 学习笔记
- Codeforces 675C Money Transfers 思维题
- js上拉加载、下拉刷新的插件
- 关于css中的水平垂直居中问题总结
- sublime中安装css 格式化插件
- javascript初学者笔记——关于innerHTML、innerText、outerHTML和outerText的区别
- jQuery -- 遍历
- CSS学习记录
- 用jspf文件做jsp页面的通用页面头
- [Javascript] Logging Pretty-Printing Tabular Data to the Console
- jQuery UI -- widgets各种部件的使用
- jQueryUI -- 交互事件(拖拽,修改尺寸,交换位置)
- Codeforces Round #353 (Div. 2)C. Money Transfers
- Effective JavaScript :第四章
- codeforces 675C Money Transfers(想法题)
- html 学习(css class选择器)