zoukankan      html  css  js  c++  java
  • HDU 3045 Picnic Cows (斜率优化DP)

    Picnic Cows

    Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1088    Accepted Submission(s): 329


    Problem Description
    It’s summer vocation now. After tedious milking, cows are tired and wish to take a holiday. So Farmer Carolina considers having a picnic beside the river. But there is a problem, not all the cows consider it’s a good idea! Some cows like to swim in West Lake, some prefer to have a dinner in Shangri-la ,and others want to do something different. But in order to manage expediently, Carolina coerces all cows to have a picnic!
    Farmer Carolina takes her N (1<N≤400000) cows to the destination, but she finds every cow’s degree of interest in this activity is so different that they all loss their interests. So she has to group them to different teams to make sure that every cow can go to a satisfied team. Considering about the security, she demands that there must be no less than T(1<T≤N)cows in every team. As every cow has its own interest degree of this picnic, we measure this interest degree’s unit as “Moo~”. Cows in the same team should reduce their Moo~ to the one who has the lowest Moo~ in this team——It’s not a democratical action! So Carolina wishes to minimize the TOTAL reduced Moo~s and groups N cows into several teams.
    For example, Carolina has 7 cows to picnic and their Moo~ are ‘8 5 6 2 1 7 6’ and at least 3 cows in every team. So the best solution is that cow No.2,4,5 in a team (reduce (2-1)+(5-1) Moo~)and cow No.1,3,6,7 in a team (reduce ((7-6)+(8-6)) Moo~),the answer is 8.
     
    Input
    The input contains multiple cases.
    For each test case, the first line has two integer N, T indicates the number of cows and amount of Safe-base line.
    Following n numbers, describe the Moo~ of N cows , 1st is cow 1 , 2nd is cow 2, and so on.
     
    Output
    One line for each test case, containing one integer means the minimum of the TOTAL reduced Moo~s to group N cows to several teams.
     
    Sample Input
    7 3 8 5 6 2 1 7 6
     
    Sample Output
    8
     
    Source
     
    Recommend
    gaojie
     
     
     

    本题也是斜率优化DP。

    首先 sort 从小到大排序。

    然后 dp[i] 前 i 个数分隔得到的最小值。

    则  dp[i]= min{dp[j]+(sum[i]-sum[j])-a[j+1](i-j)}    k<=j<i-t+1.

    之后就是斜率优化了。

    /*
    HDU 3045 Picnic Cows
    斜率DP
    
    */
    
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int MAXN=400010;
    
    int q[MAXN];
    long long  sum[MAXN];
    long long a[MAXN];
    long long dp[MAXN];
    
    long long getDP(int i,int j)
    {
        return dp[j]+(sum[i]-sum[j])-a[j+1]*(i-j);
    }
    long long getUP(int j,int k)//得到 yj-yk  k<j
    {
        return dp[j]-sum[j]+j*a[j+1]-(dp[k]-sum[k]+k*a[k+1]);
    }
    long long getDOWN(int j,int k)//得到 xj-xk  k<j
    {
        return a[j+1]-a[k+1];
    }
    
    int main()
    {
       // freopen("in.txt","r",stdin);
      //  freopen("out.txt","w",stdout);
        int n,t;
        int head,tail;
        while(scanf("%d%d",&n,&t)==2)
        {
            dp[0]=sum[0]=0;
            a[0]=0;
            for(int i=1;i<=n;i++)
            {
                scanf("%I64d",&a[i]);
            }
            sort(a+1,a+n+1);
            for(int i=1;i<=n;i++)
              sum[i]=sum[i-1]+a[i];
            head=tail=0;
            q[tail++]=0;
            for(int i=1;i<=n;i++)
            {
                while(head+1<tail && getUP(q[head+1],q[head])<=i*getDOWN(q[head+1],q[head]))
                  head++;
                dp[i]=getDP(i,q[head]);
                int j=i-t+1;
                if(j<t)continue;//注意这个条件
                while(head+1<tail && getUP(j,q[tail-1])*getDOWN(q[tail-1],q[tail-2])<=getUP(q[tail-1],q[tail-2])*getDOWN(j,q[tail-1]))
                       tail--;
                q[tail++]=j;
            }
            printf("%I64d\n",dp[n]);
        }
        return 0;
    }
  • 相关阅读:
    Flask_之使用过程中出现的异常
    GO-学习之golang 自动下载所有依赖包
    数据库-Oracle如何将clob数据以字符串打印出来
    数据库-Oracle条件判断语句
    python框架Django汇总
    每日一模块_os及subprocess模块【执行系统命令】
    每日一模块-sftp的使用
    python并发编程-队列MQ的学习
    python高效配置-响应code枚举类设计
    git-使用大全
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2657735.html
Copyright © 2011-2022 走看看