题意大概就是有n框苹果放在长度为L的环上,每框有ai个苹果。你有一个容量为k的框。要你从0点处出发,随意走。框满了就回到0点把苹果放在那里。继续走直到把苹果都拿完为止。问你最少要走多少路程。

首先贪心的策略,无论往哪边走,碰到苹果就拿,拿满就滚回去。

然后碰到走一个半圈,框还剩下容量的情况,就须要dp了。主要是L为偶数的时候,若L/2的位置有苹果,该算是哪个半圈拿比較好呢?

当然也能够继续贪心,然而太麻烦了。直接dp。

而且由此贪心能够知道若存在须要走一圈的情况。肯定最多走一次一圈。

我们把苹果的位置当作它的权值,所有铺在环上。

dp[0][j]:左半圈拿完j位置的苹果就回去的最小代价

dp[1][j]:右半圈拿完j位置的苹果就回去的最小代价

非常显然,dp[i][j]=dp[i][j-k]+val[j]

最后再枚举走一圈的时候。从左半圈的底部拿走几个苹果,从右半圈的底部拿走几个苹果。不断更新ans就可以。

详细看代码吧。非常easy懂。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
int a[2][100010],len[2];
ll dp[2][100010];
int main()
{
int T;
cin>>T;
while(T--)
{
memset(len,0,sizeof(len));
int l,n,k;
scanf("%d%d%d",&l,&n,&k);
int m=l>>1;
while(n--)
{
int x,t;
scanf("%d%d",&x,&t);
int i=0;
if(x>m)
{
i=1;
x=l-x;
}
while(t--)
a[i][++len[i]]=x;
}
for(int i=0;i<2;i++)
sort(a[i],a[i]+len[i]+1);
for(int i=0;i<2;i++)
for(int j=0;j<=len[i];j++)
{
dp[i][j]=a[i][j];
if(j>=k)
dp[i][j]+=dp[i][j-k];
}
ll ans=dp[0][len[0]]+dp[1][len[1]]<<1;
for(int i=0;i<=k&&i<=len[0];i++)
ans=min(ans,2*(dp[0][len[0]-i]+dp[1][max(0,len[1]-(k-i))])+l);
cout<<ans<<endl;
}
}

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

Total Submission(s): 1078    Accepted Submission(s): 358

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 x&#31;i,
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

hdu5303Delicious Apples的更多相关文章

  1. CF449C Jzzhu and Apples (筛素数 数论?

    Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...

  2. [2015hdu多校联赛补题]hdu5303 Delicious Apples

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...

  3. [2012山东ACM省赛] Pick apples (贪心,完全背包,枚举)

    Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描述 Once ago, there is a mystery yard which on ...

  4. dp - 2015 Multi-University Training Contest 2 1004 Delicious Apples

    Delicious Apples Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5303 Mean: 一条长为L的环形路上种着n棵 ...

  5. SDUT 2408:Pick apples

    Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描述 Once ago, there is a mystery yard which on ...

  6. 从客户端(content="<span class="Apple-s...")中检测到有潜在危险的 Request.Form 值。

    从客户端(content="<span class="Apple-s...")中检测到有潜在危险的 Request.Form 值. 说明: 请求验证过程检测到有潜在 ...

  7. (中等) CF 585C Alice, Bob, Oranges and Apples,矩阵+辗转相除。

    Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples ...

  8. Pick apples(大范围贪心,小范围完全背包)

    Pick apples Time Limit: 1000MS Memory Limit: 165536KB Submit Statistic Discuss Problem Description O ...

  9. hdu多校第4场 B Harvest of Apples(莫队)

    Problem B. Harvest of Apples Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...

随机推荐

  1. Flexbox 布局教程

    Flex 布局是什么? Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. 任何一个容器都可以指定为 Flex 布局. 使用 fle ...

  2. SQL Server 审计

    审计(Audit)用于追踪和记录SQL Server实例或数据库中发生的事件,审计主要包括审计对象(Audit)和审计规范(Audit Specification),创建审计首先需要创建一个SQL S ...

  3. 2000W条数据,加入全文检索的总结

    一) 前期准备测试: 旧版的MySQL的全文索引只能用在MyISAM表格的char.varchar和text的字段上. 不过新版的MySQL5.6.24上InnoDB引擎也加入了全文索引,所以具体信息 ...

  4. C/C++程序在main之前执行代码

    来源:<周哥教IT.C语言深学活用>https://ke.qq.com/course/242707#tuin=a71606 我们在学习C/C++语言的时候,通常认为main函数是整个程序执 ...

  5. asp .net连接打开数据库初步

    1 #endregion59 WebDriver

  6. [转载] 解读ClassLoader

    转载自http://www.iteye.com/topic/83978 ClassLoader一个经常出现又让很多人望而却步的词,本文将试图以最浅显易懂的方式来讲解 ClassLoader,希望能对不 ...

  7. SQL Server 在生产环境中这样写存储过程的坑都避免了吗?

    概述 最近因为业务的需求写了一段时间存储过程,发现之前写的存储过程存在一些不严谨的地方,特别是TRY...CATCH中嵌套事务的写法:虽然之前写的并没有错,但是还是埋藏着很大的隐患在里面.希望这篇文章 ...

  8. 软件测试博客日记Day02-11.16日 —— 赵天宇 —— 禅道的使用和配置

    禅道 1. 安装 1. 进入禅道的官方下载地址:http://www.zentao.net/download/80053.html 2. 下载禅道开源版本. 3. 正常安装,注意一定要放在根目录下. ...

  9. 盘点一下立过的flag并立几个flag

    暑假前说了,要学opencv3,要看完冰火,要健身,要家教挣钱. 好样的,全都没落下. opencv3几乎是把80%的demo码了一遍. 冰火看完,还顺带学了一波知识,收获颇丰,搞到了马丁老爷子的几本 ...

  10. 问题(一)---线程池,锁、堆栈和Hashmap相关

    一.线程池: 多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力. 假设一个服务器完成一项任务所需时间为:T1 创建线程时间,T2 在线程中 ...