BZOJ:1816 [Cqoi2010]扑克牌 (贪心或二分答案)

题面

Auutc8.png

AuuNjS.png

\(solution:\)

这道题难就难在你能否读懂题目的意思,我们将它翻译一下:

现在我有n根竹子(每根竹子有\(c_i\)节,每节竹子高度为1),我可以通过消耗一点法力值使某一根竹子的某两节之间再长出特殊的一节,现在我有m点法力值,我需要在保证同一高度只能有不超过1节特殊的竹节的情况下,使最矮的那根竹子高度尽可能的高

怎么样,会了吧!(最矮的那根竹子高度尽可能的高,这还看不出二分答案白学了)

\(code:\)

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>

#define ll long long
#define db double
#define inf 0x7fffffff
#define min(A,B) (A>B?B:A)
#define rg register int

using namespace std;

struct su{
    int k,v;
}a[55];

int n,m,t,x,ans;

inline int qr(){
    char ch;
    while((ch=getchar())<'0'||ch>'9');
    int res=ch^48;
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+(ch^48);
    return res;
}

inline bool cmp_v(su x,su y){return x.v<y.v;}

int main(){
    freopen("cards.in","r",stdin);
    freopen("cards.out","w",stdout);
    n=qr(),m=qr();
    for(rg i=1;i<=n;++i)
        a[i].k=i,a[i].v=qr();
    sort(a+1,a+n+1,cmp_v);
    ans=t=a[1].v; a[n+1].v=inf;
    for(rg i=1;i<=n;++i){
        x=(i-1)?min(m/i,t/(i-1)):(m/i);
        x=min(x,a[i+1].v-a[i].v);
        ans+=x,m-=x*i,t-=x*(i-1);
    }printf("%d\n",ans);
    return 0;
}

转载于:https://www.cnblogs.com/812-xiao-wen/p/10366998.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值