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 数学的更多相关文章

  1. Codeforces Round #353 (Div. 2) C Money Transfers

    题目链接: http://www.codeforces.com/contest/675/problem/C 题意: 给一个数组,每个数与他相邻的数相连,第一个与最后一个相连,每个数的数值可以左右移动, ...

  2. Codeforces Round #353 (Div. 2) C. Money Transfers (思维题)

    题目链接:http://codeforces.com/contest/675/problem/C 给你n个bank,1~n形成一个环,每个bank有一个值,但是保证所有值的和为0.有一个操作是每个相邻 ...

  3. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  4. Codeforces Round #353 (Div. 2)

    数学 A - Infinite Sequence 等差数列,公差是0的时候特判 #include <bits/stdc++.h> typedef long long ll; const i ...

  5. 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的最短距离之 ...

  6. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  7. Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)

    题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...

  8. 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E

    http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...

  9. 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 ...

随机推荐

  1. CentOS安装ANT

    第1步:下载ant apache-ant-1.9.2-bin.tar.gz http://archive.apache.org/dist/ant/binaries/ 第2步:解压 tar -zxvf ...

  2. python 结构化数据解析

    # -*- coding: utf-8 -*- # @Time : 2018/8/31 14:32 # @Author : cxa # @File : glomtest.py # @Software: ...

  3. 002_安装第三方APP好的站点及解除安全与隐私限制

    一.解除安全与隐私限制的任何来源. http://bbs.feng.com/read-htm-tid-10714286.html 接下来,我们就打开终端,然后输入以下命令:   sudo spctl ...

  4. IOS使用xcode编译代码

    一.安装xcode 在app store中搜索xcode然后点击安装即可. 二.创建第一个app 1.启动xcode,单击Lauchpad 2.单击xcode启动 3.单击“Create a new ...

  5. ubuntu 休眠后窗口边缘出现花边的解决方案

    可以确定是nvidia显卡的问题,详细的解决方案请参见:这里 临时的解决方案: compize --replace 永久性的解决方案: sudo add-apt-repository ppa:grap ...

  6. hdu 5475 模拟计算器乘除 (2015上海网赛H题 线段树)

    给出有多少次操作 和MOD 初始值为1 操作1 y 表示乘上y操作2 y 表示除以第 y次操作乘的那个数 线段树的叶子结点i 表示 第i次操作乘的数 将1替换成y遇到操作2 就把第i个结点的值 替换成 ...

  7. CentOS 7下安装Python3.6和pip

    一.安装python3.6 1.1.安装python3.6需要依赖包 yum install openssl-devel bzip2-devel expat-devel gdbm-devel read ...

  8. js把json数据转化成树形数据

    /*转化函数*/ function(data, attributes) { let resData = data; let tree = []; for(let i = 0; i < resDa ...

  9. PHP isset和empty 详细比较

    找了几篇博文,这应该是说的最清楚的.链接如下:http://blog.chinaunix.net/uid-25311424-id-3966622.html

  10. Wannafly挑战赛9 A - 找一找

    链接:https://www.nowcoder.com/acm/contest/71/A来源:牛客网 题目描述 给定n个正整数,请找出其中有多少个数x满足:在这n个数中存在数y=kx,其中k为大于1的 ...