HDU 5303 Delicious Apples (貪心 列舉 好題)

_TCgogogo_發表於2015-07-23


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 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?

1n,k105,ai1,a1+a2+...+an105
1L109
0x[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
 

Source
2015 Multi-University Training Contest 2

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=5303

題目大意:有一個長為L的環,n個蘋果樹,一個籃子最多裝k個蘋果,裝完要回到起點卸下再出發,給出n個蘋果樹順時針的位置及蘋果的個數,求摘完所有蘋果走的最小路程

題目分析:顯然,只有在某種特殊條件下,即兩側都還有蘋果且可以一次裝完且最後的蘋果都離起點比較遠,這種情況下,我們直接繞圈可能會更優,也就是說整圈最多繞一次,因此我們可以先對兩邊貪心,題目的資料顯示蘋果的數量最多就1e5,顯然我們可以把蘋果“離散”出來,用x[i]記錄第i個蘋果到起點的位置,然後對位置從小到大排序,先選擇路程小的,選擇的時候用dis[i]記錄單側裝了i個蘋果的最小路程,類似揹包計數的原理,答案要乘2,因為是來回的,最後在k>=i時,列舉繞整圈的情況,szl-i表示只走左邊採的蘋果數,szr - (k - i)表示只走右邊採的蘋果樹,畫個圖就能看出來了,注意右邊這裡可能值為負,要和0取最大,然後答案就是(disl[szl-i] + disr[szr - (k - i)])* 2 +L,這裡其實畫圖更加直觀。最後取最小即可,注意有幾個wa點,一個是要用long long,二是之前說的出現負數和0取大,三是每次要清零


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1e5 + 5;
int  L, n, k;
ll x[MAX], disl[MAX], disr[MAX];
vector <ll> l, r;

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        memset(disl, 0, sizeof(disl));
        memset(disr, 0, sizeof(disr));
        l.clear();
        r.clear();
        scanf("%d %d %d", &L, &n, &k);
        int cnt = 1;
        for(int i = 1; i <= n; i++) 
        {
            ll pos, num;
            scanf("%lld %lld", &pos, &num);
            for(int j = 1; j <= num; j++)
                x[cnt ++] = (ll) pos;   //離散操作
        }
        cnt --;
        for(int i = 1; i <= cnt; i++)
        {
            if(2 * x[i] < L)
                l.push_back(x[i]);
            else
                r.push_back(L - x[i]);  //記錄位置
        }   
        sort(l.begin(), l.end());
        sort(r.begin(), r.end());
        int szl = l.size(), szr = r.size();
        for(int i = 0; i < szl; i++)
            disl[i + 1] = (i + 1 <= k ? l[i] : disl[i + 1 - k] + l[i]);
        for(int i = 0; i < szr; i++)
            disr[i + 1] = (i + 1 <= k ? r[i] : disr[i + 1 - k] + r[i]);
        ll ans = (disl[szl] + disr[szr]) * 2;
        for(int i = 0; i <= szl && i <= k; i++)
        {
            int p1 = szl - i;
            int p2 = max(0, szr - (k - i));
            ans = min(ans, 2 * (disl[p1] + disr[p2]) + L);
        }
        printf("%I64d\n", ans);
    }
}



相關文章