【BZOJ1044】[HAOI2008]木棍分割

题面

bzoj

洛谷

题解

第一问显然可以二分出来的。

第二问:

设\(dp[i][j]\)表示前\(i\)个,切了\(j\)组的方案数

发现每次转移都是从前面一个区间过来的

直接前缀和优化就好了

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int Mod = 10007;
void pls(int &x, int y) { x += y; if (x >= Mod) x -= Mod; }
void dec(int &x, int y) { x -= y; if (x < 0) x += Mod; }
const int MAX_N = 50005;
int N, M, ans1, ans2, len[MAX_N], L[MAX_N], prv[MAX_N];
bool check(int v) {
int cnt = 0, s = 0;
for (int i = 1; i <= N; i++) {
if (s + len[i] > v) ++cnt, s = len[i];
else s += len[i];
if (cnt > M) return 0;
}
return 1;
}
int solve() {
int l = *max_element(&len[1], &len[N + 1]), r = L[N], res = L[N];
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) r = mid - 1, res = mid;
else l = mid + 1;
}
return res;
}
int sum1[MAX_N], sum2[MAX_N];
int main () {
N = gi(), M = gi();
for (int i = 1; i <= N; i++) L[i] = L[i - 1] + (len[i] = gi());
ans1 = solve();
for (int i = 1; i <= N; i++) {
int l = 1, r = i; prv[i] = i;
while (l <= r) {
int mid = (l + r) >> 1;
if (L[i] - L[mid - 2] <= ans1) prv[i] = mid, r = mid - 1;
else l = mid + 1;
}
}
for (int i = 1, j = 1; i <= N; i++) {
while (L[i] - L[j - 1] > ans1 && j < i) ++j;
if (L[i] - L[j - 1] <= ans1) prv[i] = max(j - 2, 0);
}
for (int i = 1; i <= N; i++) sum1[i] = sum1[i - 1], pls(sum1[i], (L[i] <= ans1));
for (int i = 1; i <= M; i++) {
for (int j = 1; j <= N; j++) sum2[j] = sum1[j - 1], dec(sum2[j], sum1[prv[j]]);
for (int j = 1; j <= N; j++) sum1[j] = sum1[j - 1], pls(sum1[j], sum2[j]);
pls(ans2, sum2[N]);
}
printf("%d %d\n", ans1, ans2);
return 0;
}

【BZOJ1044】[HAOI2008]木棍分割的更多相关文章

  1. BZOJ1044: [HAOI2008]木棍分割

    1044: [HAOI2008]木棍分割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1580  Solved: 567[Submit][Statu ...

  2. bzoj1044[HAOI2008]木棍分割 单调队列优化dp

    1044: [HAOI2008]木棍分割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4314  Solved: 1664[Submit][Stat ...

  3. BZOJ1044 [HAOI2008]木棍分割 【二分+Dp】

    1044: [HAOI2008]木棍分割 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4281  Solved: 1644 [Submit][St ...

  4. [BZOJ1044][HAOI2008]木棍分割 二分+贪心+dp+前缀和优化

    1044: [HAOI2008]木棍分割 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4112  Solved: 1577 [Submit][St ...

  5. 【czy系列赛】czy的后宫6 && bzoj1044 [HAOI2008]木棍分割

    题目描述 众所周知的是丧尸czy有很多妹子(虽然很多但是质量不容乐观QAQ),今天czy把n个妹子排成一行来检阅.但是czy的妹子的质量实在--所以czy看不下去了.检阅了第i个妹子会增加czy a[ ...

  6. [bzoj1044][HAOI2008][木棍分割] (二分+贪心+dp+队列优化)

    Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长 ...

  7. [BZOJ1044][HAOI2008]木棍分割 二分 + 单调队列优化dp + 滚动数组优化dp

    Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长 ...

  8. bzoj1044: [HAOI2008]木棍分割 二分+dp

    有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长度最小, 并且输出有多少 ...

  9. 【动态规划】bzoj1044: [HAOI2008]木棍分割

    需要滚动优化或者short int卡空间 Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍 ...

  10. BZOJ1044: [HAOI2008]木棍分割(dp 单调队列)

    题意 题目链接 Sol 比较套路的一个题. 第一问二分答案check一下 第二问设\(f[i][j]\)表示前\(i\)个数,切了\(j\)段的方案数,单调队列优化一下. 转移的时候只需要保证当前段的 ...

随机推荐

  1. 使用eclipse遇到的unable to install breakpoint的问题

    调试一个tomcat工程,设置好断点,启动工程,结果出现了下面的错误: 继续运行,再进入断点之前,还会再度提示,但是最终会命中断点. 使用CGLIB查找关键字,了解到CGLIB是一个AOP的拦截库,想 ...

  2. 奇怪的等待事件“enq: ss - contention”

    数据库有时会遇到大量的进程发生'enq: ss - contention'等待,持续5到10分钟,然后自动消失.从字面上看,'SS'是Sort Segment: select * from v$loc ...

  3. [翻译] CSStickyHeaderFlowLayout

    CSStickyHeaderFlowLayout https://github.com/jamztang/CSStickyHeaderFlowLayout Parallax, Sticky Heade ...

  4. GONMarkupParser的使用

    GONMarkupParser的使用 说明 这是一个写得非常好的富文本工具类,便于你进行简易的封装.本人抛砖引玉,只进行了少量的简化使用封装. 效果 源码 https://github.com/nic ...

  5. Stored Properties 与 Computed Properties

    Stored Properties 与 Computed Properties About Swift Stored Properties In its simplest form, a stored ...

  6. [C++] 用Xcode来写C++程序[1] 新建C++项目工程

    用Xcode来写C++程序[1] 新建C++项目工程 第一节从新建工程并编译C++源码开始 新建工程 源码: // // main.cpp // YeHelloWorld // // Created ...

  7. ubuntu 14.04 安装 openvswitch

    安装 openvswitch (这里以openvswitch lib 分支为例) 如果没有安装git,如果有请跳过 $ sudo apt-get install git install ovs $ g ...

  8. vs环境变量学习

    1. 查看vs环境变量: 在项目设置中的任何路径.目录编辑项目下,右下角有个“宏”,点开即可见所有vs环境变量的当前设置...听说还有其它地方,没看到. 2.上边的“宏”,即是英文的vs环境变量 3. ...

  9. [MongoDB]------windos下的安装部署与基础使用

    1.安装 首先前往官网进行下载,这里贴个地址https://www.mongodb.com/download-center#community 点击大大的原谅色的DOWNLOAD(msi)按钮进行下载 ...

  10. Ceph块存储介绍

    1. 块存储是什么 块存储简称RBD(RADOS Block Device),是一种有序的字节序块,也是在Ceph三大存储类型中最为常用的存储方式 ,Ceph的块存储是基于RADOS的,因此它也借助R ...