[CF1076F] Summer Practice Report
Description
Solution
这一题可以考虑Dp,设\(Dp[i][j]\) 为在第i段中,以j颜色为结尾的最后一小段长度的最小值。
那么可以先考虑以表为结尾的情况:
表上一个线段的结尾,就把表看作分隔符,那么分隔符的数量下界是$lowerBound = \lceil \frac{(Dp[i - 1][j] + a[i])}{k} \rceil - 1 \(, 如果`b[i] > cnt`, 那么\)dp[i][j] = 1\(,上界\)upperBound$是
a[i] * k。表是上一个线段的结尾, 分隔符数量下界是\(lowerBound = \lceil \frac{a[i]}{k} \rceil - 1\), 上界 $upperBound $ 是 \(a[i] * k + (k - Dp[i - 1][j ~ xor ~ 1])\)
最后判断\(dp[n][0] \leq k || dp[n][1] \leq k\)
Summary
刚开始设状态\(dp[i][j][l]\) 表示dp到ith段,段的最后颜色为j,这样的颜色在这一段的最后一部分有l长是否有解。 其实这样是不对的,算方案数判断是否可行的套路只适用于一些容斥数学题(eg. Mobius), 所以就多上一维的冗余信息。
问题的模型是: 我们有n个段,段之间首尾相连,要求段中间的连续的隔板与球不超过k个。求是否有解。这样,我们不关心内部的排列方式, 并且内部排列不同不会影响下一段。这样的话我们可以直接考虑外部限制的情况下,钦定一种内部可行的方式。 而钦定就是对简单贪心的考察。
因为上一段的影响对下一段越小,对解的限制也就越小(越容易出解),所以可以直接贪心。
先考虑公式,将无关的公式看做挡板,表看作球。然后直接尽量容纳表即可。
Code
#include<bits/stdc++.h>
using std :: min;
#define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b) for(int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clar(a, b) memset((a), (b), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
typedef long long LL;
typedef long double LD;
int read() {
char ch = getchar();
int x = 0, flag = 1;
for (;!isdigit(ch); ch = getchar()) if (ch == '-') flag *= -1;
for (;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
return x * flag;
}
void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) write(x / 10);
putchar(x % 10 + 48);
}
const int Maxn = 3e5 + 9;
static int dp[Maxn][2], n;
static int x[Maxn], y[Maxn], k;
void init() {
n = read(), k = read();
rep (i, 1, n) x[i] = read();
rep (i, 1, n) y[i] = read();
}
int getCalc(int preVal, int preSeparator, int nowVal, int nowSeparator) {
int res = 0x3f3f3f3f;
if (preVal < 0x3f3f3f3f) {
LL lowerBound = ceil((1ll * preVal + nowVal) * 1. / k) - 1;
if (nowSeparator < lowerBound) res = min(res, 0x3f3f3f3f);
else if (nowSeparator > 1ll * nowVal * k) res = min(res, 0x3f3f3f3f);
else if (nowSeparator > lowerBound) res = min(res, 1);
else res = min(1ll * res, (1ll * preVal + nowVal) % k ? (1ll * preVal + nowVal) % k : k);
}
if (preSeparator < 0x3f3f3f3f) {
LL lowerBound = ceil(nowVal * 1. / k) - 1;
if (nowSeparator < lowerBound) res = min(res, 0x3f3f3f3f);
else if (nowSeparator > 1ll * (nowVal - 1) * k + (k - preSeparator)) res = min(res, 0x3f3f3f3f);
else if (nowSeparator > lowerBound) res = min(res, 1);
else res = min(1ll * res, (1ll * nowVal) % k ? (1ll * nowVal) % k : k);
}
return res;
}
void solve() {
dp[0][0] = 0; dp[0][1] = 0;
rep (i, 1, n) {
dp[i][0] = getCalc(dp[i - 1][0], dp[i - 1][1], x[i], y[i]);
dp[i][1] = getCalc(dp[i - 1][1], dp[i - 1][0], y[i], x[i]);
}
puts(dp[n][0] <= k || dp[n][1] <= k ? "YES" : "NO");
}
int main() {
init();
solve();
#ifdef Qrsikno
debug("\nRunning time: %.3lf(s)\n", clock() * 1.0 / CLOCKS_PER_SEC);
#endif
return 0;
}
[CF1076F] Summer Practice Report的更多相关文章
- Codeforces 1076F Summer Practice Report dp
Summer Practice Report dp[ i ][ 0 ]表示放完前 i 页, 第 i 页最后一段是 0, 0个数的最小值. dp[ i ][ 1 ]表示放完前 i 页, 第 i 页最后一 ...
- Codeforces1076F. Summer Practice Report(贪心+动态规划)
题目链接:传送门 题目: F. Summer Practice Report time limit per test seconds memory limit per test megabytes i ...
- Educational Codeforces Round 54 (Rated for Div. 2) Solution
A - Minimizing the String solved 题意:给出一个字符串,可以移掉最多一个字符,在所有可能性中选取一个字典序最小的. 思路:显然,一定可以移掉一个字符,如果移掉的字符的后 ...
- Codeforces Educational Codeforces Round 54 题解
题目链接:https://codeforc.es/contest/1076 A. Minimizing the String 题意:给出一个字符串,最多删掉一个字母,输出操作后字典序最小的字符串. 题 ...
- BA Practice Lead Handbook 1 - Why Is Business Analysis Taking The World By Storm?
The articles in this series are focused on individual Business Analysts and their managers. https:// ...
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- Atitit 数据存储视图的最佳实际best practice attilax总结
Atitit 数据存储视图的最佳实际best practice attilax总结 1.1. 视图优点:可读性的提升1 1.2. 结论 本着可读性优先于性能的原则,面向人类编程优先于面向机器编程,应 ...
- 2.ASP.NET MVC 中使用Crystal Report水晶报表
上一篇,介绍了怎么导出Excel文件,这篇文章介绍在ASP.NET MVC中使用水晶报表. 项目源码下载:https://github.com/caofangsheng93/CrystalReport ...
- Monthly Income Report – August 2016
原文链接:https://marcoschwartz.com/monthly-income-report-august-2016/ Every month, I publish a report of ...
随机推荐
- Access to Image at 'file:///Users canvas本地图片跨域报错解决方案
1.设置跨域 添加跨域条件 crossorigin="anonymous" 前提是后端支持这个图片跨域 2.上面加了之后还是报错 如标题所示 你需要把你的项目放到服务器上面跑 ...
- java的反射机制和javassist、asm
1.java的反射机制,可以帮助我们在运行的时候获取我们引用的java类相关的信息,包括类的名字.所包含的方法名字.方法参数等等 2.javassit这个jar包,大概看了下,更厉害,它可以直接操作字 ...
- linux设备驱动归纳总结
前言: (总结已经基本写完,这段时间我会从新排版和修正.错误总会有的,望能指正!) 前段时间学习了嵌入式驱动,趁着没开始找工作,这段时间我会每天抽出时间来复习. 我的总结是根据学习时的笔记(李杨老师授 ...
- [React] PureComponent in React
In this lesson, you will learn how to use PureComponent in React to reduce the number of times your ...
- soapUI系列之—-07 调用JIRA Rest API接口【例】
一.调用JIRA接口------实现过滤器搜索问题 1. 在SoapUI中新建 REST Project, 在URI 中输入登录接口的 url (任意一个 Rest 接口的 url 都可以): 2. ...
- mysql 5.5安装不对容易出现问题
按照正常步骤安装完了mysql 5.5之后,再运行一下bin目录中的mysqlinstanceconfig.exe,重置一下密码!!!! 重置密码的地方:modify security setting ...
- [JS进阶] HTML5 之文件操作(file)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/oscar999/article/details/37499743 前言 在 HTML 文档中 < ...
- (17)会话之Cookie的使用详解
Cooke技术 1,特点 Cookie技术:会话数据保存在浏览器客户端. 2,Cookie技术核心 Cookie类:用于存储会话数据 1)构造Cookie对象 Cookie(java.lang.St ...
- Lightoj 1023 - Discovering Permutations
1023 - Discovering Permutations PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory L ...
- YTU 2596: 编程题B-日期格式
2596: 编程题B-日期格式 时间限制: 1 Sec 内存限制: 128 MB 提交: 981 解决: 74 题目描述 注:本题只需要提交编写的函数部分的代码即可. 将输入的日期格式 月/日/年 ...