题目1502:最大值最小化(二分答案)

九度OJ

Java

import java.util.Scanner;

public class Main {

   public static int max(int x ,int y){
return x > y ? x : y;
} public static int solve(int tot , int m , int n ,int a[]){
int low = 1 , high = tot;
while(low <= high){
int mid = (low + high) / 2;
int i = 1 , count = 0 , k , flag = 1 ;
while(i <= m){
if(a[i] > mid){
flag = 0; break;
}
int sum = 0;
for(k = i; k <= m; k++){
sum += a[k];
if(sum > mid) break;
}
i = k;
count++;
}
if(flag == 0 || count > n) low = mid + 1;
else if(count == n) high = mid - 1;
else high = mid - 1;
/*
if(count <= n) high = mid - 1;
else low = mid + 1;
*/
}
return max(low , high);
}
public static void main(String[] args){
final int N = 510 , M = 510;
int cas,n,m,tot;
int[] a = new int[M];
Scanner cin = new Scanner(System.in);
cas = cin.nextInt();
while((cas--) != 0){
m = cin.nextInt();
n = cin.nextInt();
tot = 0;
for(int i = 1; i <= m; i++){
a[i] = cin.nextInt();
tot += a[i];
}
int res = solve(tot,m,n,a);
System.out.println(res);
}
}
}

C++

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 510
#define M 510 int a[M],tot; int main()
{
int cas,n,m;
scanf("%d",&cas);
while(cas--)
{
tot = ;
scanf("%d%d",&m,&n);
for(int i=; i<=m; i++)
scanf("%d",&a[i]) , tot += a[i];
int low = , high = tot , mid;
while(low <= high)
{
mid = (low + high) >> ;
int i = , k , count = , ok = ;
while(i<=m)
{
if(a[i] > mid)
{ ok = ; break;}
int sum = ;
for(k=i; k<=m; k++)
{
sum += a[k];
if(sum > mid) break;
}
i = k;
count++;
}
if(!ok || count > n) low = mid + ;
else if(count == n) high = mid - ;
else high = mid - ;
}
printf("%d\n",max(low,high));
}
}

九度OJ 1502 最大值最小化(JAVA)的更多相关文章

  1. 九度OJ 1187:最小年龄的3个职工 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2291 解决:936 题目描述: 职工有职工号,姓名,年龄.输入n个职工的信息,找出3个年龄最小的职工打印出来. 输入: 输入第一行包括1个 ...

  2. 九度OJ 1020:最小长方形 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6019 解决:2849 题目描述:     给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在 ...

  3. 九度OJ 1086:最小花费 (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3960 解决:819 题目描述: 在某条线路上有N个火车站,有三种距离的路程,L1,L2,L3,对应的价格为C1,C2,C3.其对应关系如下 ...

  4. 九度OJ 1102:最小面积子矩阵 (DP、缓存、剪枝)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1666 解决:504 题目描述: 一个N*M的矩阵,找出这个矩阵中所有元素的和不小于K的面积最小的子矩阵(矩阵中元素个数为矩阵面积) 输入: ...

  5. 九度OJ 1209:最小邮票数 (遍历)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2252 解决:741 题目描述: 有若干张邮票,要求从中选取最少的邮票张数凑成一个给定的总值.     如,有1分,3分,3分,3分,4分五 ...

  6. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...

  7. 【九度OJ】题目1191:矩阵最大值 解题报告

    [九度OJ]题目1191:矩阵最大值 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1191 题目描述: 编写一个程序输入一个mXn的 ...

  8. 【九度OJ】题目1187:最小年龄的3个职工 解题报告

    [九度OJ]题目1187:最小年龄的3个职工 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1187 题目描述: 职工有职工号,姓名, ...

  9. 【九度OJ】题目1438:最小公倍数 解题报告

    [九度OJ]题目1438:最小公倍数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1438 题目描述: 给定两个正整数,计 ...

随机推荐

  1. 集合使用copy与mutableCopy的区别

    集合(NSArray,NSSet,NSDictionary等)使用copy与mutableCopy的区别是类似的,下面以NSMutableArray.NSArray 为例子验证如下: NSMutabl ...

  2. atitit. js 跨界面 页面 web cs 传值方法总结

    atitit. js 跨界面 页面 web cs 传值方法总结 #--需求 js #---两个方法:   直接传跟跟间接传递... 1.直接传跟new form(param)    web使用url方 ...

  3. ubuntu上安装mysql 编译安装

    为什么要折腾?首先说明的是ubuntu上安装mysql等软件是非常容易简单的,其简单的程度盖过windows上的安装,一句sudo apt-get install就可以搞定.如果想用最简便的方法安装m ...

  4. SAFS Distilled --- 9 April 2015 to 16 April 2015

    In the org.safs.model, the class Component stores: information of this component's name reference of ...

  5. RFID Exploration and Spoofer a bipolar transistor, a pair of FETs, and a rectifying full-bridge followed by a loading FET

    RFID Exploration Louis Yi, Mary Ruthven, Kevin O'Toole, & Jay Patterson What did you do? We made ...

  6. hadoop安装计

    hadoop安装计 大体上按这个做就好了 http://blog.csdn.net/hitwengqi/article/details/8008203 需要修改hadoop-env.sh export ...

  7. Linear or non-linear shadow maps?

    Quote: Original post by RobMaddisonI understand that, for aliasing mitigation, it might be beneficia ...

  8. StringBuffer&StringBuilder区别详解

    序言 StringBuffer与StringBuilder是java.lang包下被大家熟知的两个类.其异同为:一.长度都是可扩充的:二.StringBuffer是线程安全的,StringBuilde ...

  9. 跨平台web调试代理工具---whistle

    whistle是基于Node实现的跨平台web调试代理工具,支持windows.mac.linux等所有安装了Node的操作系统,可以部署在本地机器.虚拟机或远程服务器,并通过本地网页查看或修改HTT ...

  10. sublime3 配置node build环境

    折腾了很久,原来如此简单 1.package control  安装nodejs 2.修改Nodejs.sublime-settings文件,将nodejs路径修改成自己的 3.sublime tex ...