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的更多相关文章

  1. Codility 1: equilibrium

    提交了格灵深瞳的简历后,收到需要先进行一个简单的技术测试的通知,临时抱佛脚,先刷刷上面几道题: 题目要求 A zero-indexed array A consisting of N integers ...

  2. [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile

    题意:二叉树代表使得平衡天平,修改最少值使之平衡. 代码:(Accepted,0.030s) //UVa12166 - Equilibrium Mobile //Accepted 0.030s //# ...

  3. 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 ...

  4. hdu6415 Rikka with Nash Equilibrium (DP)

    题目链接 Problem Description Nash Equilibrium is an important concept in game theory. Rikka and Yuta are ...

  5. 三十分钟理解博弈论“纳什均衡” -- Nash Equilibrium

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 纳什均衡(或者纳什平衡),Nash ...

  6. 【杂题总汇】HDU2018多校赛第九场 Rikka with Nash Equilibrium

    [HDU2018多校赛第九场]Rikka with Nash Equilibrium 又是靠这样一道题擦边恰好和第两百名分数一样~愉快

  7. HDU - 6415 多校9 Rikka with Nash Equilibrium(纳什均衡+记忆化搜索/dp)

    Rikka with Nash Equilibrium Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K ...

  8. 杭电多校第九场 HDU6415 Rikka with Nash Equilibrium dp

    Rikka with Nash Equilibrium Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K ...

  9. [hdoj6415 Rikka with Nash Equilibrium][dp]

    http://acm.hdu.edu.cn/showproblem.php?pid=6415 Rikka with Nash Equilibrium Time Limit: 10000/5000 MS ...

随机推荐

  1. windows下tensorflow的安装

    一.直接python安装 1.CPU版本: pip3 install --upgrade tensorflow 2.GPU版本:pip3 install --upgrade tensorflow-gp ...

  2. 邻里街坊 golang入坑系列

    如果要追新或者怀旧,就点击https://andy-zhangtao.gitbooks.io/golang/content/ . 博客园里面的文章基本和gitbook上面是保持同步的. 这几天看了几集 ...

  3. 【二十三】php之预定义超全局变量

    php提供了九种预定义超全局变量: $_GET.$_POST.$_REQUEST.$_SERVER.$_ENV.$_FILE. $_COOKIE.$_SESSION. $GLOBALS 1.$_GET ...

  4. windows平台下基于QT和OpenCV搭建图像处理平台

        在之前的博客中,已经分别比较详细地阐述了"windows平台下基于VS和OpenCV"以及"Linux平台下基于QT和OpenCV"搭建图像处理框架,并 ...

  5. Awk,Cat,Head分析Nginx日志常用命令

    Nginx 日志分析   1.根据访问IP统计UV   awk '{print $1}'  access.log|sort | uniq -c |wc -l   2.统计访问URL统计PV   awk ...

  6. RxAndroid中observable的基本使用和表单校验操作

    RxAndroid 响应式编程 类似于监听-观察者模式 在观察者模式中,你的对象需要实现 RxJava 中的两个关键接口:Observable 和 Observer.当 Observable 的状态改 ...

  7. 大数据学习系列之四 ----- Hadoop+Hive环境搭建图文详解(单机)

    引言 在大数据学习系列之一 ----- Hadoop环境搭建(单机) 成功的搭建了Hadoop的环境,在大数据学习系列之二 ----- HBase环境搭建(单机)成功搭建了HBase的环境以及相关使用 ...

  8. tyvj4866 摆摊

    这分送的真慷慨,我随手打了个莫队,就90了.... 90分代码: #include<bits/stdc++.h> using namespace std; #define MAXN 100 ...

  9. SAP GUI 750 安装包 及 补丁3 共享

    SAP GUI 750 安装包 及 补丁3 共享 链接: https://pan.baidu.com/s/1hstkfUs%20 密码: ggbz -------------------------- ...

  10. String内存溢出异常(错误)可能的原因及解决方式

    摘要:本Blog主要为了阐述java.lang.OutOfMemoryError:PermGenspace可能产生的原因及解决方式. 当中PermGen space是Permanent Generat ...