C. Money Transfers
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Output

Print the minimum number of operations required to change balance in each bank to zero.

Examples
input
5 0 -5
output

input
-1 0 1 0
output

input
1 2 3 -6
output

Note

In the first sample, Vasya may transfer 5 from the first bank to the third.

In the second sample, Vasya may first transfer 1 from the third bank to the second, and then1 from the second to the first.

In the third sample, the following sequence provides the optimal answer:

  1. transfer 1 from the first bank to the second bank;
  2. transfer 3 from the second bank to the third;
  3. transfer 6 from the third bank to the fourth.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=100000; int a[maxn+10];
map<ll,int> mp;
int main()
{
int n;
while(~scanf("%d",&n))
{
mp.clear();
for(int i=1;i<=n;i++)
scanf("%d",&a[i]); int ans=n-1;ll sum=0;
for(int i=1;i<=n;i++)
{
sum+=a[i];
mp[sum]++;
ans=min(ans,n-1-(mp[sum]-1));
}
printf("%d\n",ans);
}
return 0;
}

  智商被碾压了,,,,,呜呜呜,,看看这篇题解基本就会了,,,让我再哭会儿

http://blog.csdn.net/acm_fighting/article/details/51437452

TTTTTTTTTTTTTTTTTTT CF 银行转账 图论 智商题的更多相关文章

  1. 11.07图论水题Test

    11.07图论水题Test 题目 描述 做法 \(BSOJ6378\) 在\(i\)位置可以到\(i+a_i\)或\(i+b_i\)求\(1\rightarrow n\)字典序最小路径 判可达性后贪心 ...

  2. 专题:CF图论杂题

    题目是来自HZW的博客(构造题我是各种不会...) Solved 1 / 1 A CodeForces 500A New Year Transportation Solved 1 / 1 B Code ...

  3. CF #355div2 D 宝藏与钥匙 dp 二维数组智商题

    D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...

  4. Codeforces Round #354 (Div. 2)-C. Vasya and String,区间dp问题,好几次cf都有这种题,看来的好好学学;

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. cf Canada cup A题

    A. Jumping Ball time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  7. 贪心 CF 332 C 好题 赞

    题目链接: http://codeforces.com/problemset/problem/332/C 题目意思: 有n个命令,要通过p个,某主席要在通过的p个中选择k个接受. 每个任务有两个值ai ...

  8. 山东省济南市历城第二中学——洛谷图论入门题--基本题必做 图的遍历—3.骑马修栅栏(fence)

    由于我这个破题提交了十四五遍,所以我决定写篇博客来记录一下. 这个题的题目描述是这样的 首先一看这个题我瞬间就想到了一笔画问题(欧拉回路). 对于能够一笔画的图,我们有以下两个定理. 定理1:存在欧拉 ...

  9. D9 图论综合题

    1.白银莲花池 LUOGU 2411 第一种思路:当然我们可以写三个bfs a掉这个题,这写下来一二百行要有了吧: 第二种:我们可以在一个bfs中维护所有的信息,一个方向数组,从起点开始,向八个方向扩 ...

随机推荐

  1. (4.16)mysql备份还原——物理备份之XtraBackup实践

    关键词:XtraBackup实践,物理备份,xtrabackup备份,innobackupex备份 [1]如何使用? [3]系列:innobackupex --help |more [4]系列:xtr ...

  2. python函数 -- 作用域,异常处理

    1.def语句和参数 python定义函数的关键词为def,格式如下: def 函数名([变元],[变元],....)          #保存在变元中的值,在函数返回后该变元就会被销毁了. 2.返回 ...

  3. flower 时区设置

    celery 搭配flower使用,flower默认使用的是UTC时间,那么如何在flower中使用当前城市的时间呢 我的环境 celery 3.1.25 ,python 3.69 1.在 app设置 ...

  4. 小记---------spark架构原理&主要组件和进程

    spark的主要组件和进程       driver (进程):     我们编写的spark程序就在driver上,由driver进程执行       master(进程):     主要负责资源的 ...

  5. 【DP 好题】hihoCoder #1520 古老数字

    题目链接 这道题的要点是状态转移的顺序. 要从低位向高位进行状态转移. Implementation string s; cin >> s; reverse(all(s)); int x, ...

  6. 在 sys.servers 中找不到服务器 '10.0.2.13'。请验证指定的服务器名称是否正确。

    工作中,因为需要,搭建同事的程序模块,附加了从同事那里拷过来的该程序使用的库.(C#.C/S..Net Framework4.0 .WCF.Win10.SQL Server 2014.VS2015) ...

  7. Idea中新建maven项目的目录结构

    maven项目的目录结构如下所示 pom.xml文件内容如下所示 <?xml version="1.0" encoding="UTF-8"?> &l ...

  8. 2019中山纪念中学夏令营-Day2[JZOJ]

    博客的开始,先聊聊代码实现: 每次比赛以后,要有归纳错误的习惯. 错误小结: 1.读入:scanf(“%c”)会读入回车和空格,但cin不会. 2.对于二维数组的输入,不能把m,n搞混了,会引起严重的 ...

  9. Django 模版语法与使用

    目录 Django 模版语法与使用 django模板语言介绍 (摘自官方文档) 链接 什么是模板? 模板语句的 注释 变量 {{ 变量 }} 点(.)在模板语言中有特殊的含义,用来获取对象的相应属性值 ...

  10. Ruby Rails正式学习:Ruby on Rails 做个演示项目吧,逐渐完善

    项目开始 一. 新建Rails项目 1. 修改一下Gemfile文件(简单修改一下) source 'https://rubygems.org' git_source(:github) { |repo ...