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 ...
随机推荐
- 树莓派远程桌面配置-开机自启SSH
必须先安装tightvncserver sudo apt-get install tightvncserver 再安装xrdp服务. sudo apt-get install xrdp 如果开着防火墙 ...
- Ubuntu下删除VMware的方法
一般我们都装的是Vmware workstation版的,所以,我们在终端下输入 sudo vmware-installer -u vmware-workstation 然后就会弹出vmware的卸载 ...
- UVA 1508 - Equipment dp状态压缩
题意: 已知n个5元组,从中选出k组,使得这些组中5个位置,每个位置上最大数之和最大. 分析:当k>5时,就是n个5元组最大的数之和,当k<5时,就当做5元组,状态压缩,用00000表示 ...
- 初窥c++11:lambda函数及其用法
转载于:点击打开链接 为什么需要lambda函数 匿名函数是许多编程语言都支持的概念,有函数体,没有函数名.1958年,lisp首先采用匿名函数,匿名函数最常用的是作为回调函数的值.正因为有这样的需求 ...
- Mysql 表分区和性能
以下内容节选自<Mysql技术内幕InnoDB存储引擎> mysql表分区: 分区功能并不是所有存储引擎都支持的,如CSV.MERGE等就不支持.mysql数据库支持的分区类型为水平分区( ...
- Android IntentService使用介绍以及源码解析
版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.IntentService概述及使用举例 IntentService内部实现机制用到了HandlerThread,如果对HandlerThrea ...
- SQL多表关联查询
在创建关系型数据表时,根据数据库范式的要求,为了降低数据的冗余,提供数据维护的灵活性 将数据分成多个表进行存储,实际工作当中,需要多个表的信息,需要将多个表合并显示 --内连接 selec ...
- Ajax中的JSON格式与php传输过程的浅析
在Ajax中的JSON格式与php传输过程中有哪些要注意的小地方呢? 先来看一下简单通用的JSON与php传输数据的代码 HTML文件: <input type="button&quo ...
- javascript实时保存时出现改动多条记录的bug
文章实现编辑,编辑页面是右側弹出层,当有改动时就保存,对文章标题title加入改变change事件,有改变时就保存文章(saveArticle) $("#title").chang ...
- 第一次面试&第一次霸面
哈哈哈哈,第一次面试和第一次都献给了CVTE! CVTE的招聘流程有点特别:网測-- 一面--笔试--二面--offer 想起网測那天就心酸.那先在做第三部分的专业測试.计时器突然出错........ ...