Allen wants to enter a fan zone that occupies a round square and has n entrances.
There already is a queue of ai people in front of the i-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute.
Allen uses the following strategy to enter the fan zone:
- Initially he stands in the end of the queue in front of the first entrance.
- Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance).
Determine the entrance through which Allen will finally enter the fan zone.
Input
The first line contains a single integer n(2≤n≤105) — the number of entrances.
The second line contains nintegers a1,a2,…,an (0≤ai≤109) — the number of people in queues. These numbers do not include Allen.
Output
Print a single integer — the number of entrance that Allen will use.
Sample Input
Input
4
2 3 2 0
Output
3
Input
2
10 10
Output
1
Input
6
5 2 6 5 7 4
Output
6
题解:
有n个队,第 i 个队有a[ i ]个人在排队,Allen从第一个队开始排,如果这个队的人数不为0,他会移到下一个队继续排,每移动一次,所有的队的人数都会减少 1 。
代码:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#define MAX 0x3f3f3f3f
using namespace std;
const int MAXN=1e5+10;
typedef long long LL;
int a[MAXN];
int main()
{
int ans=0,n,min1=MAX;
cin >> n;
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
for(LL i=1;i<=n;i++)
{
int t=(a[i]-i+n)/n; //加n是为了避免t为负数
if(min1>t)
min1=t,ans=i;
}
cout << ans <<endl;
return 0;
}