Poj3273-monthly Expense (minimized maximum)

Source: Internet
Author: User

Topic links :

cid=80117#problem/e ">click here~~

"The main topic"

The farmer JF spends every day in n days, asking for this n talent to be the M group. The number of days per group must be continuous. The sum of the costs of each group should be kept as small as possible, and the maximum value of the sum of the costs of each group should be finally output.

"Problem-solving ideas":

The classic minimization problem, which requires a continuous m-subsequence, a minimum of a subsequence and a maximum value, is the answer to enumerate the minimum values of m that satisfy the condition. therefore two points are searched.

1. Is it possible to divide a sequence into m-sequences of each sequence and not less than mid,
2. By using the current mid value to divide the number of days into groups,
3. Compare the size of mid and T to determine the mid

See the code in detail:

#include <bits/stdc++.h> #include <stdio.h> #include <string.h> #include <iostream> #include     <algorithm>using namespace Std;const int n=1e5+10;int n,m,num[n];bool is_part (int mid) {int t=0,s=0;    t== the current mid value can divide n days into groups (starting with all days as 0 groups) bool Ok=true;            for (int i=0; i<n; i++)//traverse the cost of each day {if (Num[i]>mid) {//Assuming a value is greater than, obviously not in accordance with ok=false;        Break            } if (S+num[i]>mid) {//can no longer add t++ to the current element;    S=num[i]; Exits the previous I day as a set of if (t>m-1) {//t=m.                That is, before the last element has been used the M-bar dividing line ok=false;            Break }} else s+=num[i];//adds the current element to the previous element. So as far as possible to the right to divide, greedy exactly} return OK;    void judge ()//two min find {int ll=0,rr=1e8;        while (LL&LT;RR) {int mid= (LL+RR) >>1;        if (Is_part (mid)) Rr=mid;    else ll=mid+1; } printf ("%d\n", ll);}    int main () {int maxx=0,sum=0;    scanf ("%d%d", &n,&m); for (int i=0; i<n; i++) {scanF ("%d", &num[i]);        Sum+=num[i];    Maxx=max (Maxx,num[i]); } if (m==n| |    m==1) printf ("%d\n", Maxx);    else judge (); return 0;}

Poj3273-monthly Expense (minimized maximum)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.