题意大概就是有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. Centos7.4下用Docker-Compose部署WordPress(续)-服务器端用Nginx作为反向代理并添加SSL证书(阿里云免费DV证书)

    前言 在我写完Centos7.4下用Docker-Compose部署WordPress这篇文章后,我的个人博客已经正式的开始运作.但考虑到网站访问的安全性以及今后可能会重复利用服务器来部署其他网站的可 ...

  2. 用Nodejs+Express搭建web,nodejs路由和Ajax传数据并返回状态,nodejs+mysql通过ajax获取数据并写入数据库

    小编自学Nodejs,看了好多文章发现都不全,而且好多都是一模一样的 当然了,这只是基础的demo,经供参考,但是相信也会有收获 今天的内容是用Nodejs+Express搭建基本的web,然后呢no ...

  3. 【WEB API项目实战干货系列】- WEB API入门(一)

    这篇做为这个系列的第一篇,做基本的介绍,有经验的人可以直接跳到第二部分创建 ProductController.   创建 Web API 项目 在这里我们使用VS2013, .NET 4.5.1创建 ...

  4. Js的闭包,这篇写的是比较清晰明了的

    一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量 ...

  5. ssm框架下web项目,web.xml配置文件的作用

    1. web.xml中配置了CharacterEncodingFilter,配置这个是拦截所有的资源并设置好编号格式. encoding设置成utf-8就相当于request.setCharacter ...

  6. java中的内存溢出和内存泄漏

    内存溢出:对于整个应用程序来说,JVM内存空间,已经没有多余的空间分配给新的对象.所以就发生内存溢出. 内存泄露:在应用的整个生命周期内,某个对象一直存在,且对象占用的内存空间越来越大,最终导致JVM ...

  7. linux操作系统基础篇(八)

    shell脚本的变量以及正则表达式 一.变量 含义:程序的运行就是一些列状态的变量->用变量值的变化去表示. 命名规则 以字母或下划线开头,剩下的部分可以是:字母.数字.下划线. 最好遵循下述规 ...

  8. mysql数据库的安装与基本配置

    目录 绿色版下载 mysql绿色版(5.7版本的安装与配置) 绿色版下载: mysql官网下载地址:https://www.oracle.com/index.html mysql绿色版(5.7版本的安 ...

  9. TFboy养成记 tf.cast,tf.argmax,tf.reduce_sum

    referrence: 莫烦视频 先介绍几个函数 1.tf.cast() 英文解释: 也就是说cast的直译,类似于映射,映射到一个你制定的类型. 2.tf.argmax 原型: 含义:返回最大值所在 ...

  10. 最耗性能的SQL语句

    设计优化–常见杀手级SQL •SELECT * vsSELECT col1, col2 •ORDER BY RAND() •LIMIT huge_num, offset •SELECT COUNT(* ...