题目链接:传送门

题目:

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. topcoder srm 585 div1

    problem1 link 最优的策略就是从最低下一层开始,每两层的三个节点的子树都可以用一次遍历覆盖. problem2 link 从大到小依次放置每一种数字,并记录已经放置的数字一共有多少个$m$ ...

  2. UnicodeMath数学公式编码_翻译(Unicode Nearly Plain - Text Encoding of Mathematics Version 3)

    目录 完整目录 1. 简介 2. 编码简单数学表达式 2.1 分数 2.2 上标和下标 2.3 空白(空格)字符使用 3. 编码其他数学表达式 3.1 分隔符 强烈推荐本文简明版UnicodeMath ...

  3. CSS布局学习(三) - position属性定义及解释(官网直译)

    static ①元素的位置是在文档正常布局流中的位置. ②设置top right bottom left与z-index无效. ③在未指定position时,static是默认值 以下例子进行说明: ...

  4. LOG4NET用法(个人比较喜欢的用法)

    LOG4NET用法(个人比较喜欢的用法) http://fanrsh.cnblogs.com/archive/2006/06/08/420546.html

  5. webpack不同版本导致的promise不存在问题

    之前采用的axios是基于promise的,但是IE并没有内置promise,所以要提前install一个promise插件: npm install promise import Promise f ...

  6. Confluence 6 服务器硬件要求指南

    服务器管理员可以通过本页面的指南来对在运行 Confluence 评估版本的最小服务器硬件需求进行评估.应为实际的服务器负载是很难进行预测的,所以最好的办法是通过实际运行一个 Confluence 实 ...

  7. mysql,Jdbc工具类,只需一条sql实现简单查询

    import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import ...

  8. centos 安装composer

    1 下载composer.phar curl -sS https://getcomposer.org/installer | php 2 设置全局调用 mv composer.phar /usr/lo ...

  9. 跟随我在oracle学习php(3)

    这次讲一下html中的列表和比较重要的表格 列表分为有序和无序,有序列表与无序列表都是块状元素 <ul>(父标签) 定义无序列表.复合标签(由父标签和子标签组成),不单独出现,用<l ...

  10. 用Nginx给网站做一个简单的防盗链

    目录结构 Nginx防盗链配置 有些时候,大家不想让别人调用自己的图片,一是因为个人版权的问题,再一点就是会增加服务器的负载.还会产生一些没必要的流量. 其实在Nginx里面,很容易就做到防盗链的,在 ...