视频加载失败

P10189 [USACO24FEB] Maximizing Productivity B 题解

P10189 [USACO24FEB] Maximizing Productivity B 题解
865 字
4 分钟
P10189 [USACO24FEB] Maximizing Productivity B 题解

先说说暴力做法:

每次遍历一遍,看看是否满足 ti+s≤cit_i + s \le c_i,满足就计数,不满足就挂。单次时间复杂度显然为 O(N)O(N),总得时间复杂度约为 O(NQ)O(NQ),TLE 是肯定的~

暴力代码:

// Problem: Problem 3. Maximizing Productivity
// Contest: USACO - USACO 2024 February Contest, Bronze
// URL: https://usaco.org/index.php?page=viewproblem&cpid=1385
// Memory Limit: 256 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)
/*Code by Block_Fish*/
#include <bits/stdc++.h>
#define log printf
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define FOR(i, l, r) for (int(i) = (l); (i) <= (r); ++(i))
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
using namespace std;
typedef __int128 i128;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
int n, q, c[N], t[N];
template <typename T>
inline T read() {
T sum = 0, fl = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = sum * 10 + ch - '0';
return sum * fl;
}
template <typename T>
inline void write(T x) {
static T sta[35];
int top = 0;
do { sta[top++] = x % 10, x /= 10; } while (x);
while (top) putchar(sta[--top] + 48);
}
int main() {
n = read<int>(), q = read<int>();
FOR(i, 1, n) c[i] = read<int>();
FOR(i, 1, n) t[i] = read<int>();
FOR(i, 1, q) {
int v = read<int>(), s = read<int>(), cnt = 0;
FOR(j, 1, n) if (s + t[j] < c[j])++ cnt;
if (cnt >= v) puts("YES");
else puts("NO");
}
return 0;
}

评测记录,开了优化也只有 20pts。


暴力废了,开始整正解。

2×1052 \times 10^5?二分法,了解一下?

然后,然后遇到了个问题:二分这东西要求有序,可是 cic_i 与 tit_i 是绑定在一起的啊!咋整?

注意 cic_i 与 ci−tic_i - t_i 没关系,而这个式子能够求出啥啊?想要去到第 ii 个农场最大的 SS 嘛(ci−ti+ti=cic_i - t_i + t_i = c_i,刚好卡点儿到,不可能再晚了)!

总结一下,问题相当于预处理出来一个数组 aa,使 ai=ci−tia_i = c_i - t_i,求数组中是否有至少 VV 个数满足 ai≥Sa_i \ge S。

先排个序(不然还是 O(n)O(n) 嘛),然后就可以分出来两种做法:

做法一:二分,用 upper_bound 函数(因为是大于等于),时间复杂度约为 O(QlogN)O(QlogN)。

做法二:排完序后显然满足 aV≥aV−1≥aV−2...a1a_V \ge a_{V - 1} \ge a_{V - 2} ... a_1,因此如果 aVa_V 都小于等于 SS 了,那它前面的自然也都满足。因此只需要判断 aV≥Sa_V \ge S 成不成立即可。时间复杂度约为 O(Q)O(Q)。

赛时 ACCode(做法二):

// Problem: Problem 3. Maximizing Productivity
// Contest: USACO - USACO 2024 February Contest, Bronze
// URL: https://usaco.org/index.php?page=viewproblem&cpid=1385
// Memory Limit: 256 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)
/*Code by Block_Fish*/
#include <iostream>
#include <algorithm> // 万能头CE
#define log printf
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define FOR(i, l, r) for (int(i) = (l); (i) <= (r); ++(i))
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
using namespace std;
typedef __int128 i128;
typedef long long ll;
typedef pair<int, int> PII;
int N, Q;
int close[200010], t[200010], ans[200010], V, S;
template <typename T>
inline T read() {
T sum = 0, fl = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = sum * 10 + ch - '0';
return sum * fl;
}
template <typename T>
inline void write(T x) {
static T sta[35];
int top = 0;
do { sta[top++] = x % 10, x /= 10; } while (x);
while (top) putchar(sta[--top] + 48);
}
bool x(int a, int b) { return a > b; }
int main() {
cin >> N >> Q;
for (int i = 1; i <= N; i++) { cin >> close[i]; }
for (int i = 1; i <= N; i++) { cin >> t[i]; }
for (int i = 1; i <= N; i++) { ans[i] = close[i] - t[i]; }
sort(ans + 1, ans + N + 1, x);
while (Q--) {
cin >> V >> S;
if (ans[V] > S) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}

AC记录~

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!

打赏
P10189 [USACO24FEB] Maximizing Productivity B 题解
https://www.leo2011.eu.org/posts/luogu-p10189/
作者
Block_Fish
发布于
2024-02-29
许可协议
SATA

评论区

Profile Image of the Author
Block_Fish
大胆假设,小心求证。——胡适
公告
网站全面重构,有问题欢迎联系积木鱼!
分类
标签
最新动态
站点统计
文章
56
分类
7
标签
17
总字数
84,805
运行时长
0 天
最后活动
0 天前
站点信息
构建平台
Netlify CI
博客版本
Firefly v6.16.8
文章许可
SATA

当前页面没有目录

文章目录

当前页面没有目录