Delicious Apples

Time Limit: 5000/3000 MS (Java/Others)    Memory
Limit: 524288/524288 K (Java/Others)

Total Submission(s): 321    Accepted Submission(s): 95

Problem Description
There are  apple
trees planted along a cyclic road, which is  metres
long. Your storehouse is built at position  on
that cyclic road.

The 

rev=2.4-beta-2" alt="" style="">th
tree is planted at position 

rev=2.4-beta-2" alt="" style="">,
clockwise from position .
There are 

rev=2.4-beta-2" alt="" style=""> delicious
apple(s) on the 

rev=2.4-beta-2" alt="" style="">th
tree.



You only have a basket which can contain at most 

rev=2.4-beta-2" alt="" style=""> 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?



rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">



There are less than 20 huge testcases, and less than 500 small testcases.

 
Input
First line: ,
the number of testcases.

Then  testcases
follow. In each testcase:

First line contains three integers, 

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">.

Next 

rev=2.4-beta-2" alt="" style=""> lines,
each line contains 

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">.

 
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
 
解题思路:
注意到,最多仅仅有一次会绕整个圈走一次。因此,先贪心的处理左半环和右半环。然后枚举绕整圈的时候从左側摘得苹果和从右側摘得苹果的数目。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#define LL long long
using 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;
}

最新文章

  1. Debian 7 安装配置总结
  2. PHP的启动与终止
  3. EL表达式详解及应用实例
  4. Linux 命令 - find: 搜索文件
  5. [Android 中级]Voip之CSipSimple类库的编绎
  6. Xmanager4使用记录
  7. 汉化testlink
  8. mysql 创建函数 error Code: 1227. Access denied;
  9. vue怎么样创建组件呢??
  10. 《用Java写一个通用的服务器程序》02 监听器
  11. Eclipse的调试功能的10个小窍门[转]
  12. 将wiki人脸数据集中的图片按男女分类
  13. svn版本控制迁移到git
  14. c#文件图片操作
  15. markdown 书写文档的框架
  16. python全栈开发day87~91-整个流程梳理、CRM功能、知识点梳理
  17. Java 连接MongoDB集群的几种方式
  18. How the Bitcoin protocol actually works
  19. ASP.NET生成二维码
  20. 在触屏设备上面利用html5裁剪图片

热门文章

  1. windows关于定时执行的php脚本
  2. mybatis中sql标签和include标签
  3. MyBatis学习总结(18)——MyBatis与Hibernate区别
  4. mybatis 按照条件查询
  5. scp报错:Host key verification failed. REMOTE HOST IDENTIFICATION HAS CHANGED!
  6. hdu 2032 一维数组实现杨辉三角
  7. Hadoop解析--MapReduce
  8. 汇编 -- Hook API (MessageBoxW)
  9. 归并排序(Python)
  10. WebView简介(加速加载篇)