UVa 714 抄书(贪心+二分)
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 抄书(贪心+二分)的更多相关文章
- UVA 714 Copying Books 二分
题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...
- UVa 714 Copying Books - 二分答案
求使最大值最小,可以想到二分答案. 然后再根据题目意思乱搞一下,按要求输出斜杠(这道题觉得就这一个地方难). Code /** * UVa * Problem#12627 * Accepted * T ...
- poj 2782 Bin Packing (贪心+二分)
F - 贪心+ 二分 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description ...
- Card Game Cheater(贪心+二分匹配)
Card Game Cheater Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 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 = { ...
- 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II
题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...
- 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 ...
- UVA - 714 Copying Books (抄书)(二分+贪心)
题意:把一个包含m个正整数的序列划分成k个(1<=k<=m<=500)非空的连续子序列,使得每个正整数恰好属于一个序列(所有的序列不重叠,且每个正整数都要有所属序列).设第i个序列的 ...
- UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)
Copying Books Before the invention of book-printing, it was very hard to make a copy of a book. A ...
随机推荐
- 多线程下载文件,ftp文件服务器
1: 多线程下载文件 package com.li.multiplyThread; import org.apache.commons.lang3.exception.ExceptionUtils; ...
- web.xml中对post请求的乱码问题解决
直接在web.xml中添加如下代码: <filter> <filter-name>encodingFilter</filter-name> <filter-c ...
- Py-apply用法学习【转载】
转自:https://blog.csdn.net/anshuai_aw1/article/details/82347016 1.Apply Python中apply函数的格式为:apply(func, ...
- PAT 1068 Find More Coins[dp][难]
1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...
- [LeetCode] questions conclustion_Path in Tree
Path in Tree: [LeetCode] 112. Path Sum_Easy tag: DFS input: root, target, return True if exi ...
- OpenCV中对Mat里面depth,dims,channels,step,data,elemSize和数据地址计算的理解 (转)
cv::Matdepth/dims/channels/step/data/elemSizeThe class Mat represents an n-dimensional dense numeric ...
- 012-centos6.5配置静态ip
文件名为:ifcfg-eno16777736 DEVICE=eno16777736TYPE=EthernetONBOOT=yesNM_CONTROLLED=noBOOTPROTO=staticIPAD ...
- fontawesome与amazeUI
fontawesome是很不错的东西呢~~~~ amazeUI也是很不错的东西呢~~~~~ 最近一年里,比较偏爱这两个家伙? 什么?为什么bootstrap?有什么区别? 这个我还真说不太清楚. 用一 ...
- 论文笔记:语音情感识别(三)手工特征+CRNN
一:Emotion Recognition from Human Speech Using Temporal Information and Deep Learning(2018 InterSpeec ...
- linux常用命令:chmod 命令
chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法. Linux系统中的每 ...