HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303
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
题意:
一个长为 L 的环行路线。有 n 颗苹果树,每颗苹果树上有 a[i] 个苹果,一个人在0点(仓库的位置),他有一个篮子,篮子每次最多仅仅能装 k 个苹果。求要装全然部的苹果而且回到仓库的最小路程;给出的苹果树坐标是按顺时针的。
官方题解:
PS:
贪心。把环从中间分为两段。分左右两条线。
利用 a[i] 数组记录每一个苹果所在的苹果树的位置,之后再将苹果依照所在的位置进行排序一下。
所以我们就知道了每次摘 k 个苹果的路程是最远的那个苹果所在的位置。
再用 sum[i] 表示摘第 i 个苹果时的最小代价和。
依据背包的思想得到:
if ( i <= k )
sum[i] = d[i]
else
sum[i] = d[i] + sum[i-k]
注意:
另一种情况是在最后当剩下的苹果少于等于 k 个时,也许一次性绕环一圈拿完最后的k个所需的路程更少。
枚举剩下的最后k个!
代码例如以下:
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define LL __int64
const int maxn = 100047;
vector <int> v1, v2;
LL a[maxn];
LL sum1[maxn], sum2[maxn]; int main()
{
int t;
int n, k, l;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&l,&n,&k);
int pos, num;
int h = 0;
for(int i = 0; i < n; i++)
{
scanf("%d%d",&pos,&num);
for(int j = 0; j < num; j++)
{
a[++h] = pos;
}
}
k = min(h,k);
v1.clear();
v2.clear();
for(int i = 1; i <= h; i++)
{
if(a[i]*2 < l)
{
v1.push_back(a[i]);
}
else
{
v2.push_back(l - a[i]);
}
}
memset(sum1,0,sizeof(sum1));
memset(sum2,0,sizeof(sum2));
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
int len1 = v1.size(), len2 = v2.size();
for(int i = 0; i < len1; i++)
{
int id = i+1;
if(id <= k)
{
sum1[id] = v1[i];
}
else
{
sum1[id] = v1[i]+sum1[id-k];
}
}
for(int i = 0; i < len2; i++)
{
int id = i+1;
if(id <= k)
{
sum2[id] = v2[i];
}
else
{
sum2[id] = v2[i]+sum2[id-k];
}
} LL ans = 2*(sum1[len1]+sum2[len2]);//来回
int t1, t2;
for(int i = 0; i <= k && i <= len1; i++)
{
t1 = len1 - i;
t2 = len2-(k-i);
if(t2 < 0)
{
t2 = 0;
}
ans = min(ans,2*(sum1[t1]+sum2[t2])+l);//最后不足k个绕行一圈所有摘走
}
printf("%I64d\n",ans);
}
return 0;
}
HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)的更多相关文章
- 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多校第二场 贪心 + 枚举)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- 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
这道题贪心 背包 假设在走半圆之内能够装满,那么一定优于绕一圈回到起点.所以我们从中点将这个分开,那么对于每一个区间由于苹果数非常少,所以能够利用pos[x]数组记录每一个苹果所在的苹果树位置,然后将 ...
- 多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)
题目链接: 点击打开链接 题目大意: 在一个周长为L的环上.给出n棵苹果树.苹果树的位置是xi,苹果树是ai,苹果商店在0位置,人的篮子最大容量为k,问最少做多远的距离可以把苹果都运到店里 题目分析: ...
- [多校2015.02.1004 dp] hdu 5303 Delicious Apples
题意: 在一个长度为L的环上有N棵苹果树.你的篮子容量是K个苹果. 每棵苹果树上都有a[i]个苹果. 问你从0点出发最少要走多少距离能拿完所有的苹果. 思路: 我们考虑dp,dp[0][i]代表顺时针 ...
- HDU 5303 Delicious Apples 美味苹果 (DP)
题意: 给一个长为L的环,起点在12点钟位置,其他位置上有一些苹果,每次带着一个能装k个苹果的篮子从起点出发去摘苹果,要将全部苹果运到起点需要走多少米? 思路: 无论哪处地方,只要苹果数超过k个,那么 ...
- Hdu5303 Delicious Apples 贪心
题目链接: HDU5303 题意: 有一条环形的长为L的路,仓库在位置0处, 这条路上有n棵苹果树,给出每棵苹果树的位置和苹果数量, 问用 一次最多能装K个苹果的篮子 把这条路上全部苹果採回仓库最 ...
随机推荐
- web移动端-弹性盒模型
(父元素加) : /*新版弹性盒模型*/ /* display: flex; */ /*设置主轴方向为水平方向*/ /* flex-direction: row; */ /*设置主轴方向为垂直方向*/ ...
- CodeForces-766D Mahmoud and a Dictionary 并查集 维护同类不同类元素集合
题目链接:https://cn.vjudge.net/problem/CodeForces-766D 题意 写词典,有些词是同义词,有些是反义词,还有没关系的词 首先输入两个词,需要判断是同义还是是反 ...
- java 获取config 配置文件
static ResourceBundle PropertiesUtil = ResourceBundle.getBundle("config"); public static S ...
- 【转载】02-PowerDesigner的下载及安装
原创路径:https://blog.csdn.net/ruyu00/article/details/79842807 一.下载 下载路径:https://pan.baidu.com/s/1WD7QHT ...
- C# winform压缩文件夹带进度条
注意:用了开源的CL.IO.Zip库 pbYSJD是进度条的控件名 btnImport是按钮控件名,当压缩结束之后,使按钮处于激活状态,否则无法点击按钮. /// <summary> // ...
- 2.Maven特点,Maven约定,建立第一个Maven项目
1 Maven是跨平台的项目管理工具.主要服务于基于java平台的项目构建,依赖管理和项目信息管理. 项目构建 清理à编译à測试à报告à打包à部署 理想的项目构建: 高度自己主动化 跨平台 可重 ...
- hadoop-07-ntp服务检查
hadoop-07-ntp服务检查 cd /etc/more /etc/ntp.conf里面进行了server的配置 service ntpd status / stop/ start 安装ntpd ...
- hdu3468 Treasure Hunting 二分匹配
//给一个n*m的图 //.表示空白地 //*表示有黄金 //#表示墙 //一个人须要依照A...Z..a..z的顺序以最短路径走到下一个 //每次仅仅能在他的路线上经过的地方取一块黄金 //问最多能 ...
- C++ 学习笔记(一些新特性总结3)
C++ 学习笔记(一些新特性总结3) public.protected 和 private 继承 public 继承时,基类的存取限制是不变的. class MyClass { public: // ...
- ZOJ2326Tangled in Cables(最小生成树)
Tangled in Cables Time Limit: 2 Seconds Memory Limit: 65536 KB You are the owner of SmallCableC ...