题目链接:传送门

题目:

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. SQLServer 对已有数据表添加自增主键

    最近在做老表的数据整理,发现有的表没有主键标识,.NET Core 无法一键生成模型,需要带有主键的表才可以,所以需要针对已有数据添加主键,这是我找到的两种方式. 1. 主键为int 或者bigint ...

  2. C语言获取Linux系统精确时间

    gettimeofday()函数的使用方法 1.函数原型 #include <sys/time.h> int gettimeofday(struct timeval *tv, struct ...

  3. [Java] 项目红色叹号 案例1则

    一般红色叹号是build path出错. 除了检查出错的library外,还要注意Order an Export选项中未勾选的Library. 之前导入项目后,没有勾选JRE和Maven Depend ...

  4. 5、SAMBA服务一:参数详解

    ①:SAMBA服务一:参数详解 ②:SAMBA服务二:配置实例 一.SAMBA简介 samba指SMB(Server Message Block,服务器信息块)协议在网络上的计算机之间远程共享Linu ...

  5. Git中修复bug

    问题描述:提交的远程分支中有一个小bug需要修复: 首先在本地拉取指定分支的代码: git checkout -b test origin/远程分支 git pull 再从test分支中切一个分支: ...

  6. 创建spark_读取数据

    在2.0版本之前,使用Spark必须先创建SparkConf和SparkContext,不过在Spark2.0中只要创建一个SparkSession就够了,SparkConf.SparkContext ...

  7. Win10系列:C#应用控件基础11

    RichEditBox控件 富文本格式是一种跨平台的文档格式,在这种格式的文档中可以编辑文本.图片.链接等内容.通过RichEditBox控件可以对富文本格式的文档进行编辑. 在XAML文件中,Ric ...

  8. Service(服务)

    1.Service是封装了某一特定功能的独立模块: 2.它可以通过注入的方式供别的模块使用: 3.Service分为很多种,包括:值.函数以及应用所需的特性: 4.最简单的Service import ...

  9. idea:打包jar(原文by曲高终和寡)

    idea打包java可执行jar包   1,在项目上鼠标右键 --> Open Module Settings 2, Artifacts --> + --> JAR --> F ...

  10. web工程启动时,在一个类中延迟加载Bean,因为该Bean类可能还没被JVM加载

     问题描述: (1)javaWeb项目启动中,还没启动完成,在下面这个类加载另一个Bean类, (2)通过getBean方法获取到该Bean,可以获取到,不为null (3)但是,调用该Bean的方法 ...