【poj 3273】Monthly Expense 【二分 注意上下界】
来源:互联网 发布:延边大学知乎 编辑:程序博客网 时间:2024/05/19 12:18
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.
FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called “fajomonths”. Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.
FJ’s goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.
Input
Line 1: Two space-separated integers: N and M
Lines 2.. N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Line 1: The smallest possible monthly limit Farmer John can afford to live with.
Sample Input
7 5
100
400
300
100
500
101
400
Sample Output
500
Hint
If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.
题意 一个数组,将其分成m组,然后找各个组之和最小的min。然后问 这个min的最大值。
代码
#include<cstdio>#include<iostream>#include<algorithm> #include<cmath>using namespace std;#define LL long long const int MAXN =1e5+10;const int MAXM =1e4;const int inf = 0x3f3f3f3f;/*---------------------*/LL N,M;LL arr[MAXN];bool f(int x){ LL cnt=1; LL sum=0;int i,j; for(i=0;i<N;i++){ sum+=arr[i]; if(sum>x) { cnt++;sum=0;i--; } // 不注意上下界的话,这里会陷入死循环。 if(cnt>M) return 0; } return 1;}int main(){ cin>>N>>M; LL le=0;LL ri=-1; for(int i=0;i<N;i++){ scanf("%d",&arr[i]); le=max(le,arr[i]);//最差情况就是每个人一组 ri+=arr[i]; // 最差情况所有人一组 } LL ans=-1; while(le<=ri){ int mid=(le+ri)>>1; if(f(mid)) { ans=mid; ri=mid-1;} else le=mid+1; } printf("%lld\n",ans); return 0;}
- 【poj 3273】Monthly Expense 【二分 注意上下界】
- poj 3273 二分 Monthly Expense
- poj 3273 二分 monthly expense
- poj-3273-Monthly Expense-二分
- POJ 3273 Monthly Expense(二分)
- POJ 3273 Monthly Expense(二分)
- poj 3273 Monthly Expense (二分)
- POJ 3273 Monthly Expense (二分)
- POJ - 3273 Monthly Expense 二分
- POJ 3273 Monthly Expense(二分)
- POJ 3273:Monthly Expense(二分)
- 【POJ 3273】 Monthly Expense (二分)
- poj 3273 Monthly Expense 【二分】
- poj 3273 Monthly Expense (二分)
- POJ 3273 Monthly Expense 二分
- POJ 3273 Monthly Expense(二分)
- 【poj 3273】 Monthly Expense 二分
- POJ - 3273 Monthly Expense(二分)
- LeetCode 371 Sum of Two Integers
- 【poj 3258】River Hopscotch 【二分】
- Kotlin 入门-基本语法
- 单例模式
- 【shell】cut命令及常见用法
- 【poj 3273】Monthly Expense 【二分 注意上下界】
- sql
- 分布式锁原理及实现方式
- Leetcode-14:Longest Common Prefix
- ACM零起点2017-7-24(刷题)
- Python学习02_图片操作
- 从概念、关键问题、兴起到实现需求多角度了解DevOps
- 超级详细SpringMVC基础入门
- 121. Best Time to Buy and Sell Stock