题目链接:传送门

题目:

F. Summer Practice Report
time limit per test
seconds
memory limit per test
megabytes
input
standard input
output
standard output Vova has taken his summer practice this year and now he should write a report on how it went. Vova has already drawn all the tables and wrote down all the formulas. Moreover, he has already decided that the report will consist of exactly n
pages and the i-th page will include xi tables and yi formulas. The pages are numbered from to n . Vova fills the pages one after another, he can't go filling page i+1
before finishing page i and he can't skip pages. However, if he draws strictly more than k
tables in a row or writes strictly more than k formulas in a row then he will get bored. Vova wants to rearrange tables and formulas in each page in such a way that he doesn't get bored in the process. Vova can't move some table or some formula to another page. Note that the count doesn't reset on the start of the new page. For example, if the page ends with 3
tables and the next page starts with tables, then it's counted as 8 tables in a row. Help Vova to determine if he can rearrange tables and formulas on each page in such a way that there is no more than k
tables in a row and no more than k formulas in a row.
Input The first line contains two integers n
and k (≤n≤⋅, ≤k≤ ). The second line contains n
integers x1,x2,…,xn (≤xi≤) — the number of tables on the i -th page. The third line contains n
integers y1,y2,…,yn (≤yi≤) — the number of formulas on the i -th page.
Output Print "YES" if Vova can rearrange tables and formulas on each page in such a way that there is no more than k
tables in a row and no more than k formulas in a row. Otherwise print "NO".
Examples
Input
Copy Output
Copy YES Input
Copy Output
Copy NO Input
Copy Output
Copy YES Note In the first example the only option to rearrange everything is the following (let table be 'T' and formula be 'F'): page : "TTFTTFT"
page : "TFTTFTT" That way all blocks of tables have length . In the second example there is no way to fit everything in such a way that there are no more than
tables in a row and formulas in a row.

题目大意:

  Vova在写n页报告,每页的报告有xi个表格和yi个公式,他只能连续写最多k个表格(公式),然后就要切换到写公式(表格),否则就会感到疲惫。

  Vova只能在写完第i页之后才能开始写第i+1页。问他是否能写完这n页报告而不感到疲惫。

  Vova再翻页的时候疲惫值不会更新。就是说之前那页写了a个表格的话,下一页若要先写表格,只能再连续写k-a个表格了。

思路:

  因为Vova只能在写完前一页之后才能写下一页,所以只能贪心地处理当前页

  对当前页有这样的写法:(因为表格和公式地位相等,不妨只考虑当前页先写表格的情况)

  ①:yi ≤ xi,那么就要尽量多地写表格。最好就是写k个表格之后写1个公式,再写k个表格、1个公式。。。最后还能写k个表格。这样的写法下,只要xi,yi满足yi ≤ xi ≤ k*yi + k,就能写完所有的表格。

  ②:xi < yi,此时要尽量多地写公式。最好就是写1个表格之后写k个公式,再写1个表格、k个公式。。。(这里不要再写1个表格,不然就少写了k个公式)这样的写法下,只要xi,yi满足xi > $\left \lceil y_{i}/k \right \rceil$,就能写完所有的公式。

  先写公式也是类似的。

  然后要考虑当前页写完之后对下一页的影响。前一页最后一步写的是公式(表格),会影响下一页先写公式(表格)的最大长度。此时只要记录前一页的最后一步写公式(表格)时的最短长度,在下一页先写公式(表格)时加上这个长度再进行上面①②的写法就可以了。注意:如果最后一步写的是表格(公式)的话,那么公式(表格)的长度就是0了。

  然后剩余长度不为0的情况:不妨设剩余的为公式(y),那这种情况下只能是yi > k*xi,尽量多地写公式还写不完。。。显然剩余的就是tmp = yi - k*xi了,然后如果tmp > k,那么说明根本写不完公式,那就肯定会“疲劳”了,输出NO。否则如果一直写到了最后一页,那就是YES。

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MAX_N = 3e5 + ; int N, K;
//-1为不可能,其他为剩余的flag(0x,1y)的值。
bool ans;
ll judge(int flag, ll x, ll y)
{//y开始,剩y
if(!flag)
swap(x, y);
ll l = y/K + (y%K > );
ll r = K*y + K;
if (l <= x && x <= r)
return ;
ll tmp = y - K*x;
return tmp;
} ll f[MAX_N][];//0:x, 1:y
ll x[MAX_N], y[MAX_N]; int main()
{
cin >> N >> K;
ans = true;
for (int i = ; i <= N; i++)
scanf("%lld", x+i);
for (int i = ; i <= N; i++)
scanf("%lld", y+i); f[][] = f[][] = ;
for (int i = ; i <= N; i++) {
f[i][] = judge(, x[i]+f[i-][], y[i]);
f[i][] = judge(, x[i], y[i]+f[i-][]);
if (f[i][] > K || f[i][] > K) {
ans = false;
break;
}
}
if (ans)
puts("YES");
else
puts("NO");
return ;
}

