Poj3122-pie (dichotomy + greedy thought)

Source: Internet
Author: User

One, test instructions:
There are f+1 individuals (including themselves), N-piece pizza pie, give you the radius of each pie, and you have to be fair to give as much pie as you can to everyone.
and everyone gets the pie that must be obtained from the same pie, not pieced together, the extra corners thrown away.
Two, ideas:
1, enter, and find out the maximum volume of pie
2, the dichotomy records the volume of each case, and can be divided to several people,
the greedy thought:
The largest volume that can be divided into n-1, and gradually reduce the volume of each pie
The largest volume of pie that can be obtained by the nearest n person
3, output.
Three, step:
1, input, max stores the largest pie volume
2, dichotomy:
I, exit condition Max-min <= 1e-6;
by the n-1 personal volume slowly reduced, gradually approaching the volume of n Individuals,
The pie volume stored in the last mid is the maximum size that each person has.
Ii,count Records the number of copies of a pie can be divided
III, if the number of copies < number, reduce the volume of each pie, that is, max = mid; mid = (Min+max)/2;
Otherwise, increase the volume of each pie, that is, Min = mid; mid = (Min+max)/2;
3, Output: Note the number of decimal digits of the control output.

Summary of usage of setprecision, fixed and showpoint:
1#include <iostream>2#include <iomanip>3 using namespacestd;4 Const DoublePI =3.14159265359;//This is the shortest pi length, and then the short one is WA.5 Const DoubleESP = 1e-6;//minimum precision limit value set for double dichotomy6 Doublepie[10050]; 7 8 intMain () {9     intT, N, F;//n Indicates the number of copies of PIE at the beginning, and F indicates the number of friendsTenCin>>T; One      while(t--){ ACin>>n>>F; -f++;//number plus their own -         DoubleMax =0.0; the          for(inti =0; I < n; i++){ -Cin>>Pie[i]; -Pie[i] *= pie[i];////Radius squared, do not multiply pi before calculating the volume of pie, in order to improve accuracy and reduce time -             if(max<Pie[i]) +max = Pie[i];//Record the volume of the largest pie -         } +         DoubleMin =0.0;//note Change to 0, error A         Doublemid; at          while(MAX-MIN&GT;ESP) {//The dichotomy end condition of the real number double is different from the binary ending condition of the integer int -Mid = (min + max)/2; -             intCount =0;//record How many copies can be divided according to the different mid sizes -              for(inti =0; I < n; i++){ -Count + = (int) (Pie[i]/mid);//The i-pie is cut according to the size of mid, the maximum number of people (rounding) -             } in             if(Count < f) max = mid;//Mid-Large -             ElseMin = mid;//Mid Low to         //cout<<count<< "-" <<mid<< "";//output, you will find the mystery.  +         } -cout<<fixed<<setprecision (4) <<PI*mid<<endl;//fixed and setprecision use the number of digits after the decimal point the     } *     return 0; $}
View Code

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Poj3122-pie (dichotomy + greedy thought)

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.