Codeforces 1073D:Berland Fair(模拟)
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nnn booths, arranged in a circle. The booths are numbered 111through nnn clockwise with nnn being adjacent to 111. The iii-th booths sells some candies for the price of aia_iaii burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most TTT burles at the fair. However, he has some plan in mind for his path across the booths:
- at first, he visits booth number 111;
- 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.
Input
The first line contains two integers nnn and T(1≤n≤2⋅105,1≤T≤1018)T (1≤n≤2⋅10^5, 1≤T≤10^{18})T(1≤n≤2⋅105,1≤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,…,an(1≤ai≤109)a_1,a_2,…,a_n (1≤a_i≤10^9)a1,a2,…,an(1≤ai≤109) — the price of the single candy at booth number iii.
Output
Print a single integer — the total number of candies Polycarp will buy.
Examples
input
3 38
5 2 5
output
10
input
5 21
2 4 100 2 6
output
6
Note
Let’s consider the first example. Here are Polycarp’s moves until he runs out of money:
- Booth 111, buys candy for 555, T=33T=33T=33;
- Booth 222, buys candy for 222, T=31T=31T=31;
- Booth 333, buys candy for 555, T=26T=26T=26;
- Booth 111, buys candy for 555, T=21T=21T=21;
- Booth 222, buys candy for 222, T=19T=19T=19;
- Booth 333, buys candy for 555, T=14T=14T=14;
- Booth 111, buys candy for 555, T=9T=9T=9;
- Booth 222, buys candy for 222, T=7T=7T=7;
- Booth 333, buys candy for 555, T=2T=2T=2;
- Booth 111, buys no candy, not enough money;
- Booth 222, buys candy for 222, T=0T=0T=0.
No candy can be bought later. The total number of candies bought is 101010.
In the second example he has 111 burle left at the end of his path, no candy can be bought with this amount.
题意
nnn种糖果围成一圈,每种糖果每个aia_iai元。初始时你有TTT元,接着你从111开始绕圈。一旦你发现有糖果能买,你就买一个。直到一个糖果都买不起。问最后买了多少个糖果。
Slove
首先对数据进行处理:nnn种糖果全部买一次需要多少钱,找到nnn种糖果中最便宜的价格
然后计算所有的糖果均能买的圈数有多少。
将剩余的钱进行进行按圈数模拟:一圈一圈的模拟肯定是不行的,稳稳地超时,所以需要进行优化
将剩余的钱数与当前所在位置的糖果价格进行比较,更新钱数,并记录每一圈结束后的次大值,当次大值等于最小值的时候,证明已经不能买除了最便宜的糖果外的其他糖果,此时结束循环。将剩余的钱数除以最小值(向下取整)可得到最终剩余的钱能买糖果数
Code
代码用时:77ms
/*************************************************************************
> File Name: D.cpp
> Author: WZY
> Created Time: 2019年02月15日 16:33:54
************************************************************************/
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"\n"
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
ll a[maxn];
ll sum[maxn];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
ll t;
cin>>n>>t;
ll minn=1e18+10;
ll sum=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
minn=min(minn,a[i]);
}
ll ans=0;
ll res=t/sum;
ans+=res*n;
ll s=t%sum;
if(s==0)
{
cout<<ans<<endl;
return 0;
}
ll minnn=1e9+10;
while(s>minn)
{
ll _=minnn;
for(int i=1;i<=n;i++)
{
if(s>=a[i])
{
s-=a[i];
ans++;
// 更新次小值
if(a[i]!=minn&&a[i]<minnn&&a[i]!=minn)
minnn=a[i];
}
}
// 如果次小值没有得到更新,并且次小值大于剩余钱数,证明只有最小钱数的糖果可以购买
if(minnn==_&&s<minnn)
break;
}
ans+=s/minn;
cout<<ans<<endl;
return 0;
}
Codeforces 1073D:Berland Fair(模拟)的更多相关文章
- CodeForces - 1073D Berland Fair
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nnbooths, arranged in ...
- codeforces 897A Scarborough Fair 暴力签到
codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...
- CodeForce edu round 53 Div 2. D:Berland Fair
D. Berland Fair time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- codeforces1073d Berland Fair 思维(暴力删除)
题目传送门 题目大意:一圈人围起来卖糖果,标号从1-n,每个位置的糖果都有自己的价格,一个人拿着钱从q开始走,能买则买,不能买则走到下一家,问最多能买多少件物品. 思路:此题的关键是不能买则走到下一家 ...
- [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)
[Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...
- 【Codeforces 1073D】Berland Fair
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们可以从左到右枚举一轮. 定义一个cost表示这一轮花费的钱数 如果cost+a[i]<=T那么就可以买它,并且买下它(模拟题目要求) ...
- [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,如果 ...
- Educational Codeforces Round 53 (Rated for Div. 2) D. Berland Fair
题意:一个人 有T块钱 有一圈商店 分别出售 不同价格的东西 每次经过商店只能买一个 并且如果钱够就必须买 这个人一定是从1号店开始的!(比赛的时候读错了题,以为随意起点...)问可以买多少个 ...
- 【CF1073D】Berland Fair(模拟)
题意:初始有t元,每次从1开始买,从1到n依次有n个人,每个人的东西价格为a[i],该人依次能买就买,到n之后再回到1从头开始,问最后能买到的东西数量 n<=2e5,t<=1e18,a[i ...
随机推荐
- MySQL全面瓦解29:使用Partition功能实现水平分区
1 回顾 上一节我们详细讲解了如何对数据库进行分区操作,包括了 垂直拆分(Scale Up 纵向扩展)和 水平拆分(Scale Out 横向扩展) ,同时简要整理了水平分区的几种策略,现在来回顾一下. ...
- accustom
近/反义词: acclimatize, familiarize, habituate, inure, get used to, orient; alienate, estrange, wean New ...
- CORS 如果需要指定多个域名怎么办
CORS 通过控制 Access-Control-Allow-Origin 控制哪些域名可以共享资源,取值如下 Access-Control-Allow-Origin: <origin> ...
- 【leetcode】378. Kth Smallest Element in a Sorted Matrix(TOP k 问题)
Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kt ...
- 顺序栈(C++)
栈的定义为只允许在表的末端进行插入和删除的线性表.简而言之就是先进后出的线性表. 插入和删除的一端被称呼为栈顶(top),而不允许插入删除的一端被称为栈底(bottom).无元素时的栈即为空栈. 使用 ...
- Oracle中的索引
1.Oracle 索引简介 在Oracle数据库中,存储的每一行数据都有一个rowID来标识.当Oracle中存储着大量的数据时,意味着有大量的rowID,此时想要快速定位指定的rowID, ...
- fastjson过滤多余字段
/** * Description:过滤实体中的字段 * @param src 需要过滤的对象,如 list,entity * @param clazz 实体的class ...
- vue2 页面路由
vue官方文档 src/views/Login.vue <template> <div> <h2>登录页</h2> </div> </ ...
- ES6常用的数值转换方法
<script type="text/javascript"> // Number常用方法 /* Number.isFinite() 用来检查一个数值是否为有限的(fi ...
- 【Services】【Web】【apr】安装apr
1. 基础: 1.1 描述:apr全称Apache Portable Runtime,常用于与ssl相关的环境支持,比如openssl,httpd,nginx,tomcat 1.2 链接: 官方网站: ...