TTTTTTTTTTTTTTTTTTT CF 银行转账 图论 智商题
1 second
256 megabytes
standard input
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.
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.
Print the minimum number of operations required to change balance in each bank to zero.
5 0 -5
-1 0 1 0
1 2 3 -6
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:
- transfer 1 from the first bank to the second bank;
- transfer 3 from the second bank to the third;
- 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;
}
智商被碾压了,,,,,呜呜呜,,看看这篇题解基本就会了,,,让我再哭会儿
TTTTTTTTTTTTTTTTTTT CF 银行转账 图论 智商题的更多相关文章
- 11.07图论水题Test
11.07图论水题Test 题目 描述 做法 \(BSOJ6378\) 在\(i\)位置可以到\(i+a_i\)或\(i+b_i\)求\(1\rightarrow n\)字典序最小路径 判可达性后贪心 ...
- 专题:CF图论杂题
题目是来自HZW的博客(构造题我是各种不会...) Solved 1 / 1 A CodeForces 500A New Year Transportation Solved 1 / 1 B Code ...
- CF #355div2 D 宝藏与钥匙 dp 二维数组智商题
D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...
- 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 ...
- cf Canada cup A题
A. Jumping Ball time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 贪心 CF 332 C 好题 赞
题目链接: http://codeforces.com/problemset/problem/332/C 题目意思: 有n个命令,要通过p个,某主席要在通过的p个中选择k个接受. 每个任务有两个值ai ...
- 山东省济南市历城第二中学——洛谷图论入门题--基本题必做 图的遍历—3.骑马修栅栏(fence)
由于我这个破题提交了十四五遍,所以我决定写篇博客来记录一下. 这个题的题目描述是这样的 首先一看这个题我瞬间就想到了一笔画问题(欧拉回路). 对于能够一笔画的图,我们有以下两个定理. 定理1:存在欧拉 ...
- D9 图论综合题
1.白银莲花池 LUOGU 2411 第一种思路:当然我们可以写三个bfs a掉这个题,这写下来一二百行要有了吧: 第二种:我们可以在一个bfs中维护所有的信息,一个方向数组,从起点开始,向八个方向扩 ...
随机推荐
- Storm提交Topology报错:Found multiple defaults.yaml resources.
Storm提交Topology运行方式分为本地和集群运行两种,其中集群运行需要将程序打包并把jar包复制到集群,通过以下方式执行: bin/storm jar /opt/run/storm-demo- ...
- Vue的响应系统
随着 Vue 3.0 Pre Alpha 版本的公布,我们得以一窥其源码的实现.Vue 最巧妙的特性之一是其响应式系统,而我们也能够在仓库的 packages/reactivity 模块下找到对应的实 ...
- python基础之内置函数和匿名函数
内置函数 学习函数以后多了很多概念,例如函数的命名空间,函数的作用域,函数的调用等等,函数的作用就是为了实现某些功能而方便以后可以调用,内置函数就是这样的一些公共的函数,被称为内置函数. 我们就一 ...
- API 网关性能比较:NGINX vs. ZUUL vs. Spring Cloud Gateway vs. Linkerd
前几天拜读了 OpsGenie 公司(一家致力于 Dev & Ops 的公司)的资深工程师 Turgay Çelik 博士写的一篇文章(链接在文末),文中介绍了他们最初也是采用 Nginx 作 ...
- mac下开启phpredis扩展
下载 官网下载php合适的版本:http://pecl.php.net/package/redis 这里我的php版本:7.1.23,下载的phpredis版本:5.0.0 配置安装 解包.重命名 s ...
- Zookeeper VS Chubby
目录 区别的根源 1)一致性 2)Client Cache vs No Cache 总结 参考资料 区别的根源 一个设计良好的系统应该是围绕并为其设计目标服务的. Chubby:p ...
- css动画之旋转翻牌效果
1.我们先设置两个盒子大小,颜色等等,然后定位重叠在一起,最后再进行动画设置 例子如下: <style> .box { height: 300px; width: 300px; posit ...
- yarn的安装步骤
yarn依赖npm,是npm中的一个包 https://www.npmjs.com/package/yarn 1.安装 npm i -g -yarn 2.测试是否安装成功 yarn --vresion ...
- MySQL--高性能MySQL笔记二
人们通常使用varchar(15):来存储IP地址,然而它们其实是32位无符号整数,不是字符串,所以应该使用无符号整数存储IP地址,MySQL 提供 INET_ATON() 和 INET_NTOA() ...
- python的加密方式
MD5加密 这是一种使用非常广泛的加密方式,不可逆的,在日常字符串加密中经常会用到,下面我简单介绍一下这种方式,主要用到Python自带的模块hashlib,测试代码如下,先创建一个md5对象,然后直 ...