HDU 5303 Delicious Apples(思维题)
2015-07-24 14:35
162 查看
Delicious Apples
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 555 Accepted Submission(s): 180
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
题意:一个周长为l的圆,里面有m棵树,每棵树上分别有果子,现在有一个篮子最多能放k个苹果,起点在0处,问从起点出发把所有的苹果全都摘回来需要的最短距离是多少。
思路:以为有的需要绕圆一圈摘到的果子的距离会比以摘到果子原路返回要更加优,想想就知道绕一圈的情况最多只有一次,如果以整个树作为处理的对象很难决定,所以可以将每一个苹果作为处理的对象,因为最多有100000个果子,这样的话分别处理两边的果子,寻找里面的最小的一个值就是要求的解。
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> using namespace std; int l,m,k; int pl[100100],pr[100001]; int tree,num; int pcnt[100010]; __int64 len_l[100010],len_r[100100]; int main() { int T; scanf("%d",&T); while(T--) { memset(len_l,0,sizeof(len_l)); memset(len_r,0,sizeof(len_r)); int ans = 0; scanf("%d%d%d",&l,&m,&k); for(int i=0;i<m;i++) { scanf("%d%d",&tree,&num); for(int j=0;j<num;j++) { pcnt[ans++] = tree; } } int lsize = 0,rsize = 0; for(int i=0;i<ans;i++) { if(pcnt[i]*2<l) { pl[lsize++] = pcnt[i]; } else { pr[rsize++] = l-pcnt[i]; } } sort(pl,pl+lsize); sort(pr,pr+rsize); for(int i=0;i<lsize;i++) { len_l[i+1] = i+1<=k? pl[i]:len_l[i+1-k]+pl[i]; } for(int i=0;i<rsize;i++) { len_r[i+1] = i+1<=k? pr[i]:len_r[i+1-k]+pr[i]; } __int64 sum = (len_l[lsize] + len_r[rsize])*2; for(int i=0;i<=k;i++) { if(i>lsize) { break; } int ll = lsize - i; int rr = max(0,rsize-(k-i)); sum = min(sum,(len_l[ll] + len_r[rr])*2 + l); } printf("%I64d\n",sum); } return 0; }
相关文章推荐
- [电子书] 《Android编程入门很简单》
- iOS设备上的App设计规范
- Swift语言实现地图坐标弹跳动画
- Android日历行程
- iOS 图标、图形尺寸?
- Unity Mesh Collider 顶点太多报错
- Android默认输入法语言的修改以及SettingsProvider作用
- iOS地图位置开发
- iOS 实现百度LBS定位
- android技巧:快速重建system.img
- android静态检测工具
- AndroidActionBar入门小测试
- Android--数据库2
- [Android Pro] PackageManager#getPackageSizeInfo (hide)
- IOS中获取各个文件的目录路径的方法和NSFileManager类
- iOS Sprite Kit教程之申请和下载证书
- UNITY3D学习笔记5
- [Android新手区] android里面EditTex多行输入及输入置顶问题
- IOS开发中的沙盒机制
- android 权限大全