二分法

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
const int N=200010;
const double pi=acos(-1.0);
double  h,t,ans;
int check(double r)
{
    if(pow(r,pi)>=h*(pi*r*r-r)) return 1;
    else return 0;
}

double rr(double l,double r1)
{     double mid;
    while(r1>l)
    {    mid=(l+(r1-l)/2);
    if(r1-l<1e-8) break;
        if(check(mid))  r1=mid;
        else l=mid;
    }
   return mid;
}
int main()
{
    ios::sync_with_stdio(false);
     while(cin>>t)
     {
         while(t--)
       {cin>>h;
       ans=rr(0,100000);
       printf("%.4lf\n",ans);
       }
     }

}

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int a[100010],N,M,ma,price,ans,total;
int check (int price)
{
    int t=0,sum=0;
    for(int i=0; i<N; i++)
    {
        sum+=a[i];
        if(sum>price)
        {
            t++;
            sum=a[i];
        }
    }
    if(t>M) return 1;
    else
       return 0;
}
int  seek(int l,int r)
{
       int m;
       while(r>=l)
       {
           m=l+(r-l)/2;
           if(check(m)) l=m+1;
           else { ans=m;r=m-1;}
       }
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>N>>M)
    {     total=0;
        ma=0;
        for(int i=0; i<N; i++)
        {
            cin>>a[i];
            if(a[i]>ma) ma=a[i];
            total+=a[i];
        }
        printf("%d\n",seek(ma,total));

    }
    return 0;
}

买古董

#include <bits/stdc++.h>
using namespace std;
int a[100010],N,M,ma,T,price,ans,total;
int check (int price)
{
    int t=0,sum=0;
    for(int i=0; i<N; i++)
    {
        sum+=a[i];
        if(sum>price)
        {
            t++;
            sum=a[i];
        }
    }
    if(t>=M) return 1;//
    else
       return 0;
}
int  seek(int l,int r)
{
       int m;
       while(r>=l)
       {
           m=l+(r-l)/2;
           if(check(m)) l=m+1;
           else { ans=m;r=m-1;}
       }
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>T;
  for(int i=0;i<T;i++)
    {
         cin>>N>>M;
        total=0;
        ma=0;
        for(int i=0; i<N; i++)
        {
            cin>>a[i];
            if(a[i]>ma) ma=a[i];
            total+=a[i];
        }
        printf("%d\n",seek(ma,total));

    }
    return 0;
}

卖古董-DP-二分

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值