vijos1779国王游戏
描述
恰逢H国国庆,国王邀请n位大臣来玩一个有奖游戏。首先,他让每个大臣在左、右手上面分别写下一个整数,国王自己也在左、右手上各写一个整数。然后,让这n位大臣排成一排,国王站在队伍的最前面。排好队后,所有的大臣都会获得国王奖赏的若干金币,每位大臣获得的金币数分别是:排在该大臣前面的所有人的左手上的数的乘积除以他自己右手上的数,然后向下取整得到的结果。
国王不希望某一个大臣获得特别多的奖赏,所以他想请你帮他重新安排一下队伍的顺序,使得获得奖赏最多的大臣,所获奖赏尽可能的少。注意,国王的位置始终在队伍的最前面。
格式
输入格式
第一行包含一个整数n,表示大臣的人数。
第二行包含两个整数a和b,之间用一个空格隔开,分别表示国王左手和右手上的整数。接下来n行,每行包含两个整数a和b,之间用一个空格隔开,分别表示每个大臣左手和右手上的整数。
输出格式
输出只有一行,包含一个整数,表示重新排列后的队伍中获奖赏最多的大臣所获得的金币数。
输入:
3
1 1
2 3
7 4
4 6
输出:
2
思路:将大臣按左右手整数之积从小到大排序,为解决溢出问题,采用C++大数。
#include <iostream>
#include <iomanip>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAXN=;
const int BASE=;
const int LEN=;
struct BigInt{
int e[MAXN],len;
BigInt()
{
memset(e,,sizeof(e));
len=;
}
void set(int x)
{
memset(e,,sizeof(e));
len=;
while(x>)
{
e[len++]=x%BASE;
x/=BASE;
}
}
bool operator>(const BigInt &b)
{
if(len>b.len)
{
return true;
}
else if(len==b.len)
{
for(int i=len-;i>=;i--)
{
if(e[i]>b.e[i]) return true;
else if(e[i]<b.e[i]) return false;
else ;
}
return false;
}
else
{
return false;
}
}
BigInt operator*(const BigInt &b)
{
BigInt res;
for(int i=;i<len;i++)
{
int up=;
for(int j=;j<b.len;j++)
{
int z=e[i]*b.e[j]+up+res.e[i+j];
res.e[i+j]=z%BASE;
up=z/BASE;
}
if(up!=) res.e[i+b.len]=up;
}
res.len=len+b.len;
while(res.len>&&res.e[res.len-]==) res.len--;
return res;
}
BigInt operator/(const int &b)
{
BigInt res=*this;
int carry=;
for(int i=len-;i>=;i--)
{
res.e[i]+=carry*BASE;
carry=res.e[i]%b;
res.e[i]/=b;
}
while(res.len>&&res.e[res.len-]==) res.len--;
return res;
}
friend ostream &operator<<(ostream &out,const BigInt &b)
{
out<<b.e[b.len-];
for(int i=b.len-;i>=;i--)
{
out<<setw(LEN)<<setfill('')<<b.e[i];
}
out<<endl;
return out;
}
};
struct Node{
int x,y;
}mon[MAXN];
bool comp(const Node &a,const Node &b)
{
return a.x*a.y < b.x*b.y;
}
int n,kl,kr;
int main()
{
cin>>n;
cin>>kl>>kr;
for(int i=;i<n;i++)
{
cin>>mon[i].x>>mon[i].y;
}
sort(mon,mon+n,comp);
BigInt res;
res.set();
BigInt mul;
mul.set(kl);
for(int i=;i<n;i++)
{
BigInt tmp;
tmp=mul/mon[i].y;
if(tmp>res) res=tmp;
BigInt b;
b.set(mon[i].x);
mul=mul*b;
}
cout<<res<<endl;
return ;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct BigInt{
static const int SIZE = ;
int e[SIZE], len;
BigInt()
{
len = ;
memset(e, , sizeof(e));
}
void reset(int x)
{
while(x > )
{
e[len++] = x % SIZE;
x /= SIZE;
}
while(len > && e[len-] == ) len--;
}
bool operator>(const BigInt& b)
{
if(len > b.len) return true;
else if(len < b.len) return false;
else
{
for(int i = len - ; i >= ; i--)
{
if(e[i] == b.e[i]) continue;
else if(e[i] > b.e[i]) return true;
else return false;
}
}
return false;
}
BigInt operator*(const BigInt& b)
{
BigInt ret;
for(int i = ; i < len; i++)
{
int up = ;
for(int j = ; j < b.len; j++)
{
int z = e[i] * b.e[j] + ret.e[i+j] + up;
ret.e[i+j] = z % SIZE;
up = z / SIZE;
}
if(up != )
{
ret.e[i + b.len] = up;
}
}
ret.len = len + b.len;
while(ret.len > && ret.e[ret.len-] == ) ret.len--;
return ret;
}
BigInt operator/(int b)
{
BigInt ret = *this;
int carry = ;
for(int i = len - ; i >=; i--)
{
ret.e[i] += carry * SIZE;
carry = ret.e[i] % b;
ret.e[i] /= b;
}
while(ret.len > && ret.e[ret.len - ] == ) ret.len--;
return ret;
}
void print()
{
printf("%d", e[len-]);
for(int i = len - ; i >= ; i--)
{
printf("%04d", e[i]);
}
printf("\n");
}
};
const int MAXN = ;
struct Node{
int x, y;
}nod[MAXN];
int n, kl, kr;
bool comp(Node a, Node b)
{
return a.x * a.y < b.x * b.y;
}
int main()
{
scanf("%d %d %d", &n, &kl, &kr);
for(int i = ; i < n; i++)
{
scanf("%d %d", &nod[i].x, &nod[i].y);
}
sort(nod, nod + n, comp);
BigInt res, mul;
res.reset(kl / nod[].y);
mul.reset(kl);
for(int i = ; i < n; i++)
{
BigInt b;
b.reset(nod[i-].x);
mul = mul * b;
BigInt score = mul / nod[i].y;
if(score > res)
{
res = score;
}
}
res.print();
return ;
}
vijos1779国王游戏的更多相关文章
- NOIP2012 国王游戏
2国王游戏 (game.cpp/c/pas) [问题描述] 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数 ...
- 【NOIP 2012 国王游戏】 贪心+高精度
题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右 手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排 成一排,国王站在队伍 ...
- Codevs 1198 国王游戏 2012年NOIP全国联赛提高组
1198 国王游戏 2012年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 恰逢 H 国国庆,国王邀 ...
- Luogu 1080 【NOIP2012】国王游戏 (贪心,高精度)
Luogu 1080 [NOIP2012]国王游戏 (贪心,高精度) Description 恰逢H国国庆,国王邀请n位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己 ...
- NOIP国王游戏
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- AC日记——国王游戏 洛谷 P1080
国王游戏 思路: 贪心+高精: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1005 struct Dat ...
- Luogu P1080国王游戏(贪心)
国王游戏 题目链接:国王游戏 ps:题目数据说明了要写高精度. 这个题的答案是\(a.l * a.r < b.l * b.r\)按照这个进行排序 题解中大部分只是如何证明排序是: \(a.l * ...
- 国王游戏 2012年NOIP全国联赛提高组(贪心+高精)
P1080 国王游戏 题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成 ...
- 【题解】洛谷 P1080 国王游戏
目录 题目 思路 \(Code\) 题目 P1080 国王游戏 思路 贪心+高精度.按\(a \times b\)从小到大排序就可以了. \(Code\) #include<bits/stdc+ ...
随机推荐
- zabbix_get 命令介绍
zabbix_get 是 zabbix 服务端的一个命令,用于检测 agent 端的配置是否正确,可以很方便地知道 key 是否能正常获取到数据,在测试自定义监控的时候特别有用 [root@crazy ...
- [luogu3601]签到题
[luogu3601]签到题 luogu 求\[\sum_{i=l}^ri-\phi(i)\] 一个朴素的想法是枚举l~r,根号求\(\phi\),显然这样是\((r-l)\sqrt r\),时间无法 ...
- php中生成随机密码的自定义函数代码
这篇文章主要分享下php中生成随机密码的方法,原理就是把一些要生成的字符预置一个的字符串包括数字拼音之类的以及一些特殊字符,这样我们再随机取字符组成我们想要的随机密码了 代码一: 生成一个随机密码的函 ...
- zipfile.BadZipFile: File is not a zip file
zipfile.BadZipFile: File is not a zip file 出现这个问题一般是文件损坏的可能性比较大
- Example 2 - contour plots
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" begin cdf_file = addfile("$N ...
- python用特殊方法定制类(不全)
定义在class中不需要直接调用,python的某些函数或操作符会自动的调用对应的特殊方法. 1.python中 __str__和__repr__ __str__()用于显示给用户,而__repr__ ...
- Android系统Recovery模式的工作原理【转】
本文转载自:http://blog.csdn.net/mu0206mu/article/details/7464987 在使用update.zip包升级时怎样从主系统(main system)重启进 ...
- HDU OJ 2159 FATE
#include <stdio.h> #include <string.h> ][] ; ]; //»ñµÃ¾Ñé ]; //»¨·ÑµÄÈÌÄÍ¶È int main() ...
- 算法(Algorithms)第4版 练习 1.3.37
package com.qiusongde.creative; import com.qiusongde.Queue; import edu.princeton.cs.algs4.StdOut; pu ...
- tkinter模块中常用的参数
以下内容来自于:http://www.cnblogs.com/aland-1415/p/6849193.html(个别内容掺入了自己的重新整理) cnf={}与**kw: cnf={}这是一个默认参数 ...