Description

Transmission Gate

Solution

这一题可以考虑Dp,设\(Dp[i][j]\) 为在第i段中,以j颜色为结尾的最后一小段长度的最小值。

那么可以先考虑以表为结尾的情况:

  1. 表上一个线段的结尾,就把表看作分隔符,那么分隔符的数量下界是$lowerBound = \lceil \frac{(Dp[i - 1][j] + a[i])}{k} \rceil - 1 \(, 如果`b[i] > cnt`, 那么\)dp[i][j] = 1\(,上界\)upperBound$是a[i] * k

  2. 表是上一个线段的结尾, 分隔符数量下界是\(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的更多相关文章

  1. Codeforces 1076F Summer Practice Report dp

    Summer Practice Report dp[ i ][ 0 ]表示放完前 i 页, 第 i 页最后一段是 0, 0个数的最小值. dp[ i ][ 1 ]表示放完前 i 页, 第 i 页最后一 ...

  2. Codeforces1076F. Summer Practice Report(贪心+动态规划)

    题目链接:传送门 题目: F. Summer Practice Report time limit per test seconds memory limit per test megabytes i ...

  3. Educational Codeforces Round 54 (Rated for Div. 2) Solution

    A - Minimizing the String solved 题意:给出一个字符串,可以移掉最多一个字符,在所有可能性中选取一个字典序最小的. 思路:显然,一定可以移掉一个字符,如果移掉的字符的后 ...

  4. Codeforces Educational Codeforces Round 54 题解

    题目链接:https://codeforc.es/contest/1076 A. Minimizing the String 题意:给出一个字符串,最多删掉一个字母,输出操作后字典序最小的字符串. 题 ...

  5. 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:// ...

  6. 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 ...

  7. Atitit 数据存储视图的最佳实际best practice attilax总结

    Atitit 数据存储视图的最佳实际best practice attilax总结 1.1. 视图优点:可读性的提升1 1.2. 结论  本着可读性优先于性能的原则,面向人类编程优先于面向机器编程,应 ...

  8. 2.ASP.NET MVC 中使用Crystal Report水晶报表

    上一篇,介绍了怎么导出Excel文件,这篇文章介绍在ASP.NET MVC中使用水晶报表. 项目源码下载:https://github.com/caofangsheng93/CrystalReport ...

  9. Monthly Income Report – August 2016

    原文链接:https://marcoschwartz.com/monthly-income-report-august-2016/ Every month, I publish a report of ...

随机推荐

  1. Java 等额本金等额本息工具类

    原文:http://www.open-open.com/code/view/1449034309983 等额本息: /** * Description:等额本息工具类 * Copyright: Cop ...

  2. 多硬盘分区管理fdisk

    原文:http://blog.fens.me/linux-fdisk/ ---------------------------------------------------------------- ...

  3. webpack-Dependency Graph(依赖图)

    依赖图(Dependency Graph) 任何时候,一个文件依赖于另一个文件,webpack 就把此视为文件之间有依赖关系. 这使得 webpack 可以接收非代码资源(non-code asset ...

  4. :>/dev/null 2>&1 的作用

    shell中可能经常能看到:>/dev/null 2>&1 命令的结果可以通过%>的形式来定义输出 /dev/null 代表空设备文件 > 代表重定向到哪里,例如:ec ...

  5. hdu 5303 Delicious Apples

    这道题贪心 背包 假设在走半圆之内能够装满,那么一定优于绕一圈回到起点.所以我们从中点将这个分开,那么对于每一个区间由于苹果数非常少,所以能够利用pos[x]数组记录每一个苹果所在的苹果树位置,然后将 ...

  6. java 生成压测数据

    询价接口压测,需要批量生成数据, 数据包括4个字段(车牌号,车架号,发动机号,支付号)licenseNo,vehicleFrameNo,engineNo,payFlowId 需符合LoadRunner ...

  7. centos6.5 yum安装MySQL5.6

    创建MySQL用户 #useradd mysql #passwd mysql #chmod u+w /etc/sudoers #vi /etc/sudoers mysql ALL=(ALL) ALL ...

  8. Semantic Parsing(语义分析) Knowledge base(知识图谱) 对用户的问题进行语义理解 信息检索方法

    简单说一下所谓Knowledge base(知识图谱)有两条路走,一条是对用户的问题进行语义理解,一般用Semantic Parsing(语义分析),语义分析有很多种,比如有用CCG.DCS,也有用机 ...

  9. Datatables 1.10.x在命名上与1.9.x

    1.10.x与1.9.x参数名对照表 Datatables 1.10.x在命名上与1.9.x的有区别,新版的使用的是驼峰的命名规则,而之前的是采用匈牙利命名规则 当然,这些变化都是向下兼容的,你可以继 ...

  10. Struts2中ValueStack结构和总结

    [ValueStack和ActionContext的关系] 首先,从结构上来看ValueStack是ActionContext的一个组成部分,是对ActionContext功能的扩展.ActionCo ...