POJ 1064 Cable Master (two-point answer, g++ however, C + + is over)

Source: Internet
Author: User

Topic:

This problem is a bit of a pit, g++, C + + can be too.

Conditions: N data a[], divided into K-segment, the result accuracy requires two decimal places.

Question: What is the maximum length of each paragraph?

Idea: Because the accuracy requirement is two decimal places, I first put all the length a[]*100.

We search for the answer binary, set L to 0,r set to 1000*10000*100+1 (data volume * Maximum size per data * accuracy +1).

So the number we're searching for doesn't have to be processed, so we can calculate the result and divide it by 100.

Code:

#include <iostream>#include<algorithm>#include<map>#include<vector>#include<Set>#include<math.h>#include<queue>#include<assert.h>#include<stdio.h>#include<stdlib.h>using namespaceStd;typedefLong Longll;#defineINF 2147483647//inputintN, K; Doublea[10010]; //returns the largest segment of section KDoublesolve (ll sum) {ll L=0, R = sum+1;//the left and right side of the two pointsll mx =0;//Save Results         while(L <R) {ll mid= (L + r)/2; ll Sum=0;//Sum of segments         for(inti =0; I < n; i++) sum + = A[i]/mid; //Each length mid, can be divided into K-segment        if(Sum >=k) {MX= Max (MX, mid);//Update AnswerL = mid +1; }        Else{R=mid; }    }    return 1.0*mx/ -;}intMain () {CIN>> N >>K; ll Sum=0;  for(inti =0; I < n; i++) {cin>>A[i]; A[i]*= -;//multiply the length by the length ofSum + = A[i];//Sum the lengths} printf ("%.2LF", Solve (sum)); GetChar ();    GetChar (); return 0;}

POJ 1064 Cable Master (two-point answer, g++ however, C + + is over)

Related Article

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.