Delicious Apples

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1057    Accepted Submission(s): 354

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 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.

 
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
 
解题:核心思想是贪心,关键在于枚举最后剩下的K个苹果。
 
先把圆分成两半,用sum[i]算出到i最小需要多长路
 
 
为什么是sum[i] = sum[i-k] + L[i]呢?
 
这个其实是倒推!很明显,最后可能从前开始的不足K个,为什么不是从前面推呢?
 
很明显,无论是从前面推还是后面推,其包含的K长度的区间个数是一定的,
 
但是从后面推得到好处就是和会更少!
 
这里体现了传说中的贪心思想。
 
明显从后面推,前面部分的值更少
 
最后我们需要枚举K个长度的区间,横跨两个半圆
LL ret = (sum[Lsize][0] + sum[Rsize][1])<<1;
这句话表示纯粹的使用不环绕圆的方式取完所有苹果的最短路径,是的最短,因为前面贪心了嘛。
 

int a = Lsize - i; 表示的是什么呢?表示从左半圆选择i个 那么剩下的部分不跨圆,继续使用拿完原路返回策略
int b = max(0,Rsize - (K - i)); 剩下的 K- i个采用拿了原路返回的策略
ret = min(ret,((sum[a][0] + sum[b][1])<<1) + Len); 那么这K个,我们采用拿了 不原路返回,采用绕圆的方法

所以多了Len

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
LL d[maxn],tot,sum[maxn][];
vector<LL>L,R;
int main() {
int kase,n,K,m,pos;
LL Len;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d%d",&pos,&n,&K);
L.clear();
R.clear();
Len = pos;
for(int i = tot = ; i < n; ++i) {
scanf("%d%d",&pos,&m);
for(int j = ; j < m; ++j)
d[tot++] = pos;
}
for(int i = ; i < tot; ++i) {
if(d[i]* < Len) L.push_back(d[i]);
else R.push_back(Len - d[i]);
}
sort(L.begin(),L.end());
sort(R.begin(),R.end());
int Lsize = L.size(),Rsize = R.size();
sum[][] = sum[][] = ;
for(int i = ; i < Lsize; ++i)
if(i + <= K) sum[i + ][] = L[i];
else sum[i + ][] = sum[i - K + ][] + L[i];
for(int i = ; i < Rsize; ++i)
if(i + <= K) sum[i+][] = R[i];
else sum[i+][] = sum[i-K+][] + R[i];
LL ret = (sum[Lsize][] + sum[Rsize][])<<;
for(int i = ; i <= Lsize && i <= K; ++i) {
int a = Lsize - i;
int b = max(,Rsize - (K - i));
ret = min(ret,((sum[a][] + sum[b][])<<) + Len);
}
cout<<ret<<endl;
}
return ;
}

2015 Multi-University Training Contest 2 hdu 5303 Delicious Apples的更多相关文章

  1. HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 Problem Description There are n apple trees plan ...

  2. HDU 5303 Delicious Apples (贪心 枚举 好题)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  3. HDU 5303 Delicious Apples(思维题)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  4. [多校2015.02.1004 dp] hdu 5303 Delicious Apples

    题意: 在一个长度为L的环上有N棵苹果树.你的篮子容量是K个苹果. 每棵苹果树上都有a[i]个苹果. 问你从0点出发最少要走多少距离能拿完所有的苹果. 思路: 我们考虑dp,dp[0][i]代表顺时针 ...

  5. HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  6. HDU 5303 Delicious Apples 美味苹果 (DP)

    题意: 给一个长为L的环,起点在12点钟位置,其他位置上有一些苹果,每次带着一个能装k个苹果的篮子从起点出发去摘苹果,要将全部苹果运到起点需要走多少米? 思路: 无论哪处地方,只要苹果数超过k个,那么 ...

  7. hdu 5303 Delicious Apples

    这道题贪心 背包 假设在走半圆之内能够装满,那么一定优于绕一圈回到起点.所以我们从中点将这个分开,那么对于每一个区间由于苹果数非常少,所以能够利用pos[x]数组记录每一个苹果所在的苹果树位置,然后将 ...

  8. 多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)

    题目链接: 点击打开链接 题目大意: 在一个周长为L的环上.给出n棵苹果树.苹果树的位置是xi,苹果树是ai,苹果商店在0位置,人的篮子最大容量为k,问最少做多远的距离可以把苹果都运到店里 题目分析: ...

  9. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

随机推荐

  1. JavaScript 运行机制 & EventLoop

    JavaScript 运行机制 & EventLoop 看阮老师博客和自己的理解,记录的学习笔记,js的单线程和 事件EventLoop 机制. 1. JavaScript是单线程 JavaS ...

  2. BZOJ 3524 [POI2014]KUR-Couriers (主席树)

    题目大意:给你一个序列,求某个区间出现次数大于一半的数是什么 主席树裸题,刷刷水题提升自信= = #include <cstdio> #include <cstring> #i ...

  3. php 与 nginx 的两种处理方式

    1.IP:Port 监听方式 php-fpm docker pull PHP:2.4-alpine nginx.conf fastcgi_pass 127.0.0.1:9000; php-fpm 在容 ...

  4. myeclipse反编译安装 jd-gui.exe下载

    一:在线安装 1.Help->Install New Site Name:** Location:http://jd.benow.ca/jd-eclipse/update 二:手动安装 1.下载 ...

  5. js常用特效-幻灯片

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. Mybatis解决了JDBC编程哪些问题

    一:Mybatis简介 MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动.创建connection.创 ...

  7. Android学习JNI,使用JNI实现字符串加密

    本节学习使用C语言加密字符串,大家都知道使用JAVA实现的加密都能够反编译的,而使用C写的加密是非常难被反编译的.所以我们使用JNI学习怎样使用C实现对字符串的加密. 首先:我们实现一个界面 布局文件 ...

  8. nyoj--1237--最大岛屿(dfs+数据处理)

    最大岛屿 时间限制:1000 ms  |  内存限制:65535 KB 难度: 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的战 ...

  9. 大量文件时使用ls

    有时一个目录下的文件实在太多, ls的时候就卡住了. 其实, 如果不加排序的话, 就可以迅速的显示文件. ls -f 解释: -f do not sort, enable -aU, disable - ...

  10. 你不知道的JavaScript(十一)函数参数

    JavaScript函数的定义和使用非常简单,我们看一下下面的代码: <script type="text/javascript"> var sum = functio ...