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)