CodeForce edu round 53 Div 2. D:Berland Fair
2 seconds
256 megabytes
standard input
standard output
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nn booths, arranged in a circle. The booths are numbered 11through nn clockwise with nn being adjacent to 11. The ii-th booths sells some candies for the price of aiai burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most TT burles at the fair. However, he has some plan in mind for his path across the booths:
- at first, he visits booth number 11;
- if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
- then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
The first line contains two integers nn and TT (1≤n≤2⋅1051≤n≤2⋅105, 1≤T≤10181≤T≤1018) — the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the price of the single candy at booth number ii.
Print a single integer — the total number of candies Polycarp will buy.
3 38
5 2 5
10
5 21
2 4 100 2 6
6
Let's consider the first example. Here are Polycarp's moves until he runs out of money:
- Booth 11, buys candy for 55, T=33T=33;
- Booth 22, buys candy for 22, T=31T=31;
- Booth 33, buys candy for 55, T=26T=26;
- Booth 11, buys candy for 55, T=21T=21;
- Booth 22, buys candy for 22, T=19T=19;
- Booth 33, buys candy for 55, T=14T=14;
- Booth 11, buys candy for 55, T=9T=9;
- Booth 22, buys candy for 22, T=7T=7;
- Booth 33, buys candy for 55, T=2T=2;
- Booth 11, buys no candy, not enough money;
- Booth 22, buys candy for 22, T=0T=0.
No candy can be bought later. The total number of candies bought is 1010.
In the second example he has 11 burle left at the end of his path, no candy can be bought with this amount.
之前用线段树做的。超时了很难受。因为没有有效的处理无效的点。之后借鉴了网上的答案。改成链表去做。链表有些不熟悉了。比如删除结点那里都搞错了。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#define ll long long
//#define local using namespace std; const int MOD = 1e9+;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 2e5+; ll a[maxn];
ll t;
int n;
int nex[maxn];
int pre[maxn]; int main() {
#ifdef local
if(freopen("/Users/Andrew/Desktop/data.txt", "r", stdin) == NULL) printf("can't open this file!\n");
#endif scanf("%d%lld", &n , &t);
int num = ;
for (int i = ; i < n; ++i) {
ll tmp;
scanf("%lld", &tmp);
if (tmp > t) continue;
a[num++] = tmp;
}
n = num-;
if (n == ) {
printf("0\n");
return ;
}
for (int i = ; i < n; ++i) {
nex[i] = i+;
}
nex[n] = -;
for (int i = ; i <= n; ++i)
pre[i] = i-;
ll cnt = ;
while (nex[] != -) {
ll sum = ;
int num = ;
ll rem = t;
for (int i = nex[]; i != -; i = nex[i]) {
if (a[i] <= rem) {
sum += a[i];
num++;
rem -= a[i];
} else {
nex[pre[i]] = nex[i];
pre[nex[i]] = pre[i];
}
}
if (sum == ) break;
cnt += t/sum*num;
t %= sum;
}
printf("%lld\n", cnt);
#ifdef local
fclose(stdin);
#endif
return ;
}
CodeForce edu round 53 Div 2. D:Berland Fair的更多相关文章
- 构造 Codeforces Round #Pi (Div. 2) B. Berland National Library
题目传送门 /* 题意:给出一系列读者出行的记录,+表示一个读者进入,-表示一个读者离开,可能之前已经有读者在图书馆 构造:now记录当前图书馆人数,sz记录最小的容量,in数组标记进去的读者,分情况 ...
- Educational Codeforces Round 53 (Rated for Div. 2) D. Berland Fair
题意:一个人 有T块钱 有一圈商店 分别出售 不同价格的东西 每次经过商店只能买一个 并且如果钱够就必须买 这个人一定是从1号店开始的!(比赛的时候读错了题,以为随意起点...)问可以买多少个 ...
- [codeforces][Educational Codeforces Round 53 (Rated for Div. 2)D. Berland Fair]
http://codeforces.com/problemset/problem/1073/D 题目大意:有n个物品(n<2e5)围成一个圈,你有t(t<1e18)元,每次经过物品i,如果 ...
- Codeforces Round #Pi (Div. 2) B. Berland National Library set
B. Berland National LibraryTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Codeforces Round #Pi (Div. 2) B. Berland National Library 模拟
B. Berland National LibraryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #298 (Div. 2) E. Berland Local Positioning System 构造
E. Berland Local Positioning System Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦. #include<bits/stdc++.h> #defi ...
- Codeforces Round #Pi (Div. 2) B Berland National Library
B. Berland National Library time limit per test1 second memory limit per test256 megabytes inputstan ...
- Edu Cf Round 105 (Div. 2) B. Berland Crossword 1.读懂题, 2. 思维
一. 原题链接 https://codeforces.com/contest/1494/problem/B 二. 题意 + 题解: 没看懂题目, 懵了好久, 先狡辩一下当时误解的句子, 英语是硬伤 ...
随机推荐
- [图形学] B样条曲线 - 原理和C++实现的演示程序(附源码)
http://blog.csdn.net/mahabharata_/article/details/71856907 大二的时候,曾受老师所托,用C++而不是OpenGL去写B样条曲线的教学程序.时隔 ...
- python基础数据类型练习2
1,写代码,有如下列表,按照要求实现每一个功能li = ['alex', 'wusir', 'eric', 'rain', 'alex'] 计算列表的长度并输出print(len(li))答:结果为5 ...
- ubuntu google chrome 忽略证书错误 -- 解决自签名证书不支持的问题
ubuntu chrome 打开自签名的证书的服务器的https时,提示 Your connection is not private. 错误代码:NET::ERR_CERT_AUTHORITY_IN ...
- 【Shell】单行注释和多行注释
单行注释 '# ' # echo "hello" 多行注释 方法1 : << ! 这是注释1 这是注释2 这是注释3 ! 方法2 :' 这是注释1 这是注释2 这是注释 ...
- HTML5 浏览器支持
css重置 header, section, footer, aside, nav, main, article, figure { display: block; } 为HTML添加新的元素 < ...
- Linux:Fedora系统的安装
新的一周,新的一天又来了!话不多说我们直接进入今天的系统安装吧!这次是Linux系统的第八期了,这款系统是可以当做家庭用途使用,非常强大的一款开发源操作系统. 安装Fedora系统 系统映像文件下载 ...
- io 的一些简单说明及使用
io 流简述: i->inputStream(输入流) o->outputStream (输出流) IO流简单来说就是Input和Output流,IO流主要是用来处理设备之间的数据传输, ...
- java中List按指定大小分割
之前写过的代码,几个月后遇到类似的功能,感觉很熟悉,稍微费些劲也能做到,但是觉得有些不甘.明明之前整理好的为什么要重新做?百度参考其他人的思路感觉不是很顺心,个人原因吧.于是翻箱倒柜找出以前的代码,贴 ...
- 分频器的Verilog实现
一.占空比50%的奇数分频 1.实现思路 实现奇数(N)分频,分别用上升沿计数到(N-1)/2,再计数到N-1:用下降沿计数到(N-1)/2,再计数到N-1,得到两个波形,然后把它们相或即可得到N分频 ...
- vue-resource+element upload上传(遇到formData总是变为object格式)
文件上传这种业务需求很常见,但是最近用了element,仔细看了文档,按照demo写了上传,与后台传参调取接口时,控制台总是显示未获取到文件,想了又想,发现一开始思路就跑遍了... 写此博记录下遇到的 ...