D - WE POJ - 3273 (二分法)

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 ≤ MN) 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组,然后让我们去求分完组后所有组的最小值,如果一组有好几个量,那就要把他们加到一起。但是在选择上面必须要选择连续的,不能跳着选择。。。我都是这一点没看到才很wa
这个样子看起来只能用二分法了,二分法要注意选择的区间,怎么用二分法,怎么选择区间,
在下面代码中,是我写的代码必须要这样选择区间,他的底限low不能小,具体看代码
代码如下:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 const int inf=0x3f3f3f3f3;
 8 int v[100005],n,m,w[100005];
 9 int searchs(int low,int high)
10 {
11     int maxn=0;
12     while(low<=high)
13     {
14         // printf("**%d**%d\n",low,high);
15         int mid=(low+high)>>1;
16 //        int temp=1;
17 //        w[1]=v[1];
18 //        for(int i=2;i<=n;++i)
19 //        {
20 //            w[i]=v[i];
21 //            if(w[i]+w[i-1]<=mid)
22 //            {
23 //                w[i]+=w[i-1];
24 //            }
25 //            else
26 //            {
27 //                ++temp;
28 //            }
29 //        }
30         int temp = 1,sum = 0;
31         for(int i=1; i<=n; i++)
32         {
33             if(sum+v[i] <= mid)
34             {
35                 sum += v[i];
36             }
37             else
38             {
39                 temp++;
40                 sum = v[i];
41             }
42         }
43         if(temp==m)
44         {
45             // printf("%d*%d\n",mid,temp);
46             maxn=mid;
47             high=mid-1;
48         }
49         else if(temp>m)
50         {
51             // printf("%d**%d\n",mid,temp);
52             low=mid+1;
53         }
54         else high=mid-1,maxn=mid;//printf("%d***%d\n",mid,temp);;
55     }
56     return maxn;
57 }
58 int main()
59 {
60 
61     scanf("%d%d",&n,&m);
62     int a=0,b=0;
63     for(int i=1; i<=n; ++i)
64     {
65         scanf("%d",&v[i]);
66         a+=v[i];
67         b=max(b,v[i]);   //这里可不能在b中存输入数据的最小值,因为题目是要让
68     }                    //我们求分组后的最大值,你这样可能求出来的值小于输入的最大值
69     int mid=500,temp=0;  //因为当mid<v[i]的时候temp仅仅加了一,但是这并不符合题意
70     int pow=searchs(b,a);
71     printf("%d\n",pow);
72 }
View Code

 

 
 
 
 
posted @ 2019-04-12 13:29  kongbursi  阅读(184)  评论(0编辑  收藏  举报