当前位置: 首页 > news >正文

【Codeforces】CF 2014 G

Milky Days

#模拟 #贪心 #枚举

题目描述

Little John is as little as night is day — he was known to be a giant, at possibly 2.1 2.1 2.1 metres tall. It has everything to do with his love for milk.

His dairy diary has n n n entries, showing that he acquired a i a_i ai pints of fresh milk on day d i d_i di. Milk declines in freshness with time and stays drinkable for a maximum of k k k days. In other words, fresh milk acquired on day d i d_i di will be drinkable between days d i d_i di and d i + k − 1 d_i+k-1 di+k1 inclusive.

Every day, Little John drinks drinkable milk, up to a maximum of m m m pints. In other words, if there are less than m m m pints of milk, he will drink them all and not be satisfied; if there are at least m m m pints of milk, he will drink exactly m m m pints and be satisfied, and it’s a milk satisfaction day.

Little John always drinks the freshest drinkable milk first.

Determine the number of milk satisfaction days for Little John.

输入格式

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1\leq t \leq 10^4 1t104), the number of test cases.

The first line of each test case consists of three integers n n n, m m m, k k k ( 1 ≤ n 1\le n 1n, m m m, k ≤ 1 0 5 k \le 10^5 k105), the number of diary entries, the maximum pints needed for a milk satisfaction day, and the duration of milk’s freshness.

Then follow n n n lines of each test case, each with two integers d i d_i di and a i a_i ai ( 1 ≤ d i 1\le d_i 1di, a i ≤ 1 0 6 a_i \le 10^6 ai106), the day on which the milk was acquired and the number of pints acquired. They are sorted in increasing values of d i d_i di, and all values of d i d_i di are distinct.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

输出格式

For each test case, output a single integer, the number of milk satisfaction days.

样例 #1

样例输入 #1

6
1 1 3
1 5
2 3 3
1 5
2 7
4 5 2
1 9
2 6
4 9
5 6
5 2 4
4 7
5 3
7 1
11 2
12 1
4 1 3
5 10
9 4
14 8
15 3
5 5 5
8 9
10 7
16 10
21 5
28 9

样例输出 #1

3
3
4
5
10
6

解题思路

这题要求优先喝新鲜的牛奶,也就是先进先出,我们可以使用一个栈来维护这个过程。

首先,枚举天数显然是容易超时的,因为枚举天数的话,对于没喝完的牛奶还会再次入栈,这样时间复杂度明显过大了。

那么,我们应该枚举购买记录才对,维护两次购买牛奶之间的天数之间的牛奶信息,存入栈内,这样整体的时间复杂度最多就是 O ( n ) O(n) O(n)了。

一个细节就是,我们可以最后在栈内插入一个 i n f inf inf的天数,价值为 0 0 0的哨兵,这样就不用特判最后一个购买记录了。

代码

const int N = 1e5 + 10;void solve() {int n, m, k;std::cin >> n >> m >> k;std::vector<pii>a(n + 2);for (int i = 1; i <= n; ++i) {std::cin >> a[i].first >> a[i].second;}a[n + 1] = { inf, 0LL };int lstd = 0 , res = 0;std::stack<pii>S;for (int i = 1;i<=n+1;++i) {auto [d, v] = a[i];int now = m;while (lstd < d && S.size()) {auto [pd, pv] = S.top(); S.pop();if (pd + k <= lstd) continue; //过期了if (pv >= now) {int s = std::min({ d - lstd, pd + k - lstd, (pv - now) / m  + 1}); res += s; lstd += s;pv -= now + (s - 1) * m;now = m;if (pv) S.push({ pd,pv });}else {now -= pv;}}S.push({ d,v });lstd = d;}std::cout << res << "\n";
}signed main() {std::ios::sync_with_stdio(0);std::cin.tie(0);std::cout.tie(0);int t = 1;std::cin >> t;while (t--) {solve();}
};

http://www.mrgr.cn/news/44754.html

相关文章:

  • 感知机学习算法
  • 手机一键换IP地址软件:功能、应用与选择指南‌
  • 基于SpringBoot+Vue+MySQL的药品信息管理系统
  • C语言文件操作(上)(27)
  • python的字典介绍
  • Leetcode 3310. Remove Methods From Project
  • JavaScript(JS)基础(一)
  • 算法题总结(十)——二叉树上
  • 货仓选址(贪心)
  • 制作U盘启动盘1 — UltraISO
  • 操作系统实验之内存管理
  • 分享一个我开发的操作系统镜像下载站
  • 点,点间连接的数学构型系统
  • javaScript操作节点(6个案例+代码+效果)
  • javaScript操作dom的事件(3个案例+代码+效果图)
  • k8s学习
  • ArcGIS实战——一文教会你调整适合中国宝宝体质的标准地图投影参数
  • 文件处理不再难:带你轻松攻克C语言文件操作
  • C++中的模板template
  • 操作系统 | 学习笔记 | 王道 | 3.2 虚拟内存管理