【NOIP 2012提高】国王游戏

2 篇文章 0 订阅

Description
  恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏。首先,他让每个大臣在左、右手上面分别写下一个整数,国王自己也在左、右手上各写一个整数。然后,让这 n 位大臣排成一排,国王站在队伍的最前面。排好队后,所有的大臣都会获得国王奖赏的若干金币,每位大臣获得的金币数分别是:排在该大臣前面的所有人的左手上的数的乘积除以他自己右手上的数,然后向下取整得到的结果。
  国王不希望某一个大臣获得特别多的奖赏,所以他想请你帮他重新安排一下队伍的顺序,使得获得奖赏最多的大臣,所获奖赏尽可能的少。
  注意,国王的位置始终在队伍的最前面。
Input
  第一行包含一个整数 n,表示大臣的人数。
  第二行包含两个整数 a 和 b,之间用一个空格隔开,分别表示国王左手和右手上的整数。
  接下来 n 行,每行包含两个整数 a 和 b,之间用一个空格隔开,分别表示每个大臣左手和右手上的整数。
Output
  输出只有一行,包含一个整数,表示重新排列后的队伍中获奖赏最多的大臣所获得的金币数。
Sample Input
3
1 1
2 3
7 4
4 6
Sample Output
2

【输入输出样例说明】
  按1、2、3号大臣这样排列队伍,获得奖赏最多的大臣所获得金币数为2;
  按1、3、2这样排列队伍,获得奖赏最多的大臣所获得金币数为2;
  按2、1、3这样排列队伍,获得奖赏最多的大臣所获得金币数为2;
  按2、3、1这样排列队伍,获得奖赏最多的大臣所获得金币数为9;
  按3、1、2这样排列队伍,获得奖赏最多的大臣所获得金币数为2;
  按3、2、1这样排列队伍,获得奖赏最多的大臣所获得金币数为9。
  因此,奖赏最多的大臣最少获得 2 个金币,答案输出 2。
Hint
【数据范围】
  对于 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。


写这道题主要是练习高精度
模板不是我写的。。。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;
const int maxn=1010;
struct BIGNUM{
    int len,s[4000];
    BIGNUM(){
        len=1;
        memset(s,0,sizeof(s));
    }
    BIGNUM(const char *num){*this=num;  }
    BIGNUM(int num){*this=num;}
    BIGNUM operator = (const char *num)
    {
        len=strlen(num);
        for (int i=0;i<len;i++)s[i]=num[len-i-1]-'0';
        return *this;
    }
    BIGNUM operator = (const int num)
    {
        char a[4000];
        sprintf(a,"%d",num);
        *this=a;
        return *this;
    }

    bool operator <(const BIGNUM &x)const
    {
        if (len!=x.len)return len<x.len;
        for (int i=len-1;i>=0;i--)
        if (s[i]!=x.s[i]) return s[i]<x.s[i];
        return false;
    }
    bool operator > (const BIGNUM & x)const
    {return x<*this;    }
    bool operator <=(const BIGNUM & x)const
    {return !(x<*this); }
    bool operator >=(const BIGNUM & x)const
    {return !(*this<x); }
    bool operator ==(const BIGNUM & x)const
    {return !(x<*this || *this<x);  }
    bool operator !=(const BIGNUM & x)const
    {return (x<*this || *this<x);   }
    BIGNUM operator * (const BIGNUM &a)
    {
        BIGNUM c;
        c.len=len+a.len;
        for (int i=0;i<len;i++)
        {
            for (int j=0;j<a.len;j++)
            {
                c.s[i+j]+=s[i]*a.s[j];
                c.s[i+j+1]+=c.s[i+j]/10;
                c.s[i+j]%=10;
            }
        }
        if (c.s[c.len-1]==0) c.len--;
        return c;
    }
    BIGNUM operator *=(const BIGNUM &a)
    {
        //*this=*this * a;
        return *this=*this * a;
    }
    BIGNUM operator / (const int &a)
    {
        BIGNUM c;
        int d=0;
        c.len=len;
        for(int i=len-1;i>=0;i--)
        {

            c.s[i]=(d*10+s[i])/a;
            d=(d*10+s[i])%a;
        }
        while(c.s[c.len-1]==0)c.len--;
        return c;
    }
    BIGNUM operator /= (const int &a)
    {
        //*this=*this / a;
        return *this=*this / a;
    }
};
ostream& operator << (ostream &out,const BIGNUM &x)
{
    for (int i=x.len-1;i>=0;i--)cout<<x.s[i];
    return out;
}
istream& operator >> (istream &in,BIGNUM &x)
{
    char num[4000];
    in>>num;
    x=num;
    return in;
}

struct node{
    int a,b;
} s[maxn];
BIGNUM x,y;
BIGNUM ans;
int n;
bool cmp(node x,node y){
    //return x.b*x.a<y.b*y.a;
    return max(1.0/x.b,1.0*x.a/y.b)<max(1.0/y.b,1.0*y.a/x.b);
}
int main()
{
//  freopen("game.in","r",stdin);
//  freopen("game.out","w",stdout);

//  scanf("%d",&n);
//  scanf("%d%d",&x,&y);
    cin>>n;
    cin>>x>>y;
    ans=0;
    for(int i=1;i<=n;i++)
//  scanf("%d%d",&s[i].a,&s[i].b);
    cin>>s[i].a>>s[i].b;
    sort(s+1,s+1+n,cmp);

    for(int i=1;i<=n;i++){
//      ans=max(ans,x/s[i].b);
//      x*=s[i].a;
        if(x/s[i].b>ans)ans=x/s[i].b;
        x*=s[i].a;
    }
    cout<<ans<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值