HDU 5303 Delicious Apples (2015多校第二場 貪心 + 枚舉)

來源:互聯網
上載者:User

標籤:sam   clear   str   stc   idt   testcase   pad   解題思路   tca   

Delicious ApplesTime Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 321    Accepted Submission(s): 95


Problem DescriptionThere are  apple trees planted along a cyclic road, which is  metres long. Your storehouse is built at position  on that cyclic road.
The th tree is planted at position , clockwise from position . There are  delicious apple(s) on the th tree.

You only have a basket which can contain at most  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?





There are less than 20 huge testcases, and less than 500 small testcases.
 
InputFirst line: , the number of testcases.
Then  testcases follow. In each testcase:
First line contains three integers, .
Next  lines, each line contains . 
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
 解題思路:注意到,最多僅僅有一次會繞整個圈走一次。因此,先貪心的處理左半環和右半環。然後枚舉繞整圈的時候從左側摘得蘋果和從右側摘得蘋果的數目。
#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <vector>#include <queue>#include <stack>#include <cmath>#include <algorithm>#define LL long longusing namespace std;const int MAXN = 100000 + 10;int L, N, K;LL x[MAXN];LL ld[MAXN], rd[MAXN];vector<LL>l, r;int main(){    int T;    scanf("%d", &T);    while(T--)    {        scanf("%d%d%d", &L, &N, &K);        l.clear(); r.clear();        int pos, num, m = 0;        for(int i=1;i<=N;i++)        {            scanf("%d%d", &pos, &num);            for(int i=1;i<=num;i++)                x[++m] = (LL)pos;        }        for(int i=1;i<=m;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 lsz = l.size(), rsz = r.size();        memset(ld, 0, sizeof(ld)); memset(rd, 0, sizeof(rd));        for(int i=0;i<lsz;i++)            ld[i + 1] = (i + 1 <= K ? l[i] : ld[i + 1 - K] + l[i]);        for(int i=0;i<rsz;i++)            rd[i + 1] = (i + 1 <= K ? r[i] : rd[i + 1 - K] + r[i]);        LL ans = (ld[lsz] + rd[rsz]) * 2;        for(int i=0;i<=lsz&&i<=K;i++)        {            int p1 = lsz - i;            int p2 = max(0, rsz-(K-i));            ans = min(ans, 2*(ld[p1] + rd[p2]) + L);        }        cout << ans << endl;    }    return 0;}


HDU 5303 Delicious Apples (2015多校第二場 貪心 + 枚舉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.