二分

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<algorithm>
 6 #define AA struct ss
 7 using namespace std;
 8 
 9 int n;
10 AA {
11     double b,w,d,h;
12 }re[50005];
13 
14 double P(double k)
15 {
16     double ans=0;
17     for(int i=0;i<n;i++)
18     {
19         if(k>=re[i].b)
20         {
21             ans+=min(k-re[i].b,re[i].h)*re[i].w*re[i].d;
22         }
23     }
24     return ans;
25 }
26 
27 int main()
28 {
29     int T;
30     scanf("%d",&T);
31     while(T--)
32     {
33         double r=0,l=0;
34         double sum=0;
35         scanf("%d",&n);
36         for(int i=0;i<n;i++)
37         {
38             scanf("%lf%lf%lf%lf",&re[i].b,&re[i].h,&re[i].w,&re[i].d);
39             sum+=re[i].h*re[i].w*re[i].d;
40             l=max(l,re[i].b+re[i].h);
41         }
42 
43         double v;
44         scanf("%lf",&v);
45         if(sum<v) {printf("OVERFLOW\n");continue;}
46 
47         while(r+0.0001<l)
48         {
49             double mid=(r+l)/2.0;
50             if(P(mid)<v) r=mid;
51             else l=mid;
52         }
53 
54         printf("%.2lf\n",l);
55 
56     }
57 }
View Code
 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 int s[200000],m,n;
 8 
 9 int P(int k)
10 {
11     int fir=0;
12     int c=1;
13     for(int i=1;i<=n;i++)
14         if(s[i]-s[fir]>k) fir=i-1,c++;
15     return c;
16 }
17 
18 int main()
19 {
20     memset(s,0,sizeof(s));
21     while(cin>>n>>m)
22     {
23         int r=1,l=1;
24         for(int i=1;i<=n;i++)
25                 cin>>s[i],r=max(s[i],r),s[i]+=s[i-1];
26 
27         l=s[n];
28 
29         int ans=s[n];
30         while(r<=l)
31         {
32             int mid=(r+l)>>1;
33             int re=P(mid);
34             if(re<=m) l=mid-1;
35             else r=mid+1;
36            // cout<<mid<<' '<<re<<endl;
37         }
38 
39         cout<<l+1<<endl;
40     }
41 }
View Code

 

posted on 2015-03-24 17:15  wsa  阅读(101)  评论(0编辑  收藏  举报

导航