[Bigint][贪心][模板]vijosp1779国王游戏

2 篇文章 0 订阅
2 篇文章 0 订阅
在国王游戏问题中,需要重新排列大臣的顺序,以使获得最多奖赏的大臣所获金币数尽可能少。国王和大臣们在左右手分别写有整数。通过贪心策略,比较大臣左右手的数的乘积,将乘积较大的大臣排在后面,以降低最大奖赏。样例输入和输出展示了具体的操作和限制条件。
摘要由CSDN通过智能技术生成

P1779国王游戏请 登录 后递交
标签:NOIP提高组2012[显示标签]
描述
恰逢H国国庆,国王邀请n位大臣来玩一个有奖游戏。首先,他让每个大臣在左、右手上面分别写下一个整数,国王自己也在左、右手上各写一个整数。然后,让这n位大臣排成一排,国王站在队伍的最前面。排好队后,所有的大臣都会获得国王奖赏的若干金币,每位大臣获得的金币数分别是:排在该大臣前面的所有人的左手上的数的乘积除以他自己右手上的数,然后向下取整得到的结果。
国王不希望某一个大臣获得特别多的奖赏,所以他想请你帮他重新安排一下队伍的顺序,使得获得奖赏最多的大臣,所获奖赏尽可能的少。注意,国王的位置始终在队伍的最前面。
格式
输入格式

第一行包含一个整数n,表示大臣的人数。
第二行包含两个整数a和b,之间用一个空格隔开,分别表示国王左手和右手上的整数。接下来n行,每行包含两个整数a和b,之间用一个空格隔开,分别表示每个大臣左手和右手上的整数。
输出格式

输出只有一行,包含一个整数,表示重新排列后的队伍中获奖赏最多的大臣所获得的金币数。
样例1
样例输入1[复制]

3
1 1
2 3
7 4
4 6
样例输出1[复制]

2
限制
每个测试点1s
提示
对于20%的数据,有1≤ n≤ 10,0 < a、b < 8;
对于40%的数据,有1≤ n≤20,0 < a、b < 8;
对于60%的数据,有1≤ n≤100;
对于60%的数据,保证答案不超过10^9;
对于100%的数据,有1 ≤ n ≤1,000,0 < a、b < 10000。
来源
Noip2012提高组复赛Day1T2


贪心思路:
相乘贪心
*
*
*
i
j
*
*
如果像这样排队,i,j交换不影响*人,前面乘积为sum
如果i在前面,取max(sum/ri,sum*li/rj)
如果j在前面,取max(sum/rj,sum*lj/ri)
当前情况取最值,也就是在max(sum*li/rj,sum*lj/ri)
同乘,可以发现如果li*ri>lj*rj,
也就是如果i在前面时,乘积更大,也就是要把乘积大的往后放。

#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
#define INF 0x3f3f3f3f
#define CLOCK CLOCKS_PER_SEC
#define cle(x) memset(x,0,sizeof(x))
#define maxcle(x) memset(x,0x3f,sizeof(x))
#define mincle(x) memset(x,-1,sizeof(x))
#define maxx(x1,x2,x3) max(x1,max(x2,x3))
#define minn(x1,x2,x3) min(x1,min(x2,x3))
#define cop(a,x) memcpy(x,a,sizeof(a))
#define FROP "hdu"
#define LL long long
#define smin(x,tmp) x=min(x,tmp)
#define smax(x,tmp) x=max(x,tmp)
using namespace std;
const int N = 1005;
struct bigint
{
    static const int P =1,M=10;
    int w[4050];
    bigint(){cle(w);w[0]=1;}
    void read()
    {
        string s;
        cin>>s;
        int now=1,c1=1,ct=0;
        for(int i = s.size()-1;i>=0;i--)
        {
            w[now]+=(s[i]-'0')*c1;
            c1*=10;
            ct++;
            if(ct==P&&i){c1=1;ct=0;now++;}
        }
        w[0]=now;
    }

    bigint operator * (const int &b)
    {
        bigint a=*this;
        bigint c;
        int &len=c.w[0];
        len=a.w[0]+5;
        for(int i = 1; i <= len;i++)
        {
            c.w[i]+=a.w[i]*b;
            c.w[i+1]=c.w[i]/M;
            c.w[i]%=M;
        }
        while(len>1&&!c.w[len])len--;
        return c;
    }
    bigint operator /(const int &b)
    {
        bigint a=*this;
        bigint c;
        int &len=c.w[0];
        len=a.w[0];
        int tmp=0;
        for(int i = len; i ;i--)
        {
            tmp+=a.w[i];
            if(tmp>=b)
            {
                c.w[i]+=tmp/b;
                tmp%=b;
            }
            tmp*=M;
        }
        while(len>1&&!c.w[len])len--;
        return c;
    }
    bool operator < (const bigint &b)const
    {
        bigint a=*this;
        if(a.w[0]^b.w[0])return a.w[0]<b.w[0];
        for(int i = a.w[0];i;i--)
            if(a.w[i]^b.w[i])return a.w[i]<b.w[i];
        return false;
    }
    void print()
    {
        printf("%d",w[w[0]]);
        for(int i = w[0]-1;i;i--)
            printf("%0*d",P,w[i]);
    }
}L,R;
int n;
struct ii
{
    int l,r;
    ii(int l =0,int r=0):l(l),r(r){ }
    bool operator < (const ii &b)const
    {
        return l*r<b.l*b.r;
    }
}node[N];
int main()
{
    freopen(FROP".in","r",stdin);
    freopen(FROP".out","w",stdout);
    scanf("%d",&n);
    L.read();
    R.read();
    for(int i = 1;i <= n; i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        node[i]=ii(x,y);
    }
    sort(node+1,node+n+1);
    bigint ans,tmp=L;
    for(int i = 1; i <= n; i++)
    {
        bigint x=tmp/node[i].r;
        if(ans<x)ans=x;
        tmp=tmp*node[i].l;
    }
    ans.print();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值