http://codeforces.com/contest/34

A. Reconnaissance 2
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

n soldiers stand in a circle. For each soldier his height ai is known. A reconnaissance unit can be made of such two neighbouring soldiers, whose heights difference is minimal, i.e. |ai - aj| is minimal. So each of them will be less noticeable with the other. Output any pair of soldiers that can form a reconnaissance unit.

Input

The first line contains integer n (2 ≤ n ≤ 100) — amount of soldiers. Then follow the heights of the soldiers in their order in the circle — n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1000). The soldier heights are given in clockwise or counterclockwise direction.

Output

Output two integers — indexes of neighbouring soldiers, who should form a reconnaissance unit. If there are many optimum solutions, output any of them. Remember, that the soldiers stand in a circle.

Sample test(s)
input
5
10 12 13 15 10
output
5 1
input
4
10 20 30 40
output
1 2

我们输入完数组后,就要比较相邻两个数之差的绝对值,把绝对值最小的两个位置输出来,但是我们还要比较第一个和第n个位置,这就需要特别为它写一行代码,但是,如果我们可以写成a[i+1]%n的形式的话,最后一个数就可以直接与第一个数相比较,这就显得代码比较简单清晰。

 int n;
    scanf("%d",&n);
    for (int i=0;i<n;i++)     
        scanf("%d",&a[i]);
    int best=0;
    for (int i=0;i<n;i++)
        if (abs(a[i]-a[(i+1)%n])<abs(a[best]-a[(best+1)%n]))     从第一二个数的差的绝对值依次与第
            best=i;											
从第一二个数的差的绝对值依次与第i和i+1个进行比较如果第i和i+1个比前面的小,best就变成来那个绝对值最小的位置。
printf("%d %d\n",best+1,(best+1)%n+1); return 0;}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值