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. IOS - PDF合并

    #pragma mark - Merge PDF - (void)mergePDF { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSD ...

  2. qt 透明化方法汇总

    一. QT 透明设置 背景,标题栏透明,下级Widget,painter绘出来的(比如,drawtext,drawline)不透明 QWidget window; window.setWindowFl ...

  3. AjAX 常用参数

    1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...

  4. VUE:过渡&动画

    VUE:过渡&动画 vue动画的理解 1)操作css的 trasition 或 animation 2)vue会给目标元素添加/移除特定的class 3)过渡的相关类名 xxx-enter-a ...

  5. array_combine php一个比较偏门的数组函数

    这函数  返回数组1的值 当做key,把数组2的值当做value,   当查询数据库用了 group +GROUP_CONCAT 两个组合时,(例如查询某个班级的,用户名,用户id,返回的是字符串,打 ...

  6. 安卓WebView的使用,在应用程序中嵌入一个浏览器,轻松地展示各种各样的网页

    WebView 在应用程序中嵌入一个浏览器,轻松地展示各种各样的网页. 1.定义一个WebView位置 <?xml version="1.0" encoding=" ...

  7. .net core @Html 自定义属性中包含特殊符号解决

    最近自己在练手项目用到了VUE 绑定属性的时候发现 有: -符号 这样显然是不支持的.之前发现 v-on  这种-符号也是不支持的 但是可用 @v_on 替代.可是找遍了所有资料也没找到:转义符 当时 ...

  8. AOJ GRL_1_A: Single Source Shortest Path (Dijktra算法求单源最短路径,邻接表)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A Single Source Shortest Path In ...

  9. ARM的六大类指令集---LDR、LDRB、LDRH、LDM、STR、STRB、STRH、STM

    http://blog.csdn.net/u013477200/article/details/50723555

  10. IE6 css fixed

    .fixed-top{position:fixed;bottom:auto;top:0px;} .fixed-bottom{position:fixed;bottom:0px;top:auto;} . ...