Hdu 5303 Delicious Apples 2015 Multi-University Training Contest 2
2015-07-24 20:56
393 查看
Delicious Apples
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 782 Accepted Submission(s): 263
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.
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
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define sf scanf #define mx 2111111 #define LL long long LL n,m,k; LL d[mx],d1[mx],d2[mx],f1[mx],f2[mx]; int main() { LL T; sf("%lld",&T); while(T--) { sf("%lld%lld%lld",&n,&m,&k); long long cnt = 0; for(LL i = 0;i<m;i++) { long long x,y; sf("%lld%lld",&x,&y); while(y){d[cnt++] = x;y--;} } m= cnt; k = min(k,m); long long n1=0,n2=0; for(LL i = 0;i<m;i++) if(d[i]*2<=n) d1[++n1] = d[i];else d2[++n2] = n-d[i]; //cout<<n1<<"--"<<n2<<endl; sort(d1+1,d1+n1+1); sort(d2+1,d2+n2+1); for(LL i = 1;i<=n1;i++) if(i<=k)f1[i] =d1[i]; else f1[i] = f1[i-k]+d1[i]; // for(LL i =0;i<=n1;i++) // cout<<i<<":"<<f1[i]<<" --- "<<d1[i]<<endl; for(LL i=1;i<=n2;i++) if(i<=k)f2[i] = d2[i]; else f2[i] = f2[i-k] +d2[i]; // cout<<f1[n1-1] <<" "<<f2[n2-1]<<endl; long long ans = 0; if(n1>0) ans+= f1[n1]; if(n2>0) ans+= f2[n2]; ans*=2; //cout<<ans<<endl; for(LL i = max(0LL,n1-k);i<n1;i++) { LL j = max(0LL,n2-(k-(n1-i))); // cout<<i<<" "<<j<<endl; // cout<<f1[i]<<" "<<f2[j]<<endl; ans = min(ans,(f1[i]+f2[j])*2+n); } printf("%lld\n",ans); } }
相关文章推荐
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- 感谢网友[Chen Hypo]帮我设计2015年新年banner
- Android px、dp、sp之间相互转换
- android中像素单位dp、px、pt、sp的比较
- 1.一些 贪心算法 的简单思维题:
- Android对px和dip进行尺寸转换的方法
- 贪心算法——字典序最小问题
- 贪心算法——区间调度问题
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题
- LeetCode之Maximum Product Subarray
- DP Flow