World Cup(思维+模拟)
Description
Allen wants to enter a fan zone(球迷区) that occupies a round square and has nn entrances.
There already is a queue of aiai people in front of the ii-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 nn (2≤n≤1052≤n≤105) — the number of entrances.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤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
4
2 3 2 0
3
2
10 10
1
6
5 2 6 5 7 4
6
Hint
In the first example the number of people (not including Allen) changes as follows: [2,3,2,0]→[1,2,1,0]→[0,1,0,0][2,3,2,0]→[1,2,1,0]→[0,1,0,0]. The number in bold is the queue Alles stands in. We see that he will enter the fan zone through the third entrance.
In the second example the number of people (not including Allen) changes as follows:[10,10]→[9,9]→[8,8]→[7,7]→[6,6]→[5,5]→[4,4]→[3,3]→[2,2]→[1,1]→[0,0][10,10]→[9,9]→[8,8]→[7,7]→[6,6]→[5,5]→[4,4]→[3,3]→[2,2]→[1,1]→[0,0].
In the third example the number of people (not including Allen) changes as follows:[5,2,6,5,7,4]→[4,1,5,4,6,3]→[3,0,4,3,5,2]→[2,0,3,2,4,1]→[1,0,2,1,3,0]→[0,0,1,0,2,0][5,2,6,5,7,4]→[4,1,5,4,6,3]→[3,0,4,3,5,2]→[2,0,3,2,4,1]→[1,0,2,1,3,0]→[0,0,1,0,2,0].
题目意思:圆形球场有n个门,Allen想要进去看比赛。Allen采取以下方案进入球场:开始Allen站在第一个门,如果当前门前面有人Allen会花费单位时间走到下一个门,如果没人Allen从这个门就进去了。球
场的每个门,每单位时间可以进去一个人。问Allen最终是从哪个门进入球场的?
解题思路:我们假设第i个门有a[i]个人,Allen第k圈可以从此进入,那么有:k∗n+i>=a[i],我们只需要利用这一个公式,对每一个门和排队的圈数进行遍历就可以了。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int MAX=1e5+10; 6 int a[MAX]; 7 int main() 8 { 9 int n,i,k,ans; 10 scanf("%d",&n); 11 for(i=1;i<=n;i++) 12 { 13 scanf("%d",&a[i]); 14 } 15 k=0;///圈数 16 while(1) 17 { 18 for(i=1;i<=n;i++) 19 { 20 if(a[i]<=n*k+i-1) 21 { 22 printf("%d\n",i); 23 return 0; 24 } 25 } 26 k++; 27 } 28 return 0; 29 }
我在网上看到有大佬使用了一种时间复杂度更低的方法,这里拿来做一下分析:
https://blog.csdn.net/ZscDst/article/details/80807353
我们可以发现是有规律的。我们假设第i个门有a个人,Allen第x圈可以从此进入,那么有:x∗n+i=a→x=abs(a−i)/nx∗n+i=a→x=abs(a−i)/n,所以第一个最小圈数进入的那个门就是答案。
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define INF 0x3f3f3f3f 6 const int MAX=1e5+10; 7 using namespace std; 8 int main() 9 { 10 int n,i,a,ans,mins; 11 mins=INF; 12 scanf("%d\n",&n); 13 for(i=1;i<=n;i++) 14 { 15 scanf("%d",&a); 16 if(mins>(a-i+n)/n) 17 { 18 mins=(a-i+n)/n; 19 ans=i; 20 } 22 } 23 printf("%d\n",ans); 24 return 0; 25 }