Berland Fair CodeForces - 1073D (单调栈优化 无用???)

探讨在有限预算下,如何在排列成圈的摊位中,遵循特定规则购买最多糖果的算法策略。通过分析每个摊位的糖果价格,计算在遵循规则的情况下,能够购买的糖果总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1. The i-th booths sells some candies for the price of ai burles per item. Each booth has an unlimited supply of candies.

Polycarp has decided to spend at most T burles at the fair. However, he has some plan in mind for his path across the booths:

at first, he visits booth number 1;
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 n and 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 n integers a1,a2,…,an (1≤ai≤109) — the price of the single candy at booth number i.

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 1, buys candy for 5, T=33;
Booth 2, buys candy for 2, T=31;
Booth 3, buys candy for 5, T=26;
Booth 1, buys candy for 5, T=21;
Booth 2, buys candy for 2, T=19;
Booth 3, buys candy for 5, T=14;
Booth 1, buys candy for 5, T=9;
Booth 2, buys candy for 2, T=7;
Booth 3, buys candy for 5, T=2;
Booth 1, buys no candy, not enough money;
Booth 2, buys candy for 2, T=0.
No candy can be bought later. The total number of candies bought is 10.

In the second example he has 1 burle left at the end of his path, no candy can be bought with this amount.

//确实更耗时了 ai!!!!

#pragma warning(disable:4996)
#include"iostream"
#include"functional"
#include"algorithm"
#include"cstring"
#include"stack"
#include"cmath"
#include"queue"
#include"vector"
#include"map"
typedef long long int ll;
using namespace std;
const ll ma = 1e6 + 5;
ll s[ma], ak[ma];
stack<ll> q;
int main() {
	ll a, b, sum = 0, mi;
	scanf("%lld%lld", &a, &b);
	scanf("%lld", &s[0]);
	mi = s[0];
	sum += s[0];
	for (ll i = 1; i < a; i++) {
		scanf("%lld", &s[i]);
		sum += s[i];
		mi = min(mi, s[i]);
	}
	ll ans = 0;
	ll ax = a;
	while (b >= mi) {
		if (b >= sum) {
			ans += (b / sum) * ax;
			b %= sum;
		}
		else {
			sum = 0;
			ax=0;
			for (int i = 0; b >= mi && i < a; i++ ) {
				if (b >= s[i]) {
					b -= s[i];
					sum += s[i];
					ax++;
					ans++;
				}
				
			}
		}
	}
	cout << ans;

}
#pragma warning(disable:4996)
#include"iostream"
#include"functional"
#include"algorithm"
#include"cstring"
#include"stack"
#include"cmath"
#include"queue"
#include"vector"
#include"map"
typedef long long int ll;
using namespace std;
const ll ma = 1e6 + 5;
ll s[ma], ak[ma];
stack<ll> q;
int main() {
	ll a, b, sum = 0, mi;
	scanf("%lld%lld", &a, &b);
	scanf("%lld", &s[0]);
	mi = s[0];
	sum += s[0];
	for (ll i = 1; i < a; i++) {
		scanf("%lld", &s[i]);
		sum += s[i];
		mi = min(mi, s[i]);
	}
	for (ll i = a - 1; i >= 0; i--) {
		while (!q.empty() && s[q.top()] >= s[i]) q.pop();
		if (q.empty()) ak[i] = a + 1;
		else {
			ak[i] = q.top();
		}
		q.push(i);
	}
	ll ans = 0;
	ll ax = a;
	while (b >= mi) {
		if (b >= sum) {
			ans += (b / sum) * ax;
			b %= sum;
		}
		else {
			sum = 0;
			ax=0;
			for (int i = 0; b >= mi && i < a; ) {
				if (b >= s[i]) {
					b -= s[i];
					sum += s[i];
					ax++;
					ans++;
					i++;
				}
				else {
					i = ak[i];
				}
			}
		}
	}
	cout << ans;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值