CodeForces 996B World Cup(思维)

https://codeforces.com/problemset/problem/996/B

题意:

圆形球场有n个门,Allen想要进去看比赛。Allen采取以下方案进入球场:开始Allen站在第一个门,如果当前门前面有人Allen会花费单位时间走到下一个门,如果没人Allen从这个门就进去了。

球场的每个门,每单位时间可以进去一个人。问Allen最终是从哪个门进入球场的?

一样的代码

 有点恶心人啊。

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 //const double PI=acos(-1);
17 #define Bug cout<<"---------------------"<<endl
18 const int maxn=1e5+10;
19 using namespace std;
20 
21 int a[maxn];
22 
23 int main()
24 {
25     int n;
26     scanf("%d",&n);
27     int ans;
28     int flag=1;
29     for(int i=1;i<=n;i++)
30     {
31         scanf("%d",&a[i]);
32         if(flag&&a[i]<i)
33         {
34             flag=0;
35             ans=i;
36         }
37     }
38     int Index=1;
39     int cnt=0;
40     while(flag)
41     {
42         if(Index>n)
43             Index=1;
44         if(a[Index]<=cnt)
45         {
46             flag=0;
47             ans=Index;
48         }
49         cnt++;
50         Index++;
51     }
52     printf("%d\n",ans);
53     return 0;
54 }

另一种思路:

假设第i个门一开始有a个人,k是走过的圈数即第k圈可进入,推出一道公式k∗n+i=a,求最小k所对应的i即可(注意求k的时候a-i可能小于0,这时候都加上n,不影响最后结果,这里比较费劲得理解一下)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<cmath>
 7 #include<iomanip>
 8 #include<map>
 9 #include<vector>
10 #include<queue>
11 #include<set>
12 #include<algorithm>
13 using namespace std;
14 typedef long long int LL;
15 const int MAXN=1e5+10;
16 const int INF = 0x3f3f3f3f;
17 int main()
18 {
19     int n,a,ans,min1;
20     scanf("%d",&n);
21     min1=INF;
22     for(int i=1;i<=n;i++)
23     {
24         scanf("%d",&a);
25         int x=a-i+n;
26         
27         if(min1>x/n)
28         {
29             min1=x/n;
30             ans=i;
31         }
32     }
33     printf("%d\n",ans);
34 }

猜你喜欢

转载自www.cnblogs.com/jiamian/p/11705629.html
今日推荐