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 卡常数被称为计算机算法竞赛之中最神奇的一类数字,主要特点集中于令人捉摸不透,有时候会让水平很 ...
随机推荐
- 复旦高等代数 II(17级)每周一题
本学期将继续进行高等代数每周一题的活动.计划从第一教学周开始,到第十六教学周为止(根据法定节假日安排,中间个别周会适当地停止),每周的周末将公布1道思考题(共16道),供大家思考和解答.每周一题通过“ ...
- 腾讯云centos7.2安装jdk1.7 tomcat7.0部署项目示例
说实话win server的性能并不好,所以程序员必须会在Linux上安装环境,部署项目. 第一步,官网下载tomcat和jdk压缩文件*.tar.gz 下载路径如下: jdk:http://www ...
- Unicode编码问题 如:\u529e\u7406\u9996\u6c7d\u52a0\u6cb9
python 遇到\u529e\u7406\u9996\u6c7d\u52a0\u6cb9 解决方法: a = "\u529e\u7406\u9996\u6c7d\u52a0" b ...
- TabBar + TabBarView导航风格
import 'package:flutter/material.dart'; import 'News.dart'; import 'Video.dart'; import 'Chat.dart'; ...
- IDEA中静态资源无法找到的原因
IDEA中静态资源无法找到, 原因1:同名的文件但是在不同的包里. 原因2:IDEA重启,web清空缓存. 原因3:错误的文件及路径. 原因4:其他原因排除后,可使用绝招重启试试.
- mongodb 设置权限
切换到要加密的数据库use diary 创建有 readWrite 权限的用户db.createUser({ user: "youuser", pwd: "youpass ...
- Linux 软连接 (ln命令)
这是linux中一个非常重要命令.它的功能是为某一个文件在另外一个位置建立一个同不的链接,这个命令最常用的参数是-s,具体用法是:ln -s 源文件 目标文件. 当我们需要在不同的目录,用到相同的文件 ...
- yii中的restful方式输出并调用接口和判断用户是否登录状态
//创建一个控制器接口 返回的是restful方式 <?php namespace frontend\controllers; use frontend\models\Fenlei; use f ...
- CI集成 mesos 资源分配的思考, 待续
读了mesos的论文(https://people.eecs.berkeley.edu/~alig/papers/mesos.pdf ),感觉应用在 CI 上的资源管理很赞,能够解决 jenkins在 ...
- QLineSeries QChartView 生成折线
效果图 // 创建折线上点的序列 QLineSeries *splineSeries = new QLineSeries(); //QSplineSeries *splineSeries = new ...