题目链接:

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 to 0.

Output

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

Examples
input
35 0 -5

output
1

input
4-1 0 1 0

output
2

input
41 2 3 -6

output
3

题意:
a[i]表示这个人欠第i个银行多少钱,这个人每次只能在相邻的银行之间转账,问最少要转多少次才能使所有的a[i]的值为0;其中1和n的相邻;
思路:
可以发现,最多需要n-1次,我们可以这样想,把每个a[i]都当成一个独立的集合,围成一个圈,我们的目标是把这些集合合并成0,且操作次数最少,那么怎么才能使操作最少且有达到目标呢?那就是要尽量使集合尽量多,因为合并一个数就至少需要一次操作,一次操作就会减少一个集合,而么操作次数就是集合的减少数目,所以最后的答案就是n-最多的集合数;现在就变成了怎么找最多的集合数目了;
跟前缀和有关,这个就相当于一个区间和为0,要把这个圈的数分成最多的集合就是找前缀和出现的最多的那个次数了;
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e5+25;
int n,a[N];
LL sum[N];
map<LL,int>mp;
int main()
{scanf("%d",&n);int ans=0;Riep(n){scanf("%d",&a[i]);sum[i]=sum[i-1]+a[i];mp[sum[i]]++;ans=max(ans,mp[sum[i]]);}cout<<n-ans<<"\n";return 0;
}

转载于:https://www.cnblogs.com/zhangchengc919/p/5510628.html

codeforces 675C C. Money Transfers(贪心)相关推荐

  1. 【CodeForces - 675C】Money Transfers(思维,前缀和)

    题干: There are n banks in the city where Vasya lives, they are located in a circle, such that any two ...

  2. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  3. Codeforces 675C Money Transfers (思维题)

    There are n banks in the city where Vasya lives, they are located in a circle, such that any two ban ...

  4. Codeforces 671E Organizing a Race (贪心、线段树)

    题目链接 https://codeforces.com/contest/671/problem/E 题解 完全不会做--基本是抄lk的代码 ruogu的题解: https://www.luogu.co ...

  5. CodeForces - 1529E Trees of Tranquillity(贪心+线段树)

    题目链接:https://vjudge.net/problem/CodeForces-1529E 题目大意:给出两棵根节点为 111 的树,分别称为 AAA 树和 BBB 树,现在通过两棵树可以构造出 ...

  6. [CodeForces 1603C] Extreme Extension(贪心 + 数论分块优化dp)

    problem CodeForces solution observation1:\text{observation1}:observation1: 对于一个非空子段 [l,r][l,r][l,r], ...

  7. codeforces 416C C. Booking System(贪心)

    题目链接: codeforces 416C 题目大意: 给出n个请求,每个请求包括客人数量和支付金额,再给出m个桌子,包括桌子大小,问如何安排才能最大盈利.给出最大盈利和一个能够最大盈利的方案. 题目 ...

  8. Codeforces 540B School Marks 【贪心构造】

    题目链接:Codeforces 540B School Marks Little Vova studies programming in an elite school. Vova and his c ...

  9. CodeForces 508E Arthur and Brackets 贪心

    题目: E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input s ...

  10. Codeforces 797C Minimal string【贪心】

    题意: 给出了字符串s的内容,字符串t,u初始默认为空,允许做两种操作: 1.把s字符串第一个字符转移到t字符串最后. 2.把t字符串最后一个字符转移到u字符串最后. 最后要求s.t字符串都为空,问u ...

最新文章

  1. Postman 调试技巧
  2. 关于一个枚举IE表单的DLL,编译无错,但是得不到想到的结果。
  3. c++17(2)-枚举类enum class
  4. ABB 压包指令PackRawBytes 解包指令UnpackRawBytes
  5. Python档案袋( 命令行操作 及 Os与Shutil文件操作补充 )
  6. 一位工作了10年的C++程序员总结出这些忠告
  7. Oauth2.0认证---授权码模式
  8. wcf编程用什么工具_四个强大的自学编程网站工具,用的人都是学习欲望很强!...
  9. 在html页面中使用模板继承,HTML静态模板的继承
  10. 由浅入深解释JS执行机制 EventLoop
  11. connection error mysql_MySQL ConnectionError 安装错误 解决方法
  12. 数据结构(二)之二叉树
  13. [译] 为什么给设计定义 UX、UI、CX、IA、IxD 和其他类型的头衔是愚蠢的行为
  14. Membership学习(一) Membership介绍[xgluxv]
  15. centos 中如何将python更新到最新的版本
  16. Vue开发问题—— mescroll滚动懒加载,以及保留滚动条位置。
  17. JavaSE-day01
  18. 如何知晓计算机的字长,计算机字长是什么意思,如何判断指令格式是单字长还是双字长...
  19. swagger swagger-codegen 使用
  20. HTTP状态码大全(整理)

热门文章

  1. HDU 4296 building
  2. Android应用开发提高篇(1)-----获取本地IP
  3. python使用struct处理二进制数据(使用pack和unpack进行打包和解包)
  4. 计算机课用英语怎么说cute,cute英语怎么读
  5. android kk界面旋转流程_【技术浅析】基于Android的五轴联动数控系统设计
  6. java web 开发环境布置学习笔记1
  7. Zookeeper C API 指南五(同步 API 介绍)
  8. STC89C52是51单片机吗?
  9. c++ gdb 绑定源码_gdb调试g++ -g生成文件,list后不能看到源代码
  10. 【渝粤教育】国家开放大学2018年秋季 0631-21T动物常见病防治 参考试题