Codeforces1076F. Summer Practice Report(贪心+动态规划)的更多相关文章

  1. Codeforces 1076F Summer Practice Report dp

    Summer Practice Report dp[ i ][ 0 ]表示放完前 i 页, 第 i 页最后一段是 0, 0个数的最小值. dp[ i ][ 1 ]表示放完前 i 页, 第 i 页最后一 ...

  2. 【51Nod】1510 最小化序列 贪心+动态规划

    [题目]1510 最小化序列 [题意]给定长度为n的数组A和数字k,要求重排列数组从而最小化: \[ans=\sum_{i=1}^{n-k}|A_i-A_{i+k}|\] 输出最小的ans,\(n \ ...

  3. nyoj 16-矩形嵌套(贪心 + 动态规划DP)

    16-矩形嵌套 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:13 submit:28 题目描述: 有n个矩形,每个矩形可以用a,b来描述,表示长和 ...

  4. POJ1065 Wooden Sticks(贪心+动态规划——单调递减或递增序列)

    描述 C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于 第i个处理的木棒,那么将不会耗费时间,否则 ...

  5. BZOJ 3227 [Sdoi2008]红黑树(tree) ——贪心 动态规划

    首先可以想到一个贪心的方法,然后一层一层的合并. 也可以采用动态规划的方式,为了写起来好写,把点数*2+1,然后发现在本机上跑不过1500的数据. 交上去居然A掉了. 贪心 #include < ...

  6. HDOJ-1257(贪心/动态规划)

    最少拦截系统 HDOJ-1257 我做这题的思路就是采用暴力或者贪心.也就是每次循环选出从第一个未被选择的元素开始,依次把后面可以选择的元素作为一个系统.最后统计可以有多少个系统. 还有人的思路就是利 ...

  7. UOJ#110. 【APIO2015】Bali Sculptures 贪心 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ110.html 题解 我们发现n=2000 的子任务保证A=1! 分两种情况讨论: $n\leq 100$ ...

  8. AtCoder Grand Contest 026 (AGC026) E - Synchronized Subsequence 贪心 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC026E.html 题目传送门 - AGC026E 题意 给定一个长度为 $2n$ 的字符串,包含 $n$ ...

  9. 【BZOJ4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+动态规划

    [BZOJ4922][Lydsy六月月赛]Karp-de-Chant Number Description 卡常数被称为计算机算法竞赛之中最神奇的一类数字,主要特点集中于令人捉摸不透,有时候会让水平很 ...

随机推荐

  1. 剑指offer(55)链表中环的入口节点

    题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目分析 1.一快一慢指针,先找到碰撞点. 2.然后碰撞点到入口节点的距离就是头结点到入口节点的距离. 具体原理可 ...

  2. webpack(import路径配置)(自动打开浏览器)(自定义运行命令)

  3. HTML基础【2】:基础标签

    H系列标签(H1 ~ H6) 作用: 用于给文本添加标题语义 格式: <h1>xxxxxx</h1> 注意点 H 标签是用来给文本添加标题语义的,而不是用来修改文本的样式的 H ...

  4. redis哨兵机制讲解

    原文链接:https://blog.csdn.net/yswKnight/article/details/78158540 一.什么是哨兵机制? 答:Redis的哨兵(sentinel) 系统用于管理 ...

  5. jquery 获取当前点击的是谁

    //今天早上有人问这个问题 想着没写过jquery的笔记 //那就随便写一下吧 下面两个方法 根据不同情况而定用哪种方法$(".class").click(function(){ ...

  6. Microsoft Visual Studio已停止工作

    问题:今天在安装Visual Studio时,提示“Visual Studio installer 已停止工作” 解决办法:卸载原有的 .net  framework,在微软官网下载 .net fra ...

  7. navicat 链接 mysql 报错1251

    使用版本: navicat for mysql 10.1.7版主 mysql-8.0.11-winx64 版本 报错原因:navicat版本太低(使用新版本navicat或者使用旧版本mysql) 解 ...

  8. Python语法注意点

    1. 在Python中定义函数,可以用必选参数.默认参数.可变参数.关键字参数和命名关键字参数,这5种参数都可以组合使用.但是请注意,参数定义的顺序必须是:必选参数.默认参数.可变参数.命名关键字参数 ...

  9. vue-循环并获取dom元素

    <ul class="picBox"> <li v-for="(item,index) in picArr" ><img :src ...

  10. linux下查看进程命令

    他们都是用来显示当前运行的进程,但是: ps -aux 是用BSD的格式来显示python这个进程显示的项目有:USER , PID , %CPU , %MEM , VSZ , RSS , TTY , ...