搜索的应用-计算最优解

题目:

You are given nn packages of wiwi kg from a belt conveyor in order (i=0,1,...n−1i=0,1,...n−1). You should load all packages onto kk trucks which have the common maximum load PP. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load PP.

Write a program which reads nn, kk and wiwi, and reports the minimum value of the maximum load PP to load all packages from the belt conveyor.

Input

In the first line, two integers nn and kk are given separated by a space character. In the following nn lines, wiwi are given respectively.

Output

Print the minimum value of PP in a line.

Constraints

  • 1≤n≤100,0001≤n≤100,000
  • 1≤k≤100,0001≤k≤100,000
  • 1≤wi≤10,0001≤wi≤10,000

Sample Input 1

5 3

8

1

7

3

9

Sample Output 1

10

If the first truck loads two packages of {8,1}{8,1}, the second truck loads two packages of {7,3}{7,3} and the third truck loads a package of {9}{9}, then the minimum value of the maximum load PP shall be 10.

Sample Input 2

4 2

1

2

2

6

Sample Output 2

6

If the first truck loads three packages of {1,2,2}{1,2,2} and the second truck loads a package of {6}{6}, then the minimum value of the maximum load PP shall be 6.

思路:

其实这道题目的思路非常简单,就是需要看清楚题目的意思,还有要小心TLE。为:只要卡车的运载量没有达到P,我们就让其按照顺序装货物,最后在计算所有卡车运载量的总和即可。这里以P为实参,编写一个返回可装载货物数v的函数v=f(P)。这函数的算法复杂度为O(n)。然后只要调用这个函数,利用“P增加,v也增加”(严格来说是P增加v也不会减少)的性质,用二分法搜索求P。此时算法的复杂度为O(nlogP)。

代码:

 #include <iostream>

 using namespace std;
#define MAX 100000
typedef long long llong;
int n,k;
llong T[MAX]; int check(llong P)
{
int i=;
for(int j=;j<k;j++)
{
llong s=;
while(s+T[i]<=P)
{
s+=T[i];
i++;
if(i==n) return n;
}
}
return i;
} int solve()
{
llong left=;
llong right=*;
llong mid;
while(right-left>)
{
mid=(right+left)/;
int v=check(mid);
if(v>=n) right=mid;
else
left=mid;
}
return right;
} int main()
{
cin>>n>>k;
for(int i=;i<n;i++)
{
cin>>T[i];
}
llong ans=solve();
cout<<ans<<endl;
return ;
}

总结:

题目说了,是在传送带上依次送来的货物,意思就是说,只要传送带送来了货物,就要将其装载到货车上。而在第一次看到题目的时候没有注意到这个依次,所以,总是弄不明白书上的思路,浪费了很多的时间。

