【NOIP2012提高组】国王游戏 贪心 + 高精度
题目分析
题目答案不具有单调性,所以不可以二分,转而思考贪心。因为无法确定位置,所以考虑如何才能让对于每一个$1 ~ i$使得$i$的答案最大,即$1 ~ i$最后一个最优。若设对于位置$i$,$a[i]$表示左手,$b[i]$表示右手,$S$为其前面所有人的左手之积,那么他的答案就是$\frac{S}{b[i]}$,如果存在在$i$后边的$j$的答案更优, 即$\frac{S * a[i]}{b[j]} > \frac{S * a[j]}{b[i]} => a[i] * b[i] > a[j] * b[j]$,这样只要我们以a[i] * b[i]为关键字从小到大排序,就能保证每个前缀的最后一个都是最优的,只要$o(n)$扫一遍取最大值即可,记得使用高精度。
code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; const int N = ;
int n, a[N], b[N], c[N], ka, kb, kc; inline int read(){
int i = , f = ; char ch = getchar();
for(; (ch < '' || ch > '') && ch != '-'; ch = getchar());
if(ch == '-') f = -, ch = getchar();
for(; ch >= '' && ch <= ''; ch = getchar())
i = (i << ) + (i << ) + (ch - '');
return i * f;
} inline void wr(int x){
if(x < ) putchar('-'), x = -x;
if(x > ) wr(x / );
putchar(x % + '');
} struct bign{
int len, s[];
bign():len(){memset(s, , sizeof s);}
bign(int x):len(){
memset(s, , sizeof s);
while(x){
s[++len] = x % ;
x /= ;
}
if(!len) len = ;
}
inline void clear(){
while(len > && s[len] == ) len--;
}
inline bign operator * (const bign &b) const{
bign ret;
ret.len = len + b.len + ;
for(int i = ; i <= len; i++)
for(int j = ; j <= b.len; j++)
ret.s[i + j - ] += s[i] * b.s[j];
for(int i = ; i <= ret.len; i++)
if(ret.s[i] >= ){
ret.s[i + ] += ret.s[i] / ;
ret.s[i] %= ;
}
ret.clear();
return ret;
}
inline bign operator - (const bign &b) const{
bign ret;
ret.len = len;
for(int i = ; i <= len; i++){
ret.s[i] = ret.s[i] + s[i];
if(i <= b.len) ret.s[i] = ret.s[i] - b.s[i];
if(ret.s[i] < ){
ret.s[i + ]--;
ret.s[i] += ;
}
}
ret.clear();
return ret;
}
bign operator / (int b) {
bign c;
int f = ;
for(int i = len; i >= ; i--){
f = f * + s[i];
while(!(f < b)){
f -= b;
c.s[i]++;
}
}
c.len = len;
c.clear();
return c;
}
inline bool operator > (const bign &b) const{
if(len != b.len) return len > b.len;
for(int i = len; i >= ; i--)
if(s[i] != b.s[i]) return s[i] > b.s[i];
return false;
}
inline bool operator == (const bign &b) const{
if(len != b.len) return false;
for(int i = len; i >= ; i--)
if(s[i] != b.s[i]) return false;
return true;
}
inline bool operator < (const bign &b) const{
if(len != b.len) return len < b.len;
for(int i = len; i >= ; i--)
if(s[i] != b.s[i]) return s[i] < b.s[i];
return false;
}
inline void print(){
for(int i = len; i >= ; i--)
wr(s[i]);
}
}fa, ans, big0, big10; struct node{
int a, b, c;
inline bool operator < (const node &u) const{
if(c != u.c) return c < u.c;
return b > u.b;
}
}data[N]; int main(){
n = read(), ka = read(), kb = read();
for(int i = ; i<= n; i++){
data[i].a = read(), data[i].b = read();
data[i].c = data[i].a * data[i].b;
}
sort(data + , data + n + );
fa = ka;
ans = ;
bign tmpa, t;
for(int i = ; i <= n; i++){
tmpa = data[i].a;
t = fa / data[i].b;
if(t > ans) ans = t;
fa = fa * tmpa;
}
ans.print();
return ;
}
【NOIP2012提高组】国王游戏 贪心 + 高精度的更多相关文章
- [NOIP2012提高组]国王游戏
题目:洛谷P1080.Vijos P1779.codevs1198. 题目大意:国王和每个大臣左.右手各写了一个数.规定每个大臣得到的金币数为他前面所有人左手的数字的乘积除以他自己右手的数(向下取整) ...
- [noip2012]国王游戏<贪心+高精度>
题目链接: https://vijos.org/p/1779 https://www.luogu.org/problem/show?pid=1080 http://codevs.cn/problem/ ...
- P1080 国王游戏 贪心 高精度
题目描述 恰逢 HH国国庆,国王邀请nn 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 nn 位大臣排成一排,国王站在队伍的 ...
- P1080 【NOIP 2012】 国王游戏[贪心+高精度]
题目来源:洛谷 题目描述 恰逢 H国国庆,国王邀请n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国王 ...
- NOIP2012提高组
D1T1.Vigenère密码 模拟 #include<iostream> #include<cstdio> using namespace std; int main() { ...
- GZOJ 1361. 国王游戏【NOIP2012提高组DAY1】
国王游戏[NOIP2012提高组DAY1] Time Limit:1000MS Memory Limit:128000K Description 国王游戏(game.cpp/c/pas) [问题描述] ...
- 刷题总结——疫情控制(NOIP2012提高组)
题目: 题目背景 NOIP2012 提高组 DAY2 试题. 题目描述 H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是首都,也是树中的根节点. H 国的首都 ...
- 洛谷P1080 [NOIP2012提高组D1T2]国王游戏 [2017年5月计划 清北学堂51精英班Day1]
P1080 国王游戏 题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右 手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排 ...
- [NOIP2012] 提高组 洛谷P1080 国王游戏
题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右 手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排 成一排,国王站在队伍 ...
随机推荐
- [React] Create component variations in React with styled-components and "extend"
In this lesson, we extend the styles of a base button component to create multiple variations of but ...
- 基于深度学习的目标检测(object detection)—— rcnn、fast-rcnn、faster-rcnn
模型和方法: 在深度学习求解目标检测问题之前的主流 detection 方法是,DPM(Deformable parts models), 度量与评价: mAP:mean Average Precis ...
- ACdream 1127 Base Station (离线查询+树状数组)
题目链接: http://acdream.info/problem?pid=1127 题目: 移动通信系统中,通信网的建立主要通过基站来完成. 基站可以分为主基站和子基站.子基站和各个移动用户进行连接 ...
- JS实现跑马灯效果(鼠标滑入可暂停,离开继续跑)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- UVA 11800 - Determine the Shape 几何
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- [Angular] Component architecture and Reactive Forms
It it recommeded that when deals with form component, we can create a container component to hold st ...
- FPGA实现UHS的一些资料
对使用FPGA和SD卡进行UHS模式通信的评估: 论文:基于FPGA的SD UHS-II卡控制器设计与实现 设计IP:SD UHS-II Host Controller 供应商: System Lev ...
- C#无符号右移
/// <summary> /// 无符号右移,与JS中的>>>等价 /// </summary> /// & ...
- 8、摄像头驱动_Linux的V4L2架构分析
V4L2架构可以参考 linux-3.4.2\Documentation\video4linux\v4l2-framework.txt V4L2全名为Video For Linux 2,它是针对Li ...
- log4cxx入门篇
log4cxx入门篇 先看官网:http://logging.apache.org/log4cxx/index.html 转载自:http://wenku.baidu.com/view/d88 ...