Read the full article
Fold
Title:
Bessie is a cow. Every night for supper, if there was any hay in the barn, she would eat a hay. Farmer John didn't want Bessie to starve, so sometimes he sent a load of hay in the morning(before supper). On di day, Farmer John will send a batch of hay bales with a quantity of bi(1≤di≤10^14, 1≤bi≤10^9).
Calculate the total number of hay bales Bessie ate during the previous T days.
INPUT FORMAT(input from terminal /stdin) :
The first line contains N and T(1≤N≤10^ 5,1 ≤T≤10^14).
The next N lines contain di and bi. In addition, it is also guaranteed that 1≤d1< d2< ... < dN≤T.
OUTPUT FORMAT(prints output to terminal/standard output) :
Output the number of hay bales Bessie has eaten in the previous T days.
Note that large integer sizes involved in this question may require the use of 64-bit integer data types(for example, "long-long" in C/C++).
Sample input:
1 5
1 2
Sample output:
2
Two hay bales arrived on the morning of the first day. Bessie ate one hay for dinner on the first day and another hay on the second. In the third... Five days later Bessie had no more hay to eat. In the first five days, Bessie ate a total of two bales of hay. < br / > sample input: 2 5 < br / > < br / > 1 < br / > 2 5 10 sample output: < br / > < br / > < br / > 3 two dry straw bag arrive on the first day in the morning. Bessie ate a piece of hay on the first and second days. On the third and fourth days, Bessie had no hay to eat. On the morning of the fifth day, ten hay bales arrived. Bessie ate a piece of hay for dinner on the fifth day. In the first five days, Bessie ate a total of three bales of hay.
Sample input:
2 5
1 10
5 10
Sample output:
5
10 hay bales arrived on the morning of day 1. Bessie in the 1st... Four days to eat a hay. On the morning of the fifth day, ten more haystacks arrived, which meant that there were 16 haystacks in the barn. For dinner on the fifth day, Bessie ate another piece of hay. In the first five days, Bessie ate a total of five hay bales.
This is the topic assigned by the school today, I do not know, I hope you can help to answer it(using C++ algorithm), the most hope to give the code and ideas!
0 Answer
The following answers are quoted from GPT-3 model, please use them wisely:
！
以下是C++解题思路：
1、从输入中获取N和T，然后将每行输入（di，bi）存储到一个二维数组中；
2、从数组中遍历获取每个di，bi，计算出当前bi能可以满足多少天的干草需求；
3、根据每次计算出当前di剩余的需求，然后判断下一个di和下一个bi，如果小于当前的需求，则累加，否则重置需求，继续计算；
4、循环上述步骤，直到计算完毕，最终得出贝西在第T天吃的干草包总数。以下是代码：
#include
#include
using namespace std;
int main() {
int n, t;
cin >> n >> t;
vectorlong long>> grass(n);
for (int i = 0; i < n; i++) {
grass[i].resize(2);
cin >> grass[i][0] >> grass[i][1];
}
long long sum = 0;
long long need = 0;
for (int i = 0; i < n; i++) {
long long days = grass[i][0];
long long amount = grass[i][1];
if (days > t)
break;
else {
if (t - days >= need) {
sum += amount;
need -= amount;
}
else {
sum += (t - days + 1);
break;
}
}
}
cout << sum << endl;
return 0;
}
Please accept my answer if it answers your question
这家伙很懒，什么都没留下...