Delicious Apples

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

Total Submission(s): 555    Accepted Submission(s): 180

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



    题意:一个周长为l的圆,里面有m棵树,每棵树上分别有果子,如今有一个篮子最多能放k个苹果。起点在0处,问从起点出发把全部的苹果全都摘回来须要的最短距离是多少。
    思路:以为有的须要绕圆一圈摘到的果子的距离会比以摘到果子原路返回要更加优,想想就知道绕一圈的情况最多仅仅有一次,假设以整个树作为处理的对象非常难决定,所以能够将每个苹果作为处理的对象,由于最多有100000个果子,这种话分别处理两边的果子,寻找里面的最小的一个值就是要求的解。






#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h> using namespace std; int l,m,k;
int pl[100100],pr[100001];
int tree,num;
int pcnt[100010];
__int64 len_l[100010],len_r[100100]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(len_l,0,sizeof(len_l));
memset(len_r,0,sizeof(len_r));
int ans = 0;
scanf("%d%d%d",&l,&m,&k);
for(int i=0;i<m;i++)
{
scanf("%d%d",&tree,&num);
for(int j=0;j<num;j++)
{
pcnt[ans++] = tree;
}
}
int lsize = 0,rsize = 0;
for(int i=0;i<ans;i++)
{
if(pcnt[i]*2<l)
{
pl[lsize++] = pcnt[i];
}
else
{
pr[rsize++] = l-pcnt[i];
}
}
sort(pl,pl+lsize);
sort(pr,pr+rsize);
for(int i=0;i<lsize;i++)
{
len_l[i+1] = i+1<=k? pl[i]:len_l[i+1-k]+pl[i];
}
for(int i=0;i<rsize;i++)
{
len_r[i+1] = i+1<=k? pr[i]:len_r[i+1-k]+pr[i];
}
__int64 sum = (len_l[lsize] + len_r[rsize])*2;
for(int i=0;i<=k;i++)
{
if(i>lsize)
{
break;
}
int ll = lsize - i;
int rr = max(0,rsize-(k-i));
sum = min(sum,(len_l[ll] + len_r[rr])*2 + l);
}
printf("%I64d\n",sum);
}
return 0;
}

 

HDU 5303 Delicious Apples(思维题)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  7. hdu 5303 Delicious Apples

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

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

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

  9. hdu - 1172 猜数字 (思维题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1172 这个题换一种想法,可以找出四位数中所有满足条件的数看是否只有一个. #include <iostre ...

随机推荐

  1. IIS配置Asp.net时,出现“未能加载文件或程序集“System.Web.Extensions.Design, Version=1.0.61025.0”

    如果出现未能加载文件或程序集“System.Web.Extensions.Design, Version=1.0.61025.0, 主要是没有安装.net framwork 3.5,安装一下就行了. ...

  2. Android获取apk的版本及包名等信息

    import android.app.Activity; import android.content.Context; import android.content.pm.ApplicationIn ...

  3. luci范例

    转自:http://www.cnblogs.com/souroot/p/4511760.html LuCI (Lua Configiration Interface) 是OpenWRT 的Web 管理 ...

  4. OpenShift蓝绿及灰度部署

    内容转自https://blog.csdn.net/jj_tyro/article/details/80136316, 并不断补充,感谢作者. 1.蓝绿部署 蓝绿部署实现的是全流量切换,适合于在测试完 ...

  5. cocos2d-x 学习笔录:将iOS项目编译成Andriod项目

    一.Android 环境搭建 1.安装Andriod-NDK(Native Development Kit) 新建一个文件夹(eg:NDK),解压Android-NDK,将解压文件与压缩包放在一个目录 ...

  6. ECCV 2014 Oral Paper

    今年的ECCV 的paper已经挂到了网上.本文整了当中Oral Paper. 一,Tracking and Activity Recognition

  7. ylbtech-LanguageSamples-Indexers(索引器)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Indexers(索引器) 1.A,示例(Sample) 返回顶部 “索引器”示例 本示 ...

  8. iOS:LKDBHelper实体对象映射数据库-第三方框架(在FMDB的基础上进行二次封装)

    一 插件简介: 其github地址:https://github.com/li6185377/LKDBHelper-SQLite-ORM 全面支持 NSArray,NSDictionary, Mode ...

  9. spring的条件装配bean

    1 应用程序环境的迁移 问题: 开发软件时,有一个很大的挑战,就是将应用程序从一个环境迁移到另一个环境. 例如,开发环境中很多方式的处理并不适合生产环境,迁移后需要修改,这个过程可能会莫名的出现很多b ...

  10. 【转】The && and || Operator in JavaScript

    原文: https://blog.mariusschulz.com/2016/05/25/the-andand-and-operator-in-javascript The && an ...