国王游戏(算法竞赛进阶指南 P40,贪心 + 大数乘除)

一.题目链接:

国王游戏

二.题目大意:

国王和大臣排队,国王始终排在最前面.

每个人的左右手里各有一个数 a,b.

每 i 位大臣获得钱数为  

现在对大臣重新排队,求最小化最大大臣金额.

三.分析:

按照大臣的 a * b 排序,得到的最大大臣金额是最小的.

证明:

这里选用临项交换的方法.

设第 i 位大臣的 a 为 a[i], b 为 b[i]

第 i + 1 位大臣的 a 为 a[i + 1], b 为 b[i + 1]

则,交换前两位大臣的金额分别为  和 

交换后的金额分别为  和 

提出公因项   

得: 和 

 和 

现在来比较交换前后最大的金额

即: 与 

两遍同乘以  

转化为: 与  

若 

又因为 

所以 

反之若 

又因为 

所以 

综上所得:当  时,交换后更优,反之交换前更优.

因此,将大臣按照  排序后,此时即可取得最小的金额.

证毕.

之后遍历一遍,取最大值即可.

ps:这里要手写大数乘除法(顺便留个模板).

四.代码实现:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define lc k * 2
#define rc k * 2 + 1
#define pi acos(-1.0)
#define ll long long int
using namespace std;

const int M = (int)5e3;
const ll mod = 998244353;
const int inf = 0x3f3f3f3f;

struct node
{
    int a, b;
}s[M + 5];

bool cmp(node x, node y)
{
    return x.a * x.b < y.a * y.b;
}

int product_len, quot_len, ans_len;
int product[M + 5], quot[M + 5], ans[M + 5];

void mul(int x)
{
    for(int i = 1; i <= product_len; ++i)
        product[i] *= x;
    product_len += 4;
    for(int i = 1; i <= product_len; ++i)
    {
        product[i + 1] += product[i] / 10;
        product[i] %= 10;
    }
    while(!product[product_len])
        product_len--;
}

void div(int x)
{
    int rem = 0;
    bool flag = 1;
    for(int i = product_len; i; --i)
    {
        rem = rem * 10 + product[i];
        quot[i] = rem / x;
        rem %= x;
        if(quot[i] && flag)
        {
            flag = 0;
            quot_len = i;
        }
    }
}

bool bigger()
{
    if(quot_len != ans_len)
        return quot_len > ans_len;
    for(int i = ans_len; i; --i)
    {
        if(quot[i] != ans[i])
            return quot[i] > ans[i];
    }
    return 0;
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i <= n; ++i)
        scanf("%d %d", &s[i].a, &s[i].b);
    sort(s + 1, s + n + 1, cmp);
    product[++product_len] = 1;
    mul(s[0].a);
    for(int i = 1; i <= n; ++i)
    {
        div(s[i].b);
        if(bigger())
        {
            ans_len = quot_len;
            memcpy(ans, quot, sizeof(quot));
        }
        mul(s[i].a);
    }
    for(int i = ans_len; i; --i)
        printf("%d", ans[i]);
    printf("\n");
    return 0;
}

 

全部评论

相关推荐

看到这图有点绷不住
mountisome:我直接让他多发点,可以不进,但是优惠我是必拿的
点赞 评论 收藏
分享
真tmd的恶心,1.面试开始先说我讲简历讲得不好,要怎样讲怎样讲,先讲背景,再讲技术,然后再讲提升多少多少,一顿说教。2.接着讲项目,我先把背景讲完,开始讲重点,面试官立即打断说讲一下重点,无语。3.接着聊到了项目的对比学习的正样本采样,说我正样本采样是错的,我解释了十几分钟,还是说我错的,我在上一家实习用这个方法能work,并经过市场的检验,并且是顶会论文的复现,再怎么不对也不可能是错的。4.面试官,说都没说面试结束就退出会议,把面试者晾在会议里面,丝毫不尊重面试者难受的点:1.一开始是讲得不好是欣然接受的,毕竟是学习。2.我按照面试官的要求,先讲背景,再讲技术。当我讲完背景再讲技术的时候(甚至已经开始蹦出了几个技术名词),凭什么打断我说讲重点,是不能听出人家重点开始了?这也能理解,每个人都有犯错,我也没放心上。3.我自己做过的项目,我了解得肯定比他多,他这样贬低我做过的项目,说我的工作是错误的,作为一个技术人员,我是完全不能接受的,因此我就和他解释,但无论怎么解释都说我错。凭什么,作为面试官自己不了解相关技术,别人用这个方式work,凭什么还认为这个方法是错的,不接受面试者的解释。4.这个无可厚非,作为面试官,不打招呼就退出会议,把面试者晾着,本身就是有问题。综上所述,我现在不觉得第一第二点也是我的问题,面试官有很大的问题,就是专门恶心人的,总结面试官说教,不尊重面试者,打击面试者,不接受好的面试者,技术一般的守旧固执分子。有这种人部门有这种人怎么发展啊。最后去查了一下,岗位关闭了。也有可能是招到人了来恶心人的,但是也很cs
牛客20646354...:招黑奴啊,算法工程师一天200?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务