GYM

来源:互联网 发布:linux ssh2 编辑:程序博客网 时间:2024/06/16 19:54

Dream of Sum
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output

Sometimes you must fall in order to rise but I seemed to be already dead. The abyss behind me was getting the bigger the further I was moving away of it. I turned around, looked into it and saw myself at the very bottom of it. And at that moment I realized I was still alive.

I was sleeping, and the shadows of the past took advantage of it to take their shapes. I saw my wife and my daughters. They were dead. I also saw the sequence a1, ..., an of n integers. I had to find some consecutive elements in this sequence such that their sum was equal to zero. It was a matter of life and death. I had no time so I had to find the minimal possible number of such elements.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the length of the sequence from the dream.

The second line contains n integers separated by spaces: ai ( - 109 ≤ ai ≤ 109) — the sequence itself.

Output

Output two integers separated by a space: the 1-based position in the sequence, starting from which the consecutive elements must be taken, and how many elements must be taken. If there are several possible answers, output any of them. If it's impossible to find the elements satisfying the statement, output a single integer –1 instead.

Examples
input
81 3 -2 -1 -5 2 2 1
output
2 3
input
75 -1 -1 -1 -1 -1 2
output
5 3



题意:在一堆数中,找到一个连续的区间,他们里面的数的和为0;


解题思路:假设我们知道前10个数的和为8,前15个数的和也为8,那么很明显,11~15的数的和必为0!所以我们对前缀和从前往后扫一遍,保存最小的即可。


VSCode真的舒服……

#include <iostream>#include <vector>#include <map>using namespace std;typedef long long int ll;ll a[200005];ll sum[200005];int n;int main(){    scanf("%d", &n);    map<ll, int> m;//记录前缀和出现的位置    m[0] = 1;    int min1;//记录位置    int min2 = 2000000;//长度    for (int i = 1; i <= n; i++)    {        scanf("%lld", &a[i]);        sum[i] = sum[i - 1] + a[i];        if (m[sum[i]])        {            if (i - m[sum[i]] < min2)            {                min2 = i - m[sum[i]] + 1;                min1 = m[sum[i]];            }        }        m[sum[i]] = i + 1;    }    if (min2 == 2000000)        printf("%d\n", -1);    else        printf("%d %d\n", min1, min2);    return 0;}





原创粉丝点击