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 neighbouringsoldiers, 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

解题说明:此题就是找出一个环形队列中差值最小的两个数的位置,遍历即可,最后把首尾单独进行计算

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int i,n;
	int a[101];
	int min;
	int posb,pose;
	int temp;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	min=abs(a[1]-a[0]);
	posb=0;
	pose=1;
	for(i=1;i<n-1;i++)
	{
		temp=abs(a[i+1]-a[i]);
		if(temp<min)
		{
			min=temp;
			posb=i;
			pose=i+1;
		}
	}
	temp=abs(a[0]-a[n-1]);
	if(temp<min)
	{
		min=temp;
		posb=n-1;
		pose=0;
	}
	printf("%d %d\n",posb+1,pose+1);
	return 0;
}


你可能感兴趣的:(A. Reconnaissance 2)