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 ...
随机推荐
- php中foreach中使用&的办法
刚开始在使用foreach时候一直不理解为什么要使用& 后来发现在给一个数组里面添加数据时候很好用 <?phpheader("Content-Type:text/html;ch ...
- Python入门 - 控制结构
python控制结构有:for循环, while循环, if条件语句,下面我们直接上代码. 一.for循环 a = range(5) for x in a : print(x) 0 1 2 3 4二. ...
- codeforces 630C - Lucky Numbers 递推思路
630C - Lucky Numbers 题目大意: 给定数字位数,且这个数字只能由7和8组成,问有多少种组合的可能性 思路: 假设为1位,只有7和8:两位的时候,除了77,78,87,88之外还哇哦 ...
- HDU1541--Stars(树状数组)
Stars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- HDU3790-最短路径问题
最短路径问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- linux运维、架构之路-Zabbix监控应用及分布式
一.Zabbix监控集群应用 1.监控端口 net.tcp.listen[port] 检查 TCP 端口 是否处于侦听状态.返回 0 - 未侦听:1 - 正在侦听 net.tcp.port[<i ...
- PHP 字符串转 bigint 型md5
1 2 3 4 5 6 7 8 /** * 字符串转bigint * @return bigint(string) */ public function md5( ...
- Shell 初步学习
Shell 概述 Shell:Linux命令解释器 脚本执行方式 chmod 755 脚本名:赋权限(调用必须显示的使用绝对路径或相对路径) bash 脚本名:通过Bash调用执行脚本 命令别名 al ...
- FastDFS教程IV-文件服务器集群搭建
1.简介 本文主要介绍FastDFS文件服务器的集群搭建,在阅读本文之前,您需具备FastDFS文件服务器单节点安装,扩容,迁移等方面的知识.同时,您还需了解Keepalived,nginx方 ...
- Solr中Field常用属性
FieldType 实例:<fieldType name="text_ik" class="solr.TextField"></fieldTy ...