<cf>Worms Evolution

Worms Evolution
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are n forms of worms. Worms of these forms have lengths a1a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of lengths of the other two forms. Help him to do this.

Input

The first line contains integer n (3 ≤ n ≤ 100) — amount of worm's forms. The second line contains nspace-separated integers ai (1 ≤ ai ≤ 1000) — lengths of worms of each form.

Output

Output 3 distinct integers i j k (1 ≤ i, j, k ≤ n) — such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak.

Sample test(s)
input
5
1 2 3 5 7
output
3 2 1
input
5
1 8 1 5 1
output
-1
 
    
想了很久,后来才发觉直接暴力解决就可以轻松过,完全不用任何优化。直接cin然后三重for loop暴力遍历所有组合。我优化了一下,然后发觉跟直接暴力用时完全一样T.T
AC Code:
 
    
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;

struct Num
{
    int idx;//index
    int val;//value
}a[1001];

int cmp(const void *xx,const void *yy)
{
    struct Num *x=(Num*)xx;
    struct Num *y=(Num*)yy;
    return x->val-y->val;
}

int main()
{
    int n;
    int i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i].val);
        a[i].idx=i;
    }
    qsort(&a[1],n,sizeof(a[1]),cmp);
    for(i=1;i<n;i++)
        for(j=i+1;j<n;j++)
            for(k=j+1;k<=n;k++)
            {
                if(a[k].val==a[i].val+a[j].val)
                {
                    printf("%d %d %d\n",a[k].idx,a[i].idx,a[j].idx);
                    goto loop;//这样退出较快
                }
                else if(a[k].val>a[i].val+a[j].val)
                    break;
            }
    puts("-1");
    loop:
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值