https://vjudge.net/problem/UVA-714

题意:把一个包含m个正整数的序列划分成k个非空的连续子序列,使得每个正整数恰好属于一个序列。设第i个序列的各数之和为S(i),你的任务是让所有S(i)的最大值尽量小。

思路:“最大值尽量小”问题。

区间的范围肯定是所有数中最大的一个至所有数之和,我们可以使用二分法来确定这样的一个值x,x尽量小并且每个区间都不大它。

题目要求的是前面的区间尽量小,那我们就需要贪心一下,从右边的数开始分配区间。需要注意的是,如果当前剩下来的待分配区间的数正好等于还要分配的组数,那么前面的几个数每个数都为一个区间。

 #include<cstring>
#include<string>
#include<iostream>
using namespace std; const int maxn = + ;
int n, m;
long long _max,left,right,_min; int a[maxn];
int ans[]; bool judge(long long mid) //判断能否每组都小于等于mid
{
long long sum = ;
int t = ;
for (int i = n - ; i >= ; i--)
{
if (sum + a[i] > mid)
{
sum = a[i];
t++;
if (t > m) return false; //需要分的组数大于了给定的m组,返回false
}
else
sum += a[i];
}
return true;
} void solve()
{
memset(ans, , sizeof(ans));
::left = _min, ::right = _max;
while (::left <= ::right)
{
long long mid = (::left + ::right) / ;
if (judge(mid))
{
::right = mid - ;
}
else ::left = mid + ;
}
long long sum = ;
int count = ;
for (int i = n - ; i >= ; i--)
{
if (sum + a[i] > ::left)
{
sum = a[i];
ans[i] = ;
count++;
}
else
sum += a[i];
if (m - count == i + ) //如果剩下来的值数量正好等于要划分的组数,那么一个数为一组
{
for (int j = ; j <= i; j++)
ans[j] = ;
break;
}
}
for (int i = ; i < n - ; i++)
{
cout << a[i] << " ";
if (ans[i]) cout << "/ ";
}
cout << a[n - ] << endl; } int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int t;
cin >> t;
while (t--)
{
cin >> n >> m;
_min = ;
_max = ;
for (int i = ; i < n; i++)
{
cin >> a[i];
if (_min < a[i]) _min = a[i];
_max += a[i];
}
solve();
}
return ;
}

UVa 714 抄书(贪心+二分)的更多相关文章

  1. UVA 714 Copying Books 二分

    题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...

  2. UVa 714 Copying Books - 二分答案

    求使最大值最小,可以想到二分答案. 然后再根据题目意思乱搞一下,按要求输出斜杠(这道题觉得就这一个地方难). Code /** * UVa * Problem#12627 * Accepted * T ...

  3. poj 2782 Bin Packing (贪心+二分)

    F - 贪心+ 二分 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description ...

  4. Card Game Cheater(贪心+二分匹配)

    Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分

    Heap Partition Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge A sequence S = { ...

  6. 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

    题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...

  7. Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找

    The link to problem:Problem - D - Codeforces   D. Range and Partition  time limit per test: 2 second ...

  8. UVA - 714 Copying Books (抄书)(二分+贪心)

    题意:把一个包含m个正整数的序列划分成k个(1<=k<=m<=500)非空的连续子序列,使得每个正整数恰好属于一个序列(所有的序列不重叠,且每个正整数都要有所属序列).设第i个序列的 ...

  9. UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)

      Copying Books  Before the invention of book-printing, it was very hard to make a copy of a book. A ...

随机推荐

  1. Centos 集群配置SSH免登陆脚本

    首先编写脚本生成集群服务器列表: hostsList.sh #!/bin/bash preIp="11.11.225." pwd="dyj2017" for i ...

  2. zabbix3.0 centos7 yum 安装与简单配置

    参考文档https://www.zabbix.com/documentation/3.0/start zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.zab ...

  3. [LeetCode] 627. Swap Salary_Easy tag: SQL

    Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m v ...

  4. [LeetCode] 595. Big Countries_Easy tag: SQL

    There is a table World +-----------------+------------+------------+--------------+---------------+ ...

  5. myeclipse自定义JSP模板

  6. cocos代码研究(6)有限时间动作类(FiniteTimeAction)学习笔记

    理论部分 有限时间动作类继承自Action类,被 ActionInstant(即时动作) , 以及 ActionInterval(持续动作) 继承. 即时动作是会立即被执行的动作,被 CallFunc ...

  7. ts实战项目启动中遇到的问题

    项目链接:https://github.com/Jiasm/typescript-example 储备知识须知: sequelize入门篇 : 依照README执行以下操作: npm i brew s ...

  8. yii2 restful api --app接口编程

    转 http://www.yiichina.com/tutorial/1143yii2中restful url访问配置, 登陆接口access-token验证类 [ 2.0 版本 ] 登陆接口acce ...

  9. Java: war包的作用及使用方法,如何解压后缀名为war的文件

    1.什么是war文件? 如果一个Web应用程序的目录和文件非常多,那么将这个Web应用程序部署到另一台机器上,就不是很方便了,我们可以将Web应用程序打包成Web归档(WAR)文件.这个过程和把Jav ...

  10. 调用spark API,监控任务的进度

    我们现在需要监控datapre0这个任务每一次执行的进度,操作如下: 1. 如图所示,打开spark管理页面,找到对应的任务,点击任务名datapre0 2. 进去之后,获得对应IP和端口  3. 访 ...