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 ...
随机推荐
- debug断点调试
debug断点调试 1,虫子启动2,F6 执行断点的下一步,下一个语句 F5 进入方法 F8 执行到结束 查看表达式的值:选中查看的表达式,接着按 ctrl ...
- VC++下编译 程序“减肥”
在vc6 和 vs 2008下 编译 以下代码,不更改任何编译设置(vc6 40k , s2008 7k). 一.vc6下,Release 模式 编译处理. 1.去掉不必要的 链接库 工程(Pro ...
- Java基础(二)-static关键字分析
static关键字是我们在编程中经常会使用到的,但有些可能只知其然而不知其所以然.下面介绍static关键字的作用再通过例子结合说明. static关键字共有五种作用(先说明static所修饰的不会改 ...
- POJ1273 网络流-->最大流-->模板级别-->最大流常用算法总结
一般预流推进算法: 算法思想: 对容量网络G 的一个预流f,如果存在活跃顶点,则说明该预流不是可行流. 预流推进算法就是要选择活跃顶点,并通过它把一定的流量推进到它的邻接顶点,尽可能将正的赢余减少为0 ...
- php代码在服务器中查看接值
error_log("You messed up!", 3, "/var/tmp/my-errors.log");
- mac下出现xcrun: error导致git、svn无法使用的解决办法
现象:xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun ...
- 在Ubuntu14.04下安装Docker CE(1) - repository篇
从2017年3月开始,Docker开始分为社区版本和企业版,也就是Docker CE和Docker EE, 原来Ubuntu14.04下,通过sudo apt-get install docker.i ...
- opensslBIO系列之2---BIO结构和BIO相关文件介绍
BIO结构和BIO相关文件介绍 (作者:DragonKing Mail:wzhah@263.net 公布于:http://gdwzh.126.com openssl专业论坛) ...
- SAP ABAP编程 Table Control动态隐藏列
在SAP DIALOG设计中,有时候须要动态的隐藏某些列,以下是方法. ***数据定义 CONTROLS: table_control TYPE TABLEVIEW USING SCREEN 0100 ...
- 在linux上手动搭建svn服务器
svn服务器的搭建 环境: linux CentOS 7 安装: 1.安装svn服务器 yum install subversion 2.查看版本 svnserve --version 3.创建版本库 ...