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 ...
随机推荐
- IDEA搭建SSM出现的一些错误
下面是我这几天整合SpringMVC+Spring+MyBatis框架遇到的一些问题 ,在这里总结一下: 1:HTTP Status 500 - Request processing failed; ...
- JQuery制作网页—— 第二章 JavaScript操作BOM对象
1.window对象: 浏览器对象模型(BOM)是javascript的组成之一, 它提供了独立与浏览器窗口进行交换的对象,使用浏览器对象模型可以实现与HTML的交互. 它的作用是将相关的元素组织包装 ...
- php mysql find_in_set函数 检索单子段 逗号分隔序列
FIND_IN_SET($kwd,field) 例如在 表 AA中 numbers 字段 保存列数据 1,4,8,75,41,7 就可以使用 FIND_IN_SET(8,numbers) 查询记 ...
- eBay 表结构
erp_ebay_list 建表语句 CREATE TABLE `erp_ebay_list` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT '自增主键', `na ...
- Small Talk Matters【闲谈很重要】
Small Talk Matters We' ve all been there: in a lift, in line at the bank or on an airplane, 我们都有过这样的 ...
- ln -s 软链接产生Too many levels of symbolic links错误
不能使用相对路径, ln -s ./cmake /usr/bin/ 而是要 ln -s /usr/local/bin/cmake /usr/bin/
- POJ:3273-Monthly Expense
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32067 Accepted: 12081 Des ...
- 11-Json文件配置
1-新建json文件, 设置json文件生成的方式 { "ClassNo": "1", "ClassDesc": "Asp.net ...
- dfs序线段树
dfs序+线段树,啥?如果在一棵树上,需要你修改一些节点和查询一些节点,如果直接dfs搜的话肯定超时,那用线段树?树结构不是区间啊,怎么用?用dfs序将树结构转化为一个区间,就能用线段树进行维护了. ...
- 手把手写代码学习C语言基础