Codeforces1076F. Summer Practice Report(贪心+动态规划)
题目链接:传送门
题目:
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(贪心+动态规划)的更多相关文章
- Codeforces 1076F Summer Practice Report dp
Summer Practice Report dp[ i ][ 0 ]表示放完前 i 页, 第 i 页最后一段是 0, 0个数的最小值. dp[ i ][ 1 ]表示放完前 i 页, 第 i 页最后一 ...
- 【51Nod】1510 最小化序列 贪心+动态规划
[题目]1510 最小化序列 [题意]给定长度为n的数组A和数字k,要求重排列数组从而最小化: \[ans=\sum_{i=1}^{n-k}|A_i-A_{i+k}|\] 输出最小的ans,\(n \ ...
- nyoj 16-矩形嵌套(贪心 + 动态规划DP)
16-矩形嵌套 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:13 submit:28 题目描述: 有n个矩形,每个矩形可以用a,b来描述,表示长和 ...
- POJ1065 Wooden Sticks(贪心+动态规划——单调递减或递增序列)
描述 C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于 第i个处理的木棒,那么将不会耗费时间,否则 ...
- BZOJ 3227 [Sdoi2008]红黑树(tree) ——贪心 动态规划
首先可以想到一个贪心的方法,然后一层一层的合并. 也可以采用动态规划的方式,为了写起来好写,把点数*2+1,然后发现在本机上跑不过1500的数据. 交上去居然A掉了. 贪心 #include < ...
- HDOJ-1257(贪心/动态规划)
最少拦截系统 HDOJ-1257 我做这题的思路就是采用暴力或者贪心.也就是每次循环选出从第一个未被选择的元素开始,依次把后面可以选择的元素作为一个系统.最后统计可以有多少个系统. 还有人的思路就是利 ...
- UOJ#110. 【APIO2015】Bali Sculptures 贪心 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ110.html 题解 我们发现n=2000 的子任务保证A=1! 分两种情况讨论: $n\leq 100$ ...
- AtCoder Grand Contest 026 (AGC026) E - Synchronized Subsequence 贪心 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC026E.html 题目传送门 - AGC026E 题意 给定一个长度为 $2n$ 的字符串,包含 $n$ ...
- 【BZOJ4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+动态规划
[BZOJ4922][Lydsy六月月赛]Karp-de-Chant Number Description 卡常数被称为计算机算法竞赛之中最神奇的一类数字,主要特点集中于令人捉摸不透,有时候会让水平很 ...
随机推荐
- 【Python62--scrapy爬虫框架】
一.Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中 Scrapy最初是为页面抓取而设计的,也可以应用在 ...
- TCP 基础知识
参考 朱小厮-一文详解TCP 博客园-"三次握手,四次挥手"你真的懂吗? 博客园-深度解密HTTP通信细节
- FJUT3703 这还是一道数论题(二分 + hash + manacher 或者 STL + hash 或者 后缀数组 + hash)题解
Problem Description 最后来个字符串签个到吧,这题其实并不难,所需的算法比较基础,甚至你们最近还上过课. 为了降低难度,免得所有人爆零.这里给几个提示的关键字 :字符串,回文,二分, ...
- ckeditor5 安装高亮,颜色插件
ckeditor5 安装高亮插件 1.准备 git clone -b stable https://github.com/ckeditor/ckeditor5-build-classic.git cd ...
- 安装Go插件遇到的问题及解决方法
1. 问题:在 Windows 平台下使用 go get 安装sqlite3 驱动时报错 The remote end hung up unexpectedly ? 原因及解决方法: 原因可能有两种: ...
- CodeForces - 363D --二分和贪心
题目:CodeForces - 363D 题意:给定n个学生,其中每个学生都有各自的私己钱,并且自己的私己钱只能用在自己买自行车,不能给别人. 给定m个自行车,每个自行车都有一个价格. 给定公有财产a ...
- Vue-Router + Vuex 实现单页面应用
效果查看(一个食品安全网,大家也可以发布一些食品安全的见闻,尽举手之劳): 源代码:https://pan.baidu.com/s/1i43H3LV 如果想要服务器端代码可以在评论里说明一下 利用vu ...
- C# 创建对象的方法
1.实例化方法,也就是new(): //where T:new () 表示T必须有构造函数 public static T Create<T> where T:new () { retur ...
- 『Python』装饰器
一.参考 作者:zhijun liu 链接:https://www.zhihu.com/question/26930016/answer/99243411 来源:知乎 建议大家去原答案浏览 二.装饰器 ...
- MVC实战之排球计分(六)—— 使用EF框架,创建Controller,生成数据库。
在上篇博客我们写到,此软件的数据库连接我们使用的是EF框架,code first模式下, 通过模型类,在创建controller的时候直接生成数据库,完成数据库的连接,与操作. 在使用EF框架之前,我 ...