[多校2015.02.1004 dp] hdu 5303 Delicious Apples
题意:
在一个长度为L的环上有N棵苹果树。你的篮子容量是K个苹果。
每棵苹果树上都有a[i]个苹果。
问你从0点出发最少要走多少距离能拿完所有的苹果。
思路:
我们考虑dp,dp[0][i]代表顺时针取i个苹果的最短距离。
dp[1][i]代表逆时针取i个苹果的最短距离。
那么设苹果的总是为sum
那么ans=dp[0][i]+dp[sum-i] (0<=i<=sum)
代码:
#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"iostream"
#include"queue"
#include"map"
#include"vector"
#include"string"
#include"cmath"
using namespace std;
#define ll __int64
ll l;
struct node
{
int x,s;
} ap[123456];
ll dp[2][123456];
int cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,k;
scanf("%I64d%d%d",&l,&n,&k);
for(int i=0; i<n; i++) scanf("%d%d",&ap[i].x,&ap[i].s);
memset(dp,0,sizeof(dp));
sort(ap,ap+n,cmp);
int tep=1,sum=0; //tep代表取了几个苹果 由于一定是递增的
for(int i=0; i<n; i++)
{
for(int j=0; j<ap[i].s; j++)
{
if(tep-k<0) dp[0][tep]=dp[0][0]+min(l,2LL*ap[i].x);
else dp[0][tep]=dp[0][tep-k]+min(l,2LL*ap[i].x);
tep++;
}
} tep=1;
for(int i=n-1; i>=0; i--)
{
sum+=ap[i].s;
for(int j=0; j<ap[i].s; j++)
{
if(tep-k<0) dp[1][tep]=dp[1][0]+min(l,2LL*(l-ap[i].x));
else dp[1][tep]=dp[1][tep-k]+min(l,2LL*(l-ap[i].x));
tep++;
}
} ll ans=999999999999999999LL;
for(int i=0;i<=sum;i++) ans=min(ans,dp[0][i]+dp[1][sum-i]);
printf("%I64d\n",ans);
}
return 0;
}
[多校2015.02.1004 dp] hdu 5303 Delicious Apples的更多相关文章
- 2015 Multi-University Training Contest 2 hdu 5303 Delicious Apples
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- HDU 5303 Delicious Apples (贪心 枚举 好题)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- HDU 5303 Delicious Apples(思维题)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 Problem Description There are n apple trees plan ...
- 多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)
题目链接: 点击打开链接 题目大意: 在一个周长为L的环上.给出n棵苹果树.苹果树的位置是xi,苹果树是ai,苹果商店在0位置,人的篮子最大容量为k,问最少做多远的距离可以把苹果都运到店里 题目分析: ...
- HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- HDU 5303 Delicious Apples 美味苹果 (DP)
题意: 给一个长为L的环,起点在12点钟位置,其他位置上有一些苹果,每次带着一个能装k个苹果的篮子从起点出发去摘苹果,要将全部苹果运到起点需要走多少米? 思路: 无论哪处地方,只要苹果数超过k个,那么 ...
- hdu 5303 Delicious Apples
这道题贪心 背包 假设在走半圆之内能够装满,那么一定优于绕一圈回到起点.所以我们从中点将这个分开,那么对于每一个区间由于苹果数非常少,所以能够利用pos[x]数组记录每一个苹果所在的苹果树位置,然后将 ...
- 2018多校第九场1004(HDU 6415) DP
本以为是个找规律的题一直没找出来... 题目:给你一个n*m的矩阵和1-n*m个数,问有多少种情况满足纳什均衡的点只有一个.纳什均衡点是指这个元素在所在行和所在列都是最大的. 思路:吉老师直播的思路: ...
随机推荐
- HDU 6136 Death Podracing(循环链表)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6136 [题目大意] 一堆人在操场上跑步,他们都有一定的速度和初始位置, 当两个人相遇的时候编号较小 ...
- python开发_tkinter
Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.Tk和Tkinter可以在大多数的Unix平台下使用, 同样可以应用在Windows和Macint ...
- java - 内存泄漏
内存泄漏问题产生原因 长生命周期的对象持有短生命周期对象的引用就很可能发生内存泄露,尽管短生命周期对象已经不再需要,但是因为长生命周期对象持有它的引用而导致不能被回收,这就是java中内存泄露的发生场 ...
- BR16F84 OBD II Interface Chip For PWM, VPW, and ISO 9141-2 Vehicles
http://faq.ford77.ru/pdf/eec/DataSheet.pdf FEATURES:Operating Voltage 5.0 VOperating Current 5 Ma. T ...
- Install Linux Kernel 4.10 In CentOS and Ubuntu
https://www.ostechnix.com/install-linux-kernel-4-10-centos-ubuntu/
- 前端必备工具-Emmet (Zen Coding)
Emmet 可以快速的编写 HTML 和 CSS,输入指令如: ul#nav>li*4>a*4 敲击一下TAB 键,就会输出: <ul id="nav"> ...
- DCI:The DCI Architecture: A New Vision of Object-Oriented Programming
SummaryObject-oriented programming was supposed to unify the perspectives of the programmer and the ...
- redis的主从配置 扩容
由于redis的集群功能没有完全编写好,见:http://redis.io/topics/cluster-spec 集群只有测试版的第8个版本redis3.0.0 见 http://redis.io/ ...
- java解析json数组
java解析json数组 import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; ...
- HTML学习要点
目标 掌握HTML基本语法,了解HTML Document结构,能熟练使用HTML Element对象. 要点 基本概念:什么是HTML.HTML标签? 熟悉常用的HTML标签含义以及应用场合. ht ...