题目传送门

  题目大意:一圈人围起来卖糖果,标号从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.

codeforces1073d Berland Fair 思维(暴力删除)的更多相关文章

  1. 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 ...

  2. ZOJ - 3983 - Crusaders Quest(思维 + 暴力)

    题意: 给出一个字符串,长度为9,包含三种各三个字母"a","g","o",如果一次消除连续三个一样的分数+1,消完自动向左补齐 其中可以消 ...

  3. CodeForces - 1073D Berland Fair

    XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nnbooths, arranged in ...

  4. 【数据结构】【CF1073D】 Berland Fair

    Description 给定 \(n\) 个商店,他们围成一个圆圈,按照顺时针从 \(1\) 到 \(n\) 编号.你有 \(T\) 元钱,从 \(1\) 号点开始按照顺时针方向走,每到一个商店,只要 ...

  5. Codeforces 1073D:Berland Fair(模拟)

    time limit per test: 2 secondsmemory limit per test: 256 megabytesinput: standard inputoutput: stand ...

  6. Nikita and string [思维-暴力] ACM

    codeforces Nikita and string time limit per test   2 seconds memory limit per test   256 megabytes O ...

  7. [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,如果 ...

  8. codeforce 429D. Tricky Function (思维暴力过)

    题目描述 Iahub and Sorin are the best competitive programmers in their town. However, they can't both qu ...

  9. codeforces 768 C. Jon Snow and his Favourite Number(思维+暴力)

    题目链接:http://codeforces.com/contest/768/problem/C 题意:给出n个数,k个操作,和一个x,每次操作先排序然后对奇数位数进行xor x操作,最后问k次操作后 ...

随机推荐

  1. 关于equal和toString方法的实验报告

    一 实验目的 了解equal和toString方法 二 实验软件环境 操作系统:windows xp java version: "1.7.0_51" 开发工具:Eclipse S ...

  2. jQuery 给class附点击事件获取对应的索引

    有一类div标签,class为pointbox,数量不等,有多个.我需要在点击某一个标签的时候实时获取该标签在这类标签中索引值,以便进行其他操作. 代码很简单: $(".pointbox&q ...

  3. 关于mysql自增字段问题

    最近遇到mysql字段的自增问题,需要临时处理一下,然后就顺便补补课,这样就有了这样一篇文章. 1.自增值是什么 他是一个字段属性,是用来创建唯一标识的列的 The AUTO_INCREMENT at ...

  4. Bean管理注解的例子

  5. spring.net 集成nhibernate配置文件(这里暴露了GetCurrentSession 对于 CurrentSession unbond thread这里给出了解决方法)

    我这里主要分成了两个xml来进行spring.net管理实际情况中可自己根据需要进行分类 Dao2.xml <?xml version="1.0" encoding=&quo ...

  6. jqgrid 编辑表格(包含下拉框)

    .1在jqgrid 按钮 <asp:JQGridColumn TextAlign=" DataField="act" Visible="True" ...

  7. 【原创】请不要对Boost Format使用Byte作为参数

    曾几何时我们可以肆无忌惮的对sprintf传入BYTE等类型作为参数,只要你指定的为%D即可打印出对应的数字 但是boost format不可以,当你发生类型截断,错误,异常,请尽快查看你传入的类型是 ...

  8. shell 字符串中定位字符位置 获取字符位置

    linux shell 字符串操作(长度,查找,替换)详解 该博文中描述的如下两个字符串操作, ${string:position} #在$string中, 从位置$position开始提取子串 ${ ...

  9. java实现链式队列

    java实现链式队列...比较简单 package datastruct; public class QueueLink implements Queue { // 定义一个节点内部类 class N ...

  10. ERC230 VS ERC223

    ERC223对ERC220的改进 ERC223是以太坊上最新的代币(token)接口标准,主要是为了解决ERC220代币转账丢失问题,那么怎么解决的呢,一起来看看. 1. ERC220 存在问题 ER ...