hdu 5303 Delicious Apples
这道题贪心 背包
假设在走半圆之内能够装满,那么一定优于绕一圈回到起点。所以我们从中点将这个分开,那么对于每一个区间由于苹果数非常少,所以能够利用pos[x]数组记录每一个苹果所在的苹果树位置,然后将苹果依照所在的位置排序,那么也就是我们知道每次拿k个苹果的代价是苹果所在的最远的位置。
最基本的是为什么这样选择是最优的结果
比方说在一条直线上
| pos | num |
|---|---|
| 1 | 4 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
篮子大小为6
那么我们能够选择先把位置4和位置2的带回家 花费 8
第二次 我们把位置3带回家 花费 6
第三次 我们把位置1带回家 花费2
那么总花费是 16
假设我们按顺序带呢 先带大的 比方位置4 的4个 、 位置3的2个 花费 8
然后带 位置3的1个 、 位置2的2个、位置1的3个 花费 6
最后带走 位置1的一个 花费 2
总花费是 16
我们发现并无差别 是选择的数据太水了吗
那么原理是什么呢
比方说三个连续的树上分别有 x y z 个苹果。篮子能够装k个苹果
假设 y+ z < k 那么肯定选择带走后面两个树的苹果 ,再带走第一个树的苹果 。这是最优解
假设 y+z >k &&x+z<k 呢 是不是感觉先带走第一个和第三个是最优解
我们能够看一下 带走第一个和第三个 那么总花费是 (z+(y+k-1)/k*y)*2
假设继续依照顺序从后面往前取 花费是 ( z+(y-k+z+k-1)/k*y+(x-(y-k+z)%k))*2
带入几组数 前一个式子>=后面的
那么依照顺序从后面往前取的贪心策略是正确的
注意点:最后枚举圈剩余的苹果数小于等于k时候,还有一面有可能是0,所以减得时候,有可能是负数
/* **********************************************
Auther: xueaohui
Created Time: 2015-7-25 13:19:20
File Name : D.cpp
*********************************************** */
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <functional>
#include <algorithm>
using namespace std;
#define ll long long
#define N 111111
ll l;
int n,k;
int a[N];
ll dpx[N],dpy[N];
ll pos[N];
vector<ll>x,y;
int main(){
int T;
scanf("%d",&T);
while(T--){
x.clear();
y.clear();
memset(dpx,0,sizeof(dpx));
memset(dpy,0,sizeof(dpy));
scanf("%I64d%d%d",&l,&n,&k);
int cnt = 0 ;
int all;
ll p;
for(int i=1;i<=n;i++){
scanf("%I64d%d",&p,&all);
for(int j=1; j<=all;j++)
pos[++cnt]=p;
}
//:printf("cnt = = %d\n",cnt);
for(int i=1;i<=cnt;i++){
ll pp =pos[i]*2;
if(pp<=l) x.push_back(pos[i]);
else y.push_back(l-pos[i]);
}
sort(x.begin(),x.end());
sort(y.begin(),y.end());
int xsize = x.size();
int ysize = y.size();
for(int i=1;i<=xsize;i++){
dpx[i] = (i>k)?dpx[i-k] + x[i-1]:x[i-1];
}
for(int i=1;i<=ysize;i++){
dpy[i] = (i>k)?dpy[i-k] + y[i-1]:y[i-1];
}
ll ans = dpx[xsize] + dpy[ysize];
ans *= 2;
for(int i=0;i<=k&&i<=xsize;i++){
int c = max(0,ysize+i-k);//注意点
ans = min(ans,(dpx[xsize-i] + dpy[c])*2+l);
}
printf("%I64d\n",ans);
}
}
hdu 5303 Delicious Apples的更多相关文章
- 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 ...
- 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 (2015多校第二场 贪心 + 枚举)
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 ...
- HDU 5303 Delicious Apples 美味苹果 (DP)
题意: 给一个长为L的环,起点在12点钟位置,其他位置上有一些苹果,每次带着一个能装k个苹果的篮子从起点出发去摘苹果,要将全部苹果运到起点需要走多少米? 思路: 无论哪处地方,只要苹果数超过k个,那么 ...
- [多校2015.02.1004 dp] hdu 5303 Delicious Apples
题意: 在一个长度为L的环上有N棵苹果树.你的篮子容量是K个苹果. 每棵苹果树上都有a[i]个苹果. 问你从0点出发最少要走多少距离能拿完所有的苹果. 思路: 我们考虑dp,dp[0][i]代表顺时针 ...
- 多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)
题目链接: 点击打开链接 题目大意: 在一个周长为L的环上.给出n棵苹果树.苹果树的位置是xi,苹果树是ai,苹果商店在0位置,人的篮子最大容量为k,问最少做多远的距离可以把苹果都运到店里 题目分析: ...
- Delicious Apples (hdu 5303 贪心+枚举)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
随机推荐
- 内置函数filter和map
filter filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filter()根据判断结果自动过滤掉不符合条件的元素,返回 ...
- 高德地图api之location定位
关于定位,分为GPS定位和网络定位.本文将详细描述的浏览器定位,属于网络定位.这是一种通过使用高德JS-API来实现位置定位.城市定位的方法,包含了IP定位,检索等多种网络定位方式.如果您的手机支持G ...
- 关闭 macOS Google Chrome 黑暗模式风格
终端执行命令 defaults write com.google.Chrome NSRequiresAquaSystemAppearance -bool YES 恢复 defaults write c ...
- MySQL sys Schema
MySQL sys Schema 使用sys Schema的先决条件 使用sys Schema sys Schema Progress Reporting sys Schema Object Refe ...
- 上海苹果维修点分享苹果电脑MACBOOK故障维修常见案例
苹果的电子设备无论是外观和性能都是无与伦比的美丽,很多开发者都开始选用苹果电脑macbook.近年来苹果售后维修点来维修苹果电脑的用户也越来越多,我们上海苹果维修点就整理分享了一些苹果电脑MACBOO ...
- 南阳理工 58 最少步数 (DFS)
描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0,0,1,1,0,0,0,1 1,0,1,0,1,1,0,1,1 1,0,0, ...
- laravel(4.2) +Zizaco
操作步骤:https://github.com/Zizaco/entrust/tree/1.0 这篇博客说的蛮详细的:http://blog.boolw.com/?p=241 简化后的步骤 1.在根项 ...
- luogu3313 [SDOI2014]旅行
对每一个宗教建一棵线段树,然后树剖搞搞 #include <iostream> #include <cstdio> using namespace std; int n, m, ...
- INFO main org.springframework.context.support.AbstractApplicationContext
原因, spring-framework-5.0.2.RELEASE 需要使用 jdk8.
- windows 环境下.Net使用Redis缓存
Redis简介 Redis是一个开源的,使用C语言编写,面向“键/值”对类型数据的分布式NoSQL数据库系统,特点是高性能,持久存储,适应高并发的应用场景.Redis纯粹为应用而产生,它是一个高性能的 ...