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. Java简述

    Java的特点(11个关键术语)from<Java核心技术I> 1) 简单性 2)  面向对象 3)  分布式 Java有一个丰富的例程库,用于处理像HTTP和FTP之类的TCP/IP协议 ...

  2. BZOJ 2150 部落战争 (二分图匹配)

    题目大意:给你一个n*m的棋盘,有一些坏点不能走,你有很多军队,每支军队可以像象棋里的马一样移动,不过马是1*2移动的,而军队是r*c移动的,军队只能从上往下移动,如果一个点已经被一直军队经过,那么其 ...

  3. Hive中的一种假NULL

    Hive中有种假NULL,它看起来和NULL一摸一样,但是实际却不是NULL. 例如如下这个查询: hive> desc ljn004; OK a       string Time taken ...

  4. myeclipse如何取消某一个文件的校验

  5. 2015 Multi-University Training Contest 7 hdu 5372 Segment Game

    Segment Game Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  6. java properties类读取配置文件

    1.JAVA Properties类,在java.util包里,具体类是java.util.properties.Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值 ...

  7. [Angular] Set Metadata in HTTP Headers with Angular HttpHeaders

    Besides sending (or requesting) the actual data to the server API, there’s also often the need to se ...

  8. Javaee 应用分层架构

    应用分层的优点:修改方便,仅修改有问题的那层以及其相邻几层即可,层数越多,其相应的资源分配也会更加平均 缺点:耗费时间,速度慢,调用占用大量堆栈. JAVAEE的分层: 4层分法:1.客户层:运行在客 ...

  9. WPF错误:必须使“Property”具有非 null 值。

    这个问题一般出如今Triggers中Property指定的类型为Nullable. 解决的方法就是用DataTrigger取代Trigger, 然后用Binding+Converter转换为详细非Nu ...

  10. 改动android 系统时间

    命令如 date -s "yyyymmdd.[[[hh]mm]ss]" 直接在CRT上执行,举例:date -s "20120801.120503" 但在adb ...