Equilibrium point
Given an array A your task is to tell at which position the equilibrium first occurs in the array. Equilibrium position in an array is a position such that the sum of elements below it is equal to the sum of elements after it.
Input:
The first line of input contains an integer T denoting the no of test cases then T test cases follow. First line of each test case contains an integer N denoting the size of the array. Then in the next line are N space separated values of the array A.
Output:
For each test case in a new line print the position at which the elements are at equilibrium if no equilibrium point exists print -1.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
1
1
5
1 3 5 2 2
Output:
1
3
Explanation:
1. Since its the only element hence its the only equilibrium point
2. For second test case equilibrium point is at position 3 as elements below it (1+3) = elements after it (2+2)
这个题目的题意就是找到一个平衡点,它之前的所有元素和等于它之后的所有元素和,如果不存在就输出“-1”。
解题思路:
首先算出数组总和,然后从第一个元素开始假设它是“当前平衡点”,计算其左右之和是否相等,如果相等,则输出该平衡点。否则假设下一个元素是“当前平衡点”进行依次遍历。
巧妙的是,我选择suml当作平衡点左边的元素之和,sumr起初是表示数组全部元素之和,但是在程序中可以利用它每次减去“当前平衡点数值大小”用来表示“当前平衡点右边元素之和”。然后对比即可,使得整个程序的时间复杂度得到了很好的控制。
下面是我的代码实现:
#include <iostream> using namespace std; int main() { int T;cin>>T; int N; int *arr; while(T--) { cin>>N; arr=new int[N]; int i,j,suml=0,sumr=0,flag=-1; for(i=0;i<N;i++) { cin>>arr[i]; sumr+=arr[i]; } for(i=0;i<N;i++) { sumr=sumr-arr[i]; if(suml==sumr) { cout<<i+1<<endl; flag=1; } suml+=arr[i]; } if(flag==-1) cout<<"-1"<<endl; } return 0; }
如果有问题,欢迎留言哈。
Equilibrium point的更多相关文章
- Codility 1: equilibrium
提交了格灵深瞳的简历后,收到需要先进行一个简单的技术测试的通知,临时抱佛脚,先刷刷上面几道题: 题目要求 A zero-indexed array A consisting of N integers ...
- [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile
题意:二叉树代表使得平衡天平,修改最少值使之平衡. 代码:(Accepted,0.030s) //UVa12166 - Equilibrium Mobile //Accepted 0.030s //# ...
- Twitter OA prepare: Equilibrium index of an array
Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to ...
- hdu6415 Rikka with Nash Equilibrium (DP)
题目链接 Problem Description Nash Equilibrium is an important concept in game theory. Rikka and Yuta are ...
- 三十分钟理解博弈论“纳什均衡” -- Nash Equilibrium
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 纳什均衡(或者纳什平衡),Nash ...
- 【杂题总汇】HDU2018多校赛第九场 Rikka with Nash Equilibrium
[HDU2018多校赛第九场]Rikka with Nash Equilibrium 又是靠这样一道题擦边恰好和第两百名分数一样~愉快
- HDU - 6415 多校9 Rikka with Nash Equilibrium(纳什均衡+记忆化搜索/dp)
Rikka with Nash Equilibrium Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K ...
- 杭电多校第九场 HDU6415 Rikka with Nash Equilibrium dp
Rikka with Nash Equilibrium Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K ...
- [hdoj6415 Rikka with Nash Equilibrium][dp]
http://acm.hdu.edu.cn/showproblem.php?pid=6415 Rikka with Nash Equilibrium Time Limit: 10000/5000 MS ...
随机推荐
- 关于RDLC报表打印预览界面显示页码问号的问题
原来在reportview中,vs2010新增了一个属性,pageCountMode,默认的Estimate,提供估算的页数,另外一个属性Actual,提供实际的页数.
- Android Studio 升级到3.0 提示 java.lang.NoClassDefFoundError
Android Studio 升级到3.0 提示 java.lang.NoClassDefFoundError 这个问题折腾了2个小时,最后解决了,Stack Overflow 上也有一次类似的问题, ...
- 面试相关-转载-well,yzl——持续更新
转载yl,yzl大神的面经,顺便自己复习一下专业课的内容 操作系统相关: 什么是进程, 什么是线程.它们之间的区别和联系. 进程管理内存资源+运行过程, 线程只管理运行过程, 线程要在进程提供的资源基 ...
- Android 软键盘的显示和隐藏,这样操作就对了
一.前言 如果有需要用到输入的地方,通常会有需要自动弹出或者收起软键盘的需求.开篇明义,本文会讲讲弹出和收起软键盘的一些细节,最终还会从源码进行分析. 想要操作软键盘,需要使用到 InputMetho ...
- Numpy入门 - 数组排序
本节主要讲解numpy数组的排序方法sort的应用,包括按升序排列和按降序排列. 一.按升序排列 import numpy as np arr = np.array([[3, 1, 2], [6, 4 ...
- MongoDB优化与一些需要注意的细节
这里总结下这段时间使用mongo的心得,列出了几个需要注意的地方. 1. 系统参数及mongo参数设置 mongo参数主要是storageEngine和directoryperdb,这两个参数一开始不 ...
- Python连接MySQL数据库中各种坑
第一个坑 要想连接数据库,我们必须拥有MySQL-python这个模块,首先,我在安装这个模块的时候就到了第一个大坑. 常规安装方法:进入cmd 使用 pip install MySQL-python ...
- SpringCloud高可用Eureka搭建
网上很多博客写的都是在本地一台机器上面搭建的,我用两台机器来为大家搭建一个注册中心高可用集群 第一步:需要在每一台机器上面搭建一个注册中心. 第二步:编写第一台机器注册中心配置文件 第三步:编写第二台 ...
- 酷狗歌曲缓存kgtemp转mp3工具
一直用网易音乐听歌,不过网易的歌曲版权确实是少了一些,在酷狗上可以找到,但收费歌曲只能试听不能下载. 寻找方案 从设置里可以看出,酷狗会设置缓存目录,试听的歌曲存放到这个缓存里. 打开缓存目录: 可以 ...
- netty详解之reactor模型
假设在办理各种证件时分为填表,审核,制作3个过程,每个过程用时10分钟,这样一个工作人员需要30分钟办理一个证件.那么有没有办法提供效率,减少等待时间呢.可以让一个专门的工作人员,每个顾客到来时就负责 ...