「日常训练」Greedy Arkady (CFR476D2C)

题意(Codeforces 965C)

k人分n个糖果,每个糖果至多属于1个人。A某人是第一个拿糖果的。(这点很重要!!) 他x个x个的发糖果,从第一个(他自己)到最后一个,然后再到第一个;多余的糖果丢掉。x不能大于M,单个人最多分糖果轮到他D次。问A某人最多能拿多少糖果。

分析

先说一句话:这条题目极度坑爹。
先考虑题意,可以发现,A某人不论如何,一定会吃到 xdmax x d m a x 个糖果,这里的 dmax d m a x 指该次分发中的最多次数。因此,我们只需要求这个值即可。
然后观察数据规模,会发现n,k,m,d中只有d的规模最小(不到1000)。因此对d进行枚举,然后选择最大x,并计算/更新值即可。
那么这题坑在哪里呢?首先是前面的数据枚举,选错了那就是对 1018 10 18 操作,那就很坑了;第二,看下面的数据:

23925738098196565 23925738098196565 23925738098196565 1000

输出是:

23925738098196565

这个数据坑爹在哪里呢?这个数据 (di1)k ( d i − 1 ) ∗ k 的值恰好是unsigned long long的上限。。。然后+1就喜溢出了,变成0了。。。。然后,然后就RE了啊!!!!然后,如果你用double是不是以为你自己赢定了?除法误差2333(就是会恰好让你 n=(i1)k+1 n = ( i − 1 ) ∗ k + 1 ,然后你如果用浮点数比较会产生浮点数误差)
这个数据估计比赛的时候是一坑一个准,专坑C/C++选手,死不瞑目啊……

代码

为了排错我一个一个的加注释解读啊。。。

#include<bits/stdc++.h>

#define inf 0x3f3f3f3f
#define PB push_back
#define MP make_pair
#define fi first
#define se second
#define lowbit(x) (x&(-x))
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
#define pr(x) cout << #x << " = " << x << " ";
#define prl(x) cout << #x << " = " << x << endl;
#define ZERO(X) memset((X),0,sizeof(X))
#define ALL(X) X.begin(),X.end()
#define SZ(x) (int)x.size()

using namespace std;

typedef pair<double ,double > PI; //specially defined here.
typedef pair<pair<int,int>, int> PII;
typedef pair<pair<pair<int,int>, int>, int> PIII; 
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define quickio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
/*      debug("Precalc: %.3f\n", (double)(clock()) / CLOCKS_PER_SEC);
clock_t z = clock();
        solve();
        //debug("Test: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
*/
template<typename T = int>
inline T read() {
    T val=0, sign=1;
    char ch;
    for (ch=getchar();ch<'0'||ch>'9';ch=getchar())
        if (ch=='-') sign=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())
        val=val*10+ch-'0';
    return sign*val;
}

int main()
{
    ll n,k,m,d;
    cin>>n>>k>>m>>d;
    ll ans=0;
    for(ll i=1;i<=d;++i) // i should be ll as well.
    {
        // no matter which value d or x has, Arakdy always has x*d candies. so we only need to have a larger x :)
        ll maxx;
        ld tmp=(i-1)*k+1;
        // 坑坑坑!
        if(n<tmp) maxx=min(m,ll(0));
        else if(n-1==(i-1)*k) maxx=min(m,ll(1)); //....
        else maxx=min(m,ll(n/tmp));
        tmp=(n/maxx+k-1);
        if(maxx==0 || ll(tmp/k)!=i) // add k-1 means make A always be able to get his candy in d times.
            continue;
        ans=max(ans,maxx*i);
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值