题目传送门

  题目大意:一圈人围起来卖糖果,标号从1-n,每个位置的糖果都有自己的价格,一个人拿着钱从q开始走,能买则买,不能买则走到下一家,问最多能买多少件物品。

  思路:此题的关键是不能买则走到下一家,一旦走到下一家,我们会发现之前的这家以后无论转几圈我们都买不起,所以直接把这个店删掉就可以了。

  于是先将n当成周期,算出此时的sum,和原来的money比较,能买几个周期则买几个周期,然后遍历双向链表,不能买则删去,更新周期和sum,继续判断能不能买得起此时的周期,然后走到下一家店,直到剩下一家店。

  由于每家店最多被删去一次,所以时间复杂度是O(n)的,只要注意链表的一些细节处理就可以了。

//#pragma comment(linker,"/STACK:102400000,102400000")
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<stdlib.h>
//#include<unordered_map>
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)|1
#define CLR(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
typedef long long ll;
using namespace std;
inline ll read(){
ll x=,f=;
char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;}
const int maxn=2e5+;
struct node{
ll val;
int pre,Next;
}a[maxn];
ll sum,money,ans;
int main(){
int n;
cin>>n>>money;
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i].val);
sum+=a[i].val;
a[i].pre=i-,a[i].Next=i+;
}
a[].pre=n,a[n].Next=;
if(money>=sum)
{
ans+=money/sum*n;
money%=sum;
}
int i=;
while(a[i].Next!=i)
{
if(money>=a[i].val)
{
ans++; money-=a[i].val;
if(money<=)break;
i=a[i].Next;
}else{
a[a[i].pre].Next=a[i].Next;
a[a[i].Next].pre=a[i].pre;
sum-=a[i].val;
n--;
if(money>=sum&&a[i].Next!=a[i].pre)
{
ans+=money/sum*n;
money%=sum;
}
if(money<=)break;
i=a[i].Next;
}
}
if(money>=sum)
{
ans+=money/sum*n;
money%=sum;
}
printf("%lld\n",ans);
}
D. 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 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.

Input

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.

Output

Print a single integer — the total number of candies Polycarp will buy.

Examples
input

Copy
3 38
5 2 5
output

Copy
10
input

Copy
5 21
2 4 100 2 6
output

Copy
6
Note

Let's consider the first example. Here are Polycarp's moves until he runs out of money:

  1. Booth 11, buys candy for 55, T=33T=33;
  2. Booth 22, buys candy for 22, T=31T=31;
  3. Booth 33, buys candy for 55, T=26T=26;
  4. Booth 11, buys candy for 55, T=21T=21;
  5. Booth 22, buys candy for 22, T=19T=19;
  6. Booth 33, buys candy for 55, T=14T=14;
  7. Booth 11, buys candy for 55, T=9T=9;
  8. Booth 22, buys candy for 22, T=7T=7;
  9. Booth 33, buys candy for 55, T=2T=2;
  10. Booth 11, buys no candy, not enough money;
  11. 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.

最新文章

  1. mysqlnd cannot connect 连接错误处理方法
  2. XAF应用开发教程(八) 汉化与多国语言支持
  3. loj 1251(2-sat + 输出一组可行解)
  4. UVa 10047,独轮车
  5. Android文件Apk下载变ZIP压缩包
  6. Umbraco中的权限体系结构
  7. (hdu)1042 N! 大数相乘
  8. ICE之C/S通信原理
  9. SVN无法修改以前提交日志的办法
  10. jvm工具的使用
  11. 双核 CPU
  12. win7彻底卸载iis
  13. 股票K线图
  14. 阿里云 Windows Server 2012 r2 部署asp.net mvc网站 平坑之旅
  15. 所活天数!java Date应用
  16. 【redis】redis配置文件参数解析
  17. 最接近原点的K个点
  18. Linux使用nexus搭建maven私服
  19. 关于break,return,和coutiune
  20. CocosCreator引擎修改与定制

热门文章

  1. 33.HAVING 子句
  2. cmake的一些词的解释
  3. git vs sourcetree
  4. [.net 多线程]CountdownEvent
  5. Flask写web时cookie的处理
  6. 数组中 reduce累计运算
  7. WebStrom常用快捷键
  8. 在Linux环境下的卸载Oracle11G操作
  9. 《spring 攻略》笔记1
  10. windows查看和杀死占用端口的进程