Picnic Cows

Picnic Cows

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


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
 
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 10010
#define MOD 1000000007
#define LL long long
const int mod = 998244353;

int T;
int n,m;

long long a[410000];
long long sum[410000];
long long dp[410000];
int que[410000];

void solve()
{
    int fr=0,re=0;

    dp[m] = sum[m] - m*a[1];

    que[re++] = 0;

    for(int i=1; i<=n; i++)
    {
        while(fr+1<re)
        {
            int k = que[fr];
            int t = que[fr+1];

            long long yy = dp[t] - sum[t] + t*a[t+1]  - (dp[k] - sum[k] + k*a[k+1]);
            long long  xx = a[t+1] - a[k+1];

            if(i*xx>=yy) fr++;
            else break;
        }

        int k = que[fr];
        dp[i] = dp[k] + sum[i] - sum[k] - (i-k)*a[k+1];

        int j=i-m+1;
        if(j<m) continue;            ///限制长度,添加到末尾。

        while(fr+1<re)
        {
            int k = que[re-2];
            int t = que[re-1];

            long long  yy = dp[t] - sum[t] + t*a[t+1]  - (dp[k] - sum[k] + k*a[k+1]);
            long long xx = a[t+1] - a[k+1];

            k=t;
            t=j;

            long long yz = dp[t] - sum[t] + t*a[t+1]  - (dp[k] - sum[k] + k*a[k+1]);
            long long xz = a[t+1] - a[k+1];

            if(yy*xz>=yz*xx) re--;
            else break;
        }
        que[re++] = j;
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        sum[0]=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%I64d",&a[i]);
        }

        sort(a+1,a+1+n);
        for(int i=1; i<=n; i++)
            sum[i]=sum[i-1]+a[i];

        solve();

        printf("%I64d\n",dp[n]);
    }
    return 0;
}


























   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值