搜索的应用--计算最优解:Aizu - ALDS1_4_D Allocation的更多相关文章

  1. 如何使用 Lucene 做网站高亮搜索功能?

    现在基本上所有网站都支持搜索功能,现在搜索的工具有很多,比如Solr.Elasticsearch,它们都是基于 Lucene 实现的,各有各的使用场景.Lucene 比较灵活,中小型项目中使用的比较多 ...

  2. Elasticsearch学习笔记(十四)relevance score相关性评分的计算(1)

    一.多shard场景下relevance score不准确问题     1.问题描述:            多个shard下,如果每个shard包含指定搜索条件的document数量不均匀的情况下, ...

  3. 记忆化搜索 P1464 Function

    题目描述 对于一个递归函数w(a,b,c) 如果a≤0 or b≤0 or c≤0就返回值1. 如果a>20 or b>20 or c>20就返回w(20,20,20) 如果a< ...

  4. 从零搭建 ES 搜索服务(六)相关性排序优化

    一.前言 上篇介绍了搜索结果高亮的实现方法,本篇主要介绍搜索结果相关性排序优化. 二.相关概念 2.1 排序 默认情况下,返回结果是按照「相关性」进行排序的--最相关的文档排在最前. 2.1.1 相关 ...

  5. ElasticStack系列之十二 & 搜索结果研究

    问题 使用 ElasticSearch 做搜索 时,比如用户输入 --> 柠檬,搜出来的结果 --> 柠檬汽水,柠檬味牙膏等在前面,真正想要的水果那个 柠檬 在后面.已经在中文分词中加了 ...

  6. Lucene第二讲——索引与搜索

    一.Feild域 1.Field域的属性 是否分词:Tokenized 是:对该field存储的内容进行分词,分词的目的,就是为了索引. 否:不需要对field存储的内容进行分词,不分词,不代表不索引 ...

  7. 【智能算法】迭代局部搜索(Iterated Local Search, ILS)详解

    迭代局部搜索(Iterated Local Search, ILS) 源代码下载请关注微信公众号[程序猿声],在后台回复:[ILS],不包括[]即可下载. 00 目录 局部搜索算法 简单局部搜索 迭代 ...

  8. 怎么以最新汇率牌价计算XX美元相当于多少人民币

    http://www.meiguozhuji.com/exchange-rate 美国主机都是以美元来报价的,至于XX美元相当于多少人民币,很多朋友都不太清楚.为了让大家更直接的了解购买美国主机需要花 ...

  9. uva 11762 数学期望+记忆化搜索

    题目大意:给一个正整数N,每次可以在不超过N的素数中随机选择一个P,如果P是N的约数,则把N变成N/p,否则N不变,问平均情况下需要多少次随机选择,才能把N变成1? 分析:根据数学期望的线性和全期望公 ...

随机推荐

  1. Springboot-技术专区-war包部署在Tomcat上并修改默认端口

    springboot项目内置Tomcat,直接打成jar包在dos下运行即可,但有时我们需要用war包以非内嵌Tomcat的方式来部署,以下是本人的实际经验 1.首先需要修改pom.xml文件 < ...

  2. 【TWRP】使用adb sideload线刷ROM的方法

    本教程详细介绍 手机刷三方ROM 之前需要安装的 TWRP 这个神器工具 楼主的手机是小米,所以此教程以小米手机为例.其他手机原理类似 第一步,解锁引导程序 访问小米的官方解锁网站并申请解锁权限. 等 ...

  3. mongodb的有关操作

    mongodb的几种启动方法 https://www.cnblogs.com/LLBFWH/articles/11013791.html MongoDB 之 你得知道MongoDB是个什么鬼 Mong ...

  4. 我心中的ASP.NET Core 新核心对象WebHost(一)

    以本系列文章向Fish 前辈的那篇我心中的ASP.NET 核心对象致敬.(虽然不知道前辈现在在干什么).一晃就6年过去了,那首 郝云 的<回到那一天>怎么唱来着? 时光一晃,你就三十了. ...

  5. 一个简单的winform程序调用webservices

    本文原创,如需转载,请标明源地址,谢谢合作!http://blog.csdn.net/sue_1989/article/details/6597078 本文的编写IDE为VSTS2008和.NET F ...

  6. traceroute学习

    之前只知道ping telnet命令,后面学习了traceroute命令 ping最常用的,看是否可以ping通ip,查看网络是否可达 telnet探测端口是否通,telnet ip port tra ...

  7. 获取input输入值

  8. shortcut to add throws declaration in Intellij Idea

    When a piece of code needs error handling, IntelliJ underlines it with red. Set your pointer on that ...

  9. Go's Declaration Syntax

    Introduction Newcomers to Go wonder why the declaration syntax is different from the tradition estab ...

  10. Sass函数:Sass Maps的函数-map-has-key($map,$key)

    map-has-key($map,$key) 函数将返回一个布尔值.当 $map 中有这个 $key,则函数返回 true,否则返回 false. 前面的示例,当 $key 不在 $map 中时,使用 ...