HDU 5303 Delicious Apples (貪心 枚舉 好題),hdu5303

來源:互聯網
上載者:User

HDU 5303 Delicious Apples (貪心 枚舉 好題),hdu5303


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

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.
 
InputFirst 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. 
OutputOutput total distance in a line for each testcase. 
Sample Input
210 3 22 28 25 110 4 12 28 25 10 10000
 
Sample Output
1826
 
Source2015 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 longusing 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);    }}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.