zoukankan      html  css  js  c++  java
  • 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
    ",ans);
    }
  • 相关阅读:
    FDR校正
    PHP base64 编码转化图片并进行指定路径的保存和上传处理(转自https://cloud.tencent.com/developer/article/1333877)
    google网页评测工具
    链接类型:预加载
    鼠标经过时显示、隐藏提示
    thinkphp5导入excel数据
    JS消息窗口滚动到底部
    datatable分页使用箭头
    Mysql基础
    php-MD5/sha1
  • 原文地址:https://www.cnblogs.com/mhy12345/p/4449335.html
Copyright © 2011-2022 走看看