code forces 996BWorld Cup
1 second
256 megabytes
standard input
standard output
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.
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.
Print a single integer — the number of entrance that Allen will use.
4
2 3 2 0
3
2
10 10
1
6
5 2 6 5 7 4
6
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个队伍,你不想排队,每次只能走到下一个队伍
请问你在哪个队伍可以不用排队
首先我们可以想到,第一遍走的情况,从头到尾,
那么第一遍模拟就可以是 for(int i =0 ;i<n;i++) a[i]-=i;
这里就有问题了 如果第一遍走不完怎么办 继续从最后一个走到第一个 ,这是一个类似于环一样的
可是我们第一遍模拟的时候只考虑了走后面的情况,走过前面的需要还原,所以我们这里倒着减一下,
使得走一遍后保证每个队伍都走了n个单位长度
那么这里有问题了 如果n特别小但是a[i]特别大呢
我们有两个优化的地方
1.找出a[i]的最小值,每个都减去min
2.由于a[i]<1e9,n<=1e5,那么在n的循环里面循环20次就可以满足至少出现一个空队列的情况
代码如下
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn= 1e5+;
const int INF = 0x3f3f3f3f;
ll a[maxn];
int main() {
int n;
while(~scanf("%d",&n)) {
int ans=;
int minn=INF;
for(int i=; i<n; i++) {
scanf("%lld",&a[i]);
if(a[i]<minn) minn=a[i];
}
int cnt=minn/n;
//优化1
for(int i=; i<n ; i++) {
a[i]-=cnt*n;
}
//优化2
for(int d=; d<=; d++) {
for(int i=; i<n; i++) {
a[i]-=i;
if(a[i]<=) {
cout<<i+<<endl;
return ;
}
}
//还原
for(int i=n-;i>=;i--) {
a[i] -= n - i;
}
}
}
return ;
}
code forces 996BWorld Cup的更多相关文章
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- Code Forces 543A Writing Code
题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...
- code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)
Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...
- code forces 382 D Taxes(数论--哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- code forces Watermelon
/* * Watermelon.cpp * * Created on: 2013-10-8 * Author: wangzhu */ /** * 若n是偶数,且大于2,则输出YES, * 否则输出NO ...
- code forces Jeff and Periods
/* * c.cpp * * Created on: 2013-10-7 * Author: wangzhu */ #include<cstdio> #include<iostrea ...
- Code Forces Gym 100971D Laying Cables(单调栈)
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- ZOJ3329 概率DP
One Person Game Time Limit: 1 Second Memory Limit: 32768 KB Special Judge There is a very ...
- Dropping Balls(小球下落)
紫书P148,例题6-6 Sample Input 4 2 3 4 10 1 2 2 8 128 Sample Output 12 7 512 3 255 这应该不仅仅是一棵完全二叉树,题目中说保证所 ...
- POJ:2395-Out of Hay
Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18780 Accepted: 7414 Descripti ...
- 初见spark-01
今天我们来学习spark,spark是一种快速,通用,可扩展的大数据分析引擎,现已成为Apache顶级项目,Spark是MapReduce的替代方案,而且兼容HDFS,Hive,可融入Hadoop的生 ...
- HTML中body相关标签-02
今日内容: 字体标签: h1~h6.<font>.<u>.<b>.<strong><em>.<sup>.<sub> ...
- [WC2002][洛谷P1578]奶牛浴场
洛谷题解里那个人可真是话多呢. 题目描述 由于John建造了牛场围栏,激起了奶牛的愤怒,奶牛的产奶量急剧减少.为了讨好奶牛,John决定在牛场中建造一个大型浴场.但是John的奶牛有一个奇怪的习惯,每 ...
- Lambda与LINQ
Lambda与LINQ写法对比: 上为Lambda 下为LINQ 显示指定列 Students.select(u=>(new {Name=u.Sname,Address=u.Saddress}) ...
- 网易考拉Android客户端网络模块设计
本文来自网易云社区 作者:王鲁才 客户端开发中不可避免的需要接触到访问网络的需求,如何把访问网络模块设计的更具有扩展性是每一个移动开发者不得不面对的事情.现在有很多主流的网络请求处理框架,如Squar ...
- 跟着学!教你开发第一个Java程序
今天我们的目标是开发人生中的第一个Java程序,虽然可能会很简单,但是这小小的一步却是跨入IT行业的一大步!下面我们来一起来仔细的了解开发的流程. 准备工作 1,作为一名准程序猿自备一台电脑那是必不可 ...
- 剑指Offer - 九度1523 - 从上往下打印二叉树
剑指Offer - 九度1523 - 从上往下打印二叉树2013-12-01 00:35 题目描述: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 输入: 输入可能包含多个测试样例,输入以E ...