题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1044

咳咳...终于A了...

居然没注意到正着找pos是n方会TLE...所以要倒着找pos;

二分还写错了,改了半天...

小心前缀和取模后相减变成负数!!!!!!!!!

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const maxn=,mod=;
int n,m,a[maxn],s[maxn][],f[maxn][],ans,mn,sum,l,r,pos[maxn];
bool pd(int x)
{
int s=,cnt=;
for(int i=;i<=n;i++)
{
if(s+a[i]>x)
{
cnt++;
s=;
}
s+=a[i];
}
return cnt<=m;//m!!
return ;
}
void solve1()
{
r=s[n][];
int mid=(l+r)>>;
while (l<=r)
{
if (pd(mid)) mn=mid,r=mid-;
else l=mid+;
mid=(l+r)>>;
}
}
void solve2()
{
for(int i=;i<=n;i++)
{
if(s[i][]<=mn)f[i][]=;
else break;
}
for (int i=;i<=n;i++)
{
if (s[i][]<=mn) continue;
for (int j=i-;j>=;j--)
if (s[i][]-s[j][]>mn) {pos[i]=j+;break;}
// for(int j=0;j<i;j++)
// if(s[i][0]-s[j][0]<=mn){pos[i]=j;break;}//TLE!!!(n方)
}
bool x=;
while(m--)
{
for(int i=;i<=n;i++)
s[i][x]=(s[i-][x]+f[i][x])%mod;//
x=!x;
for(int i=;i<=n;i++)
f[i][x]=(s[i-][!x]-s[max(pos[i]-,)][!x]+mod)%mod;//i-1! //pos[i]-1! //mod后小心负数!!!!!
(ans+=f[n][x])%=mod;
}
printf("%d %d",mn,ans);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]),s[i][]=s[i-][]+a[i],l=max(l,a[i]);
solve1();
solve2();
return ;
}

bzoj1044 [HAOI2008]木棍分割——前缀和优化DP的更多相关文章

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

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

  2. bzoj 1044 [HAOI2008]木棍分割——前缀和优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1044 前缀和优化. 但开成long long会T.(仔细一看不用开long long) #i ...

  3. BZOJ_1044_[HAOI2008]木棍分割_二分答案+DP+单调队列

    BZOJ_1044_[HAOI2008]木棍分割_二分答案+DP Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个 ...

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

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

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

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

  6. BZOJ1044: [HAOI2008]木棍分割

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

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

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

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

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

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

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

随机推荐

  1. winrar5.0破解

    RAR registration data Federal Agency for Education 1000000 PC usage license UID=b621cca9a84bc5deffbf ...

  2. linux下reboot和shutdown关机命令详解

    我 们在操作Linux v/服务器的时候肯定会有需要重启系统,或者关闭系统等操作.有些用户是直接到VPS主机商家面板上操作的,这样一来比较麻烦,二来有些面板还不易于使用 容易导致面板卡死.所以最好的方 ...

  3. Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]

    传送门 D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 me ...

  4. Method and system for early speculative store-load bypass

    In an embodiment, the present invention describes a method and apparatus for detecting RAW condition ...

  5. linux 报错:E: Package 'libmemcached' has no installation candidate

    linux 报错:E: Package 'libmemcached' has no installation candidate 网上查资料说是软件安装源没有这个软件,需要添加软件源. 1.备份源列表 ...

  6. windows7 下安装使用Redis

    Redis 安装使用 本地环境:Windows7 64位web环境:wamp集成环境,php版本:PHP Version 7.1.17 学习参考网站: RUNOOB.COM官网  http://www ...

  7. Delphi:解决重绘造成的窗体闪烁问题

    解决窗体闪烁问题 具体代码: 1.在声明窗体类时加入:   private     procedure CreateParams(var Params: TCreateParams); overrid ...

  8. 洛谷—— P1536 村村通

    P1536 村村通 题目描述 某市调查城镇交通状况,得到现有城镇道路统计表.表中列出了每条道路直接连通的城镇.市政府“村村通工程”的目标是使全市任何两个城镇间都可以实现交通(但不一定有直接的道路相连, ...

  9. Codeforces 938G(cdq分治+可撤销并查集+线性基)

    题意: 有一个无向连通图,支持三个操作: 1 x y d : 新建一条x和y的无向边,长度为d 2 x y    :删除x和y之间的无向边 3 x y    :询问x到y的所有路径中(可以绕环)最短的 ...

  10. 2017CodeM初赛A场

    A.最长树链(loj6159) 分析: 对于每个质因数,取出所有是它倍数的点组成一个树,然后找最长路径 每个数操作次数是其质因数的个数 所以总的复杂度不超过O(nlogA) B.二分图染色(loj61 ...