Berland Fair CodeForces - 1073D (单调栈优化 无用???)
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;
}