[关闭]
@lawk97 2017-04-19T16:28:08.000000Z 字数 5731 阅读 1067

寒假专题训练--二分/三分

专题 二分 三分


VJ地址

A - Strange fuction

HDU - 2899

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <string>
  7. using namespace std;
  8. double fun1(double x,double y)
  9. {
  10. return 6*x*x*x*x*x*x*x+8*x*x*x*x*x*x+7*x*x*x+5*x*x-y*x;
  11. }
  12. double fun2(double x,double y)
  13. {
  14. return 42*x*x*x*x*x*x+48*x*x*x*x*x+21*x*x+10*x-y;
  15. }
  16. int main()
  17. {
  18. int t;
  19. scanf("%d",&t);
  20. while(t--)
  21. {
  22. double y;
  23. scanf("%lf",&y);
  24. double l=0,r=100;
  25. while(r-l>1e-8)
  26. {
  27. double m=l+(r-l)/2;
  28. if(fun2(m,y)<=0)
  29. l=m;
  30. else
  31. r=m;
  32. }
  33. printf("%.4f\n",fun1(l, y));
  34. }
  35. return 0;
  36. }

B - Can you find it?

HDU - 2141

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <string>
  7. using namespace std;
  8. long long a[505],b[505],c[505];
  9. long long ab[250005];
  10. int main()
  11. {
  12. int L,M,N;
  13. int kase=0;
  14. while(~scanf("%d%d%d",&L,&M,&N))
  15. {
  16. for(int i=0;i<L;i++)
  17. scanf("%lld",&a[i]);
  18. for(int i=0;i<M;i++)
  19. scanf("%lld",&b[i]);
  20. for(int i=0;i<N;i++)
  21. scanf("%lld",&c[i]);
  22. for(int i=0;i<L;i++)
  23. for(int j=0;j<M;j++)
  24. ab[i*M+j]=a[i]+b[j];
  25. sort(ab, ab+L*M );
  26. int s;
  27. scanf("%d",&s);
  28. printf("Case %d:\n",++kase);
  29. while(s--)
  30. {
  31. long long key;
  32. scanf("%lld",&key);
  33. int flag=0;
  34. for(int i=0;i<N;i++)
  35. {
  36. int l=0,r=L*M;
  37. long long k=key-c[i];
  38. while(r-l>1)
  39. {
  40. int m=l+(r-l)/2;
  41. if(ab[m]<=k)
  42. l=m;
  43. else
  44. r=m;
  45. }
  46. if(ab[l]==k)
  47. {
  48. flag=1;
  49. break;
  50. }
  51. }
  52. if(flag)
  53. printf("YES\n");
  54. else
  55. printf("NO\n");
  56. }
  57. }
  58. return 0;
  59. }

C - Monthly Expense

POJ - 3273

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <string>
  7. using namespace std;
  8. int n,m,sum,ma;
  9. int a[100005];
  10. int arr(int x)
  11. {
  12. int sx=0,num=1;
  13. for(int i=0;i<n;i++)
  14. {
  15. sx+=a[i];
  16. if(sx>x)
  17. {
  18. sx=a[i];
  19. num++;
  20. }
  21. }
  22. return num;
  23. }
  24. int main()
  25. {
  26. scanf("%d%d",&n,&m);
  27. scanf("%d",&a[0]);
  28. sum=a[0];
  29. ma=a[0];
  30. for(int i=1;i<n;i++)
  31. {
  32. scanf("%d",&a[i]);
  33. sum+=a[i];
  34. if(ma<a[i])
  35. ma=a[i];
  36. }
  37. int l=ma,r=sum;
  38. while(l<r)
  39. {
  40. int mid=l+(r-l)/2;
  41. if(arr(mid)<=m)
  42. r=mid-1;
  43. else
  44. l=mid+1;
  45. }
  46. printf("%d\n",l);
  47. return 0;
  48. }

D - River Hopscotch

POJ - 3258

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. using namespace std;
  6. int main(){
  7. int l,n,m,le,ri;
  8. int dis[50005];
  9. scanf("%d%d%d",&l,&n,&m);
  10. dis[0]=0;
  11. dis[n+1]=l;
  12. le=l;
  13. ri=l;
  14. for(int i=1;i<=n;i++){
  15. scanf("%d",&dis[i]);
  16. }
  17. sort(dis,dis+n+2);//按石头的原始位置排列
  18. for(int i=1;i<=n+1;i++){
  19. if (dis[i]-dis[i-1]<le)
  20. {
  21. le=dis[i]-dis[i-1];
  22. }
  23. }
  24. while(le<ri){
  25. int sum=0,mid=le+(ri-le)/2,count=0;
  26. for (int i = 1; i <= n+1; ++i)//到n+1是为了对最后一段进行处理
  27. {
  28. sum+=dis[i]-dis[i-1];
  29. if (sum<=mid)//why <= ??感觉是因为贪心算法
  30. {
  31. count++;
  32. }else{
  33. sum=0;
  34. }
  35. }
  36. if(count<=m){
  37. le=mid+1;
  38. }else{
  39. ri=mid;
  40. }
  41. }
  42. printf("%d\n",le);
  43. return 0;
  44. }

E - Communication System

[POJ - 1018]

We have received an order from Pizoor Communications Inc. for a
special communication system. The system consists of several devices.
For each device, we are free to choose from several manufacturers.
Same devices from two manufacturers differ in their maximum bandwidths
and prices. By overall bandwidth (B) we mean the minimum of the
bandwidths of the chosen devices in the communication system and the
total price (P) is the sum of the prices of all chosen devices. Our
goal is to choose a manufacturer for each device to maximize B/P.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤
10), the number of test cases, followed by the input data for each
test case. Each test case starts with a line containing a single
integer n (1 ≤ n ≤ 100), the number of devices in the communication
system, followed by n lines in the following format: the i-th line (1
≤ i ≤ n) starts with mi (1 ≤ mi ≤ 100), the number of manufacturers
for the i-th device, followed by mi pairs of positive integers in the
same line, each indicating the bandwidth and the price of the device
respectively, corresponding to a manufacturer. Output Your program
should produce a single line for each test case containing a single
number which is the maximum possible B/P for the test case. Round the
numbers in the output to 3 digits after decimal point.

Sample Input

1 3
3 100 25 150 35 80 25
2 120 80 155 40
2 100 100 120 110

Sample Output

0.649


F - Light Bulb

[ZOJ - 3203]

Compared to wildleopard's wealthiness, his brother mildleopard is
rather poor. His house is narrow and he has only one light bulb in his
house. Every night, he is wandering in his incommodious house,
thinking of how to earn more money. One day, he found that the length
of his shadow was changing from time to time while walking between the
light bulb and the wall of his house. A sudden thought ran through his
mind and he wanted to know the maximum length of his shadow.

Input

The first line of the input contains an integer T (T <= 100),
indicating the number of cases.

Each test case contains three real numbers H, h and D in one line. H
is the height of the light bulb while h is the height of mildleopard.
D is distance between the light bulb and the wall. All numbers are in
range from 10-2 to 103, both inclusive, and H - h >= 10-2.

Output

For each test case, output the maximum length of mildleopard's shadow
in one line, accurate up to three decimal places..

Sample Input

3
2 1 0.5
2 0.5 3
4 3 4

Sample Output

1.000
0.750
4.000


添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注