• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
mhy12345
博客园    首页    新随笔    联系   管理    订阅  订阅

bzoj 1761: [Baltic2009]beetle 区间dp

1761: [Baltic2009]beetle

Time Limit: 4 Sec  Memory Limit: 64 MB
Submit: 255  Solved: 92
[Submit][Status][Discuss]

Description

在一条直线上有N个点,每个点M升水. 一个虫子在坐标轴0点上,它每个单位时间移动一格,每个点的水每单位时间消失1升. 问虫子最多可以喝到多少水,喝水的时间忽略不计

Input

第一行给出数字N,M 下面N行给出N个点的坐标Xi 0 ≤ n ≤ 300, 1 ≤ m ≤ 1,000,000, −10,000 ≤ x1, x2, . . . , xn ≤ 10,000,

Output

最多可以喝到多少水

Sample Input

3 15
6
-3
1

Sample Output


25

HINT


虫子开始在0点,它先到1这个点喝水,再到-3,再到6.

  

  走一步产生的损失等于剩下没有喝的位置总数,则另f[x][y][p]表示左端点x,右端点y,当前在x/y位置,最小损失为多少,转移的时候为了处理水量不能减成负数的问题,需要在枚举最终要喝多少个位置。

  4s O(n^3)有一点常数问题。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 310
#define INFL 0x3f3f3f3f3f3f3f3fLL
typedef long long qword;
int a[MAXN];
qword dp[MAXN][MAXN][2];
qword dis[MAXN][MAXN];
inline void deal(qword &x,qword y)
{
        x=min(x,y);
}

int main()
{
        freopen("input.txt","r",stdin);
        int n,m;
        scanf("%d%d",&n,&m);
        for (int i=0;i<n;i++)
                scanf("%d",a+i);
        a[n++]=0;
        qword ans=-INFL;
        sort(a,a+n);
        for (int i=0;i<n;i++)
                for (int j=0;j<n;j++)
                        dis[i][j]=abs(a[i]-a[j]);
        int x=lower_bound(a,a+n,0)-a;
        for (register int tot=1;tot<=n;tot++)
        {
                memset(dp,0x3f,sizeof(dp));
                dp[x][x][0]=0;
                for (register int i=1;i<=tot;i++)
                {
                        for (register int j=0;j+i-1<n;j++)
                        {
                                if (dp[j][j+i-1][0]<INFL)
                                {
                                        if (j)
                                                deal(dp[j-1][j-1+(i+1)-1][0],dp[j][j+i-1][0]+dis[j][j-1]*(tot-i));
                                        if (j+i-1<n-1)
                                                deal(dp[j][j+(i+1)-1][1],dp[j][j+i-1][0]+dis[j][j+(i+1)-1]*(tot-i));
                                }
                                if (dp[j][j+i-1][1]<INFL)
                                {
                                        if (j)
                                                deal(dp[j-1][j-1+(i+1)-1][0],dp[j][j+i-1][1]+dis[j+i-1][j-1]*(tot-i));
                                        if (j+i-1<n-1)
                                                deal(dp[j][j+(i+1)-1][1],dp[j][j+i-1][1]+dis[j+i-1][j+(i+1)-1]*(tot-i));
                                }
                        }
                }
                for (int j=0;j+tot-1<n;j++)
                {
                        ans=max(ans,(qword)m*(tot-1)-min(dp[j][j+tot-1][0],dp[j][j+tot-1][1]));
                }
        }
        printf("%lld\n",ans);
}

 

by mhy12345(http://www.cnblogs.com/mhy12345/) 未经允许请勿转载

本博客已停用,新博客地址:http://mhy12345.xyz

posted @ 2015-04-23 08:52  mhy12345  阅读(218)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3