hdu5303Delicious Apples
题意大概就是有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
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.
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.
2
10 3 2
2 2
8 2
5 1
10 4 1
2 2
8 2
5 1
0 10000
18
26
hdu5303Delicious Apples的更多相关文章
- CF449C Jzzhu and Apples (筛素数 数论?
Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...
- [2015hdu多校联赛补题]hdu5303 Delicious Apples
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...
- [2012山东ACM省赛] Pick apples (贪心,完全背包,枚举)
Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描述 Once ago, there is a mystery yard which on ...
- 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棵 ...
- SDUT 2408:Pick apples
Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描述 Once ago, there is a mystery yard which on ...
- 从客户端(content="<span class="Apple-s...")中检测到有潜在危险的 Request.Form 值。
从客户端(content="<span class="Apple-s...")中检测到有潜在危险的 Request.Form 值. 说明: 请求验证过程检测到有潜在 ...
- (中等) 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 ...
- Pick apples(大范围贪心,小范围完全背包)
Pick apples Time Limit: 1000MS Memory Limit: 165536KB Submit Statistic Discuss Problem Description O ...
- hdu多校第4场 B Harvest of Apples(莫队)
Problem B. Harvest of Apples Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
随机推荐
- hdu 6231 -- K-th Number(二分+尺取)
题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...
- Navi.Soft31.产品.登录器(永久免费)
1系统简介 1.1功能简述 电商平台和传统店铺相比,确实方便不少,直接在网上下单,快递直接送货到家.这其中,做电商平台的童鞋表示压力很大,因为可能同时开很多店铺,每个店铺都要登录.查看订单量.发货拣货 ...
- 清理win10过期补丁的命令
作用是删除已经被新版本取代的旧系统文件 DISM.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase 注1: 执行后, 补丁就无法 ...
- Java8一:Lambda表达式教程
1. 什么是λ表达式 λ表达式本质上是一个匿名方法.让我们来看下面这个例子: public int add(int x, int y) { return x + y; } 转成 ...
- C#进阶--WebApi异常处理机制
其实对于C#异常处理大家都不陌生,但是对于在WeiApi上的异常处理实际上也和传统异常处理区别不大,但是却经过封装可以让异常更加友好,https://docs.microsoft.com/en-us/ ...
- Ubuntu初始化设置
1.安装chrome sudo apt-get install chromium-browser sudo apt-get install pepperflashplugin-nonfree sudo ...
- 【转】在SAE部署WordPress
原文:http://blog.csdn.net/jackrex/article/details/9305835 虽然几个月前我是以安装应用的形式将wordpress部署在sae,虽然当时sae应用商店 ...
- Memcached 及 Redis 架构分析和区别比较
Memcached和Redis作为两种Inmemory的key-value数据库,在设计和思想方面有着很多共通的地方,功能和应用方面在很多场合下(作为分布式缓存服务器使用等) 也很相似,在这里把两者放 ...
- iis7 部署网站 403错误
C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i 403 - 禁止访问: 访问被拒绝. 您无权使用所提供的凭据查看此 ...
- 简述Handler机制
我会对android的消息处理有三个核心类逐步介绍,他们分别是:Looper,Handler和Message.其实还有一Message Queue(消息队列),知道它是队列即可,就像我们所熟知的数组, ...