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 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.
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.
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取大,三是每次要清零
題目連結: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);
}
}
相關文章
- hdu5380 貪心+雙端佇列佇列
- HDU 4550卡片遊戲(貪心)遊戲
- HDU5630 Hiking(貪心+優先佇列)佇列
- HDU 5813 Elegant Construction (貪心)Struct
- hdu4268 multiset應用 貪心
- hdu 3177Crixalis's Equipment(貪心)UI
- 反悔貪心雜題
- hdu 1789 Doing Homework again(簡單貪心)AI
- 貪心 做題筆記筆記
- 「貪心」做題記錄
- hdu4313 貪心並查集 || 樹形dp並查集
- POJ 2718簡單列舉貪心演算法(好久沒寫程式碼了。。)演算法
- 貪心
- HDU 4252A Famous City(弱資料可以使用貪心)
- 貪心(入門簡單題)
- 【力扣】最大子陣列和(貪心)力扣陣列
- 2017廣東工業大學程式設計競賽決賽 題解&原始碼(A,數學解方程,B,貪心博弈,C,遞迴,D,水,E,貪心,面試題,F,貪心,列舉,LCA,G,dp,記憶化搜尋,H,思維題)...程式設計原始碼遞迴面試題
- 演算法基礎 - 列舉/遞迴/動歸/深廣搜/二分/貪心演算法遞迴
- 列舉子集+預處理最佳化dp+貪心視角轉化成可做dp
- 求最大子陣列(貪心演算法)陣列演算法
- 反悔貪心
- Supermarket(貪心)
- HDU 5135 Little Zu Chongzhi's Triangles(狀壓dp或者貪心)
- HDU 5339 Untitled (狀態壓縮列舉)
- HDU 4937 Lucky Number(列舉進位制)
- 貪心演算法——換酒問題演算法
- 8.13(優先佇列貪心維護+打表找規律+對頂堆優先佇列+DFS減枝+貪心dp)佇列
- HDU 4427 Math Magic【dp+優化+滾動陣列】【好題】優化陣列
- HDU - 6003 Problem Buyer題解(貪心選擇演算法,鴿巢原理,優先佇列維護)演算法佇列
- 加油站問題(貪心演算法)演算法
- 貪心演算法篇——區間問題演算法
- JAVA列舉的作用與好處Java
- 貪心模式記錄模式
- 貪心、構造合集
- 貪心演算法演算法
- Sunscreen POJ - 3614(防曬油) 貪心-優先佇列佇列
- 2013南京站I題||hdu4810 二進位制列舉
- UVA11292 HDU1902 POJ3646 The Dragon of Loowater【貪心】Go