Delicious Apples
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 311 Accepted Submission(s): 92
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 i th tree is planted at position xi , clockwise from position 0 . There are ai delicious apple(s) on the i th 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 i th tree is planted at position xi , clockwise from position 0 . There are ai delicious apple(s) on the i th 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
题意:
有一个圈,圈上不同位置有一定数量的苹果,苹果与起点有个距离。初始位置在0点。有个篮子,每次可以装K个苹果。问,要把所有的苹果都用篮子装到起点,至少要走多远的距离。
分析:
苹果总数 <= 100000. 那么按顺序排序每个苹果。
按顺时针,和逆时针分别处理。
dp[0][i]表示只能顺时针向前走,逆时针回到起点。把顺时针的前i个苹果带回原点的最小路程
那么dp[0][i] = dp[0][i-K] + 2*dist[i] -----------dist[i]表示第i个苹果顺时针距离起点的距离
同理逆时针的转移方程为
dp[1][i] = dp[1][i+K] + 2*(L-dist[i])
然后枚举第i个苹果是分界线,i个苹果和之前的苹果是顺时针取得,i+1和之后的苹果是逆时针取得
那么ans = min(ans,dp[0][i]+dp[1][i+1])
但是存在这样的情况,取走第i个苹果的时候,不原路放回,二是直接向前走到终点。
那么ans = min(ans,dp[0][i-K]+dp[1][i+1] + L)
最后ans就是答案
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define maxn 100007
struct Point{
int x;
int num;
};
Point p[100007];
Point px[maxn];
int cmp(Point a,Point b){
return a.x < b.x;
}
ll dp[2][maxn];
int main(){
int t,L,n,K;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&L,&n,&K);
int cnt = 1,x,y;
for(int i = 0;i < n; i++){
scanf("%d%d",&px[i].x,&px[i].num);
}
sort(px,px+n,cmp);
for(int i = 0;i < n; i++){
while(px[i].num--){
p[cnt++].x = px[i].x;
}
}
memset(dp[0],0,sizeof(ll)*(cnt+3));
memset(dp[1],0,sizeof(ll)*(cnt+3));
for(int i = 1;i < cnt; i++){
dp[0][i] = dp[0][max(0,i-K)] + p[i].x * 2;
}
for(int i = cnt-1;i > 0; i--){
dp[1][i] = dp[1][min(cnt,i+K)] + (L - p[i].x) * 2;
}
ll ans = min(dp[0][cnt-1],dp[1][1]);
for(int i = 1;i < cnt; i++){
ans = min(ans,dp[0][i]+dp[1][i+1]);
ans = min(ans,dp[0][max(0,i-K)] + dp[1][i+1] + L);
}
printf("%I64d\n",ans);
}
return 0;
}