zoukankan      html  css  js  c++  java
  • hdu3045 Picnic Cows

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


    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

    题意:给你一些牛,把它们分成若干组,每一头牛有自己的价值,每一组的牛的个数不少于T,每一组贡献的价值为这一组内的牛与最小价值牛的差的和,问所有组贡献的价值最小是多少。

    思路:这题dp方程容易想出dp[i]=dp[k]+sum[i]-sum[k]-(i-k)*a[k+1];(k>=m && i-(k+1)+1>=m),但是在处理的时候有一定的技巧,wa了很多次,注意点详见代码。

    <pre name="code" class="cpp">#include<iostream>
    #include<math.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define ll long long
    #define maxn 800050
    ll dp[maxn],a[maxn],sum[maxn];
    ll q[1111111];
    ll up(int k)
    {
        return dp[k]-sum[k]+k*a[k+1];
    }
    ll down(int k)
    {
        return a[k+1];
    }
    
    
    int main()
    {
        int n,m,i,j;
        int front,rear;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            sum[0]=0;
            for(i=1;i<=n;i++){
                scanf("%lld",&a[i]);
            }
            sort(a+1,a+1+n);
            for(i=1;i<=n;i++){
                sum[i]=sum[i-1]+a[i];
            }
            for(i=m;i<=min(2*m-1,n);i++){
                dp[i]=sum[i]-i*a[1];
            }
            front=1;rear=1;
            q[front]=0; //这里相当于是前i个都放在一组的情况
            for(i=2*m-1;i<=n;i++){
                while(front<rear && up(q[front+1])-up(q[front])<=i*(down(q[front+1])-down(q[front]))   ){
                    front++;
                }
                int k=q[front];
                dp[i]=dp[k]+sum[i]-sum[k]-(i-k)*a[k+1];
                /*
                while(front<rear && (up(q[rear])-up(q[rear-1]))*(down(i)-down(q[rear])  )>=(up(i)-up(q[rear]))*(down(q[rear])-down(q[rear-1])  )        ){
                    rear--;
                }
                注意:这里不要把i和q[rear],q[rear-1]放进队列,而是要把i-m+1放进队列!
                */
                while(front<rear && (up(q[rear])-up(q[rear-1]))*(down(i-m+1)-down(q[rear])  )>=(up(i-m+1)-up(q[rear]))*(down(q[rear])-down(q[rear-1])  )        ){
                    rear--;
                }
                rear++;
                q[rear]=i-m+1;
            }
            printf("%lld
    ",dp[n]);
        }
        return 0;
    }
    



    
    
  • 相关阅读:
    linq 查询 过滤数据中某个值是否在数组中
    Nginx 摘要
    Unity调用外部摄像头,全屏显示摄像头画面
    Vuforia切换回识别场景后黑屏解决
    Unity3d通过脚本生成apk
    EF6 CodeFirst连接MySql 报nvarchar('max')错误解决办法
    UNITY_MATRIX_MVP和UnityObjectToClipPos
    Unity的stateMachineBehaviour
    U3D加载服务器上的assetbundle
    jquery解析xml,获取xml标签名
  • 原文地址:https://www.cnblogs.com/herumw/p/9464652.html
Copyright © 2011-2022 走看看