WY62. 瞌睡
描述
小易觉得高数课太无聊了,决定睡觉。不过他对课上的一些内容挺感兴趣,所以希望你在老师讲到有趣的部分的时候叫醒他一下。你知道了小易对一堂课每分钟知识点的感兴趣程度,并以分数量化,以及他在这堂课上每分钟是否会睡着,你可以叫醒他一次,这会使得他在接下来的k分钟内保持清醒。你需要选择一种方案最大化小易这堂课听到的知识点分值。输入描述
第一行 n (1 <= n <= 105), k (0 <= k <= 105) ,表示这堂课持续多少分钟,以及叫醒小易一次使他能够保持清醒的时间。输出描述
小易这堂课听到的知识点的最大兴趣值。示例1
输入:
6 3 1 3 5 2 5 4 1 1 0 1 0 0
输出:
16
C++14 解法, 执行用时: 7ms, 内存消耗: 1036KB, 提交时间: 2020-08-24
#include <stdio.h> using namespace std; template <class T> inline void scan_d(T &ret) { char c; ret = 0; while ((c = getchar()) < '0' || c > '9'); while (c >= '0' && c <= '9') { ret = ret * 10 + (c - '0'), c = getchar(); } } int main() { int n, k; // scanf("%d %d", &n, &k); scan_d(n); scan_d(k); // cin >> n >>k; int interest[100001] = {0}; int wake[100001] = {0}; for(int i = 0; i < n; i++) { scan_d(interest[i]); } int max = 0; int sum = 0; for(int i = 0; i < n; i++) { scan_d(wake[i]); if(wake[i]) sum += interest[i]; } int start_i = 0; int sleep_sum = 0; for(int i =0; i < n; i++) { if(wake[i] == 0) { start_i = i; for(int j = i; j < i + k && j < n; j++) { if(wake[j] == 0) sleep_sum += interest[j]; } break; } } max = sleep_sum; for(int i = start_i; i < n - k; i ++) { if(wake[i] == 0 && wake[i + k] == 0) { sleep_sum = sleep_sum - interest[i] + interest[i + k]; } else if(wake[i] == 1 && wake[i + k] == 0) { sleep_sum += interest[i + k]; } else if(wake[i] == 0 && wake[i + k] == 1) { sleep_sum -= interest[i]; } if(sleep_sum > max) max = sleep_sum; } sum += max; printf("%d\n", sum); return 0; }
C++14 解法, 执行用时: 8ms, 内存消耗: 1704KB, 提交时间: 2020-10-05
#include <stdio.h> using namespace std; template <class T> inline void scan_d(T &ret) { char c; ret = 0; while ((c = getchar()) < '0' || c > '9'); while (c >= '0' && c <= '9') { ret = ret * 10 + (c - '0'), c = getchar(); } } int main() { int n, k; // scanf("%d %d", &n, &k); scan_d(n); scan_d(k); // cin >> n >>k; int interest[100001] = {0}; int wake[100001] = {0}; for(int i = 0; i < n; i++) { scan_d(interest[i]); } int max = 0; int sum = 0; for(int i = 0; i < n; i++) { scan_d(wake[i]); if(wake[i]) sum += interest[i]; } int start_i = 0; int sleep_sum = 0; for(int i =0; i < n; i++) { if(wake[i] == 0) { start_i = i; for(int j = i; j < i + k && j < n; j++) { if(wake[j] == 0) sleep_sum += interest[j]; } break; } } max = sleep_sum; for(int i = start_i; i < n - k; i ++) { if(wake[i] == 0 && wake[i + k] == 0) { sleep_sum = sleep_sum - interest[i] + interest[i + k]; } else if(wake[i] == 1 && wake[i + k] == 0) { sleep_sum += interest[i + k]; } else if(wake[i] == 0 && wake[i + k] == 1) { sleep_sum -= interest[i]; } if(sleep_sum > max) max = sleep_sum; } sum += max; printf("%d\n", sum); return 0; }