hdu5303(2015多校2)--Delicious Apples(贪心+枚举)

Delicious Apples

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

Total Submission(s): 587    Accepted Submission(s): 188

Problem Description

There are n apple
trees planted along a cyclic road, which is L metres
long. Your storehouse is built at position 0 on
that cyclic road.

The ith
tree is planted at position xi,
clockwise from position 0.
There are ai delicious
apple(s) on the ith
tree.

You only have a basket which can contain at most K apple(s).
You are to start from your storehouse, pick all the apples and carry them back to your storehouse using your basket. What is your minimum distance travelled?

1≤n,k≤105,ai≥1,a1+a2+...+an≤105

1≤L≤109

0≤x[i]≤L

There are less than 20 huge testcases, and less than 500 small testcases.

Input

First line: t,
the number of testcases.

Then t testcases
follow. In each testcase:

First line contains three integers, L,n,K.

Next n lines,
each line contains xi,ai.

Output

Output total distance in a line for each testcase.

Sample Input

2
10 3 2
2 2
8 2
5 1
10 4 1
2 2
8 2
5 1
0 10000

Sample Output

18
26

题目大意:有一个圈,圈的长度是l,在正上方是0点,在圈上有n棵苹果树,给出每棵苹果树的位置和苹果的数量,现在一个人在0点的农场里,有一个小篮子,一次可以装k个苹果,问最少走多少距离可以把苹果收回农场。

赛中一看就是贪心的题目,然后就是各种不会啊,当时想了各种办法,又想了各种反例,,,,最终还是不会,,,

赛后补题,结论:表示一定要注意给出的范围的条件呀,尤其是比较特别的,一定有用。

对于摘苹果有几种情况:

1、正向去摘,然后按原路返回

2、反向去摘,然后按原路返回

3、还有就是直接走一圈

这三种方式,前面两个是比较容易解决的,直接去摘就好,特别的是去直接走一圈,如果两侧都有很多,那么直接走一圈一定是浪费的,那么会在什么情况下会变成节省路程的呢?

结果就是如果在圈上只剩下了k个苹果,可以一次摘走,如果这k个在一侧,或者在接近0点的两侧,那么走半圆是优的;如果在两侧并且比较靠下方,那么直接走一圈就是优的。而且走一圈只可能出现一次,否则就可以用半圈来代替了。

题目中给出了所有的苹果不会超过10^5个,让a[i]表示第i个苹果的位置,disr[i]表示正向摘完第i个需要的距离,disl[i]表示反向摘完第i个需要的距离,然后通过它们找出如果没走过整圈需要的最小值,和走一个整圈需要的最小值,其中小的那个是结果。

注意:如果k大于所有的苹果数,那么len的距离一定可以摘完,要特判一下最小值。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define LL __int64
struct node{
    int x , a ;
}p[100010];
int a[100010] , cnt ;
LL disl[100010] , disr[100010] ;
int cmp(node t1,node t2) {
    return t1.x < t2.x ;
}
int main() {
    int t , n , k ;
    int i , j ;
    LL len , ans ;
    scanf("%d", &t) ;
    while( t-- ) {
        scanf("%d %d %d", &len, &n, &k) ;
        for(i = 0 ; i < n ; i++) {
            scanf("%d %d", &p[i].x, &p[i].a) ;
        }
        sort(p,p+n,cmp) ;
        cnt = 1 ;
        for(i = 0 ; i < n ; i++) {
            for(j = 0 ; j < p[i].a ; j++)
                a[cnt++] = p[i].x ;
        }
        memset(disl,0,sizeof(disl)) ;
        memset(disr,0,sizeof(disr)) ;
        for(i = 1 ; i < cnt ; i++) {
            j = max(i-k,0) ;
            disr[i] = disr[j] + 2*a[i] ;
        }
        for(i = cnt-1 ; i > 0 ; i--) {
            j = min(i+k,cnt) ;
            disl[i] = disl[j] + 2*(len-a[i]) ;
        }
        ans = 0 ;
        for(i = 0 ; i < cnt ; i++) {
            if( ans == 0 ) ans = disr[i] + disl[i+1] ;
            else ans = min(ans,disr[i]+disl[i+1]) ;
        }
        for(i = 0 ; i+k+1 <= cnt ; i++)
            ans = min(ans,disr[i]+disl[i+k+1]+len) ;
        if( k >= cnt ) ans = min(ans,len) ;
        printf("%I64d\n", ans) ;
    }
    return 0 ;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 05:29:19

hdu5303(2015多校2)--Delicious Apples(贪心+枚举)的相关文章

HDU 5303 Delicious Apples (贪心 枚举 好题)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 199    Accepted Submission(s): 54 Problem Description There are n apple trees planted along a cyclic road, which is L metres l

Hdu5303 Delicious Apples 贪心

题目链接: HDU5303 题意: 有一条环形的长为L的路,仓库在位置0处, 这条路上有n棵苹果树,给出每棵苹果树的位置和苹果数量, 问用 一次最多能装K个苹果的篮子   把这条路上所有苹果采回仓库最少需要走的距离 解题思路: 这条路是环形的,先把果树分为两部分,圆的左半边算一部分,圆的右半边算另一部分 对所有苹果根据距离排序 , 用类似背包的思想,  统计左半边,右半边用 来回走(来回的长度一定小于一个圆环的周长)的方式采集完苹果所需要走的最少距离; 最后 考虑需要走一圈的情况:左边 多出k1

hdu5353(2015多校6)--Average(贪心)

题目链接:点击打开链接 题目大意:有n个人围城一个环,每一个人手里都有一些糖果,第i个人有ai块. 现在有三种操作: 第i个人给第i+1个人一块.如果i有 第i+1个人给第i个人一块.如果i+1有 什么都不做. 第i个人和第i+1个人之间,可以选择一种操作并执行,问最终能不能让所有人手里的糖相等. 当n = 1 时,永远是YES 当n = 2 时,注意1和2之间只能有一种操作,不存在循环. 当n > 2 时: 糖的总数不能均分到n个人手中,直接NO 可以分开,存在一个循环,那么枚举第1个人对第2

hdu 5358 First One 2015多校联合训练赛#6 枚举

First One Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 142    Accepted Submission(s): 37 Problem Description soda has an integer array a1,a2,-,an. Let S(i,j) be the sum of ai,ai+1,-,aj. No

HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 321    Accepted Submission(s): 95 Problem Description There are  apple trees planted along a cyclic road, which is  metres lon

[2015hdu多校联赛补题]hdu5303 Delicious Apples

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果树上的苹果数,现在你要采集所有的苹果(大概是你有一个容量为K的筐,你采集的苹果要装在筐里,采集完要回到坐标0的家里清空筐),问你最少需要走多少路 解:可以想象最多需要走一次整环,剩下的是走两个半环,那么分别对两个半环贪心(由于数据量较小,有很优雅的姿势~) 然后考虑走的这一次整环,由于这次整环的花费

2015 Multi-University Training Contest 2 hdu 5303 Delicious Apples

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1057    Accepted Submission(s): 354 Problem Description There are n apple trees planted along a cyclic road, which is L metres

Delicious Apples (hdu 5303 贪心+枚举)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 395    Accepted Submission(s): 122 Problem Description There are n apple trees planted along a cyclic road, which is L metres

hdu5289||2015多校联合第一场1002贪心+RMQ

http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to