luogu P1080国王游戏
贪心加高精
传送门:QWQ
先考虑两个人
a0 | b0 | |
---|---|---|
p1 | a1 | b1 |
p2 | a2 | b2 |
那么满足:\(\huge ans1=\max(\frac{a0}{b1} , \frac{a0a1}{b2})\)
a0 | b0 | |
---|---|---|
p2 | a2 | b2 |
p2 | a1 | b1 |
\(\huge ans2=\max(\frac{a0}{b2} , \frac{a0a2}{b1})\)
如果要让ans1<ans2
设 \(k1=\frac{a0}{b1} , k2=\frac{a0a1}{b2} , k3=\frac{a0}{b2} , k4=\frac{a0a2}{b1}\)
则 k1< k4
k2 > k3
如果要让ans1<ans2
那么 k4 > k2
展开,得:\(\frac{a0a2}{b1} > \frac{a0a1}{b2}\)
移项得:\(a1b1<a2b2\)
根据冒泡排序
两个相邻交换一定使答案更差
所以可以根据ab的大小排序(下标排序)
然后就是麻烦的高精了
//自动无视c++14
// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e4+5;
int n;
// The MIT License (MIT)
// Copyright (c) YEAR NAME
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
struct big{
static const int base=10000;
int a[2333];
int len;
big(){memset(a,'\000',sizeof a);len=1;}
int& operator[](int x){return a[x];}
big(int x){*this=x;}
big& operator =(int x){
*this=big();
for(len=0;x;x/=base)a[++len]=x%base;
if(!x)len=1;
return *this;
}
friend big operator + (const big&a,const big& b){
big c;
c.len=max(a.len,b.len);
for(int i=1;i<=c.len;++i){
c[i+1]+=((c[i]+=(a.a[i]+b.a[i]))/base);
c[i]%=base;
}
if(c[c.len+1])c.len++;
return c;
}
friend big operator * (const big&a,const big& b){
big c;
c.len=a.len+b.len-1;
for(int i=1;i<=a.len;++i){
for(int j=1;j<=b.len;++j){
c[i+j-1]+=a.a[i]*b.a[j];
c[i+j]+=c[i+j-1]/base;
c[i+j-1]%=base;
}
}
while(c[c.len+1]){
c[c.len+1]+=c[c.len]/base;
c[c.len]%=base;
c.len++;
}
return c;
}
friend big operator + (const big a,int b){big c(b);return a+c;}
friend big operator * (const big a,int b){big c(b);return a*c;}
friend bool operator < (const big & a,const big&b){
if(a.len==b.len){
for(int i=a.len;i;--i){
if(a.a[i]!=b.a[i])return a.a[i]<b.a[i];
}
}else{
return a.len<b.len;
}
return 0;
}
friend big operator / (const big& a,int x){
big ans;
ans.len=a.len;
int jw=0;
for(int i=ans.len;i;--i){
ans[i]=(jw*base+a.a[i])/x;
((jw*=base)+=a.a[i])%=x;
}
while(!ans[ans.len]&&ans.len)ans.len--;
return ans;
}
void print()const{printf("%d",a[len]);
for(int i=len-1;i;i--){
printf("%04d",a[i]);
}
}
friend ostream& operator << (ostream& out,const big& a){
a.print();
return out;
}
};
int a[maxn],b[maxn];
int bit[maxn];
big f[maxn];
big ans;
big mul(1);
int main(){
cin>>n;
for(int i=0;i<=n;++i){
cin>>a[i]>>b[i];
big x(a[i]),y(b[i]);
f[i]=x*y;
bit[i]=i;
}
sort(bit+1,bit+1+n,[](const int& a,const int& b){
return f[a]<f[b];
});
for(int i=0;i<=n;++i){
if(i)ans=max(ans,mul/b[bit[i]]);
mul=mul*a[bit[i]];
}
ans.print();
return 0;
}
luogu P1080国王游戏的更多相关文章
- Luogu P1080国王游戏(贪心)
国王游戏 题目链接:国王游戏 ps:题目数据说明了要写高精度. 这个题的答案是\(a.l * a.r < b.l * b.r\)按照这个进行排序 题解中大部分只是如何证明排序是: \(a.l * ...
- luogu P1080 国王游戏
题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国王站在队伍的最 ...
- 【题解】洛谷 P1080 国王游戏
目录 题目 思路 \(Code\) 题目 P1080 国王游戏 思路 贪心+高精度.按\(a \times b\)从小到大排序就可以了. \(Code\) #include<bits/stdc+ ...
- P1080 国王游戏 (等待高精度AC)
P1080 国王游戏 题解 贪心策略:按照大臣左右手数字乘积从小到大排序 假设我们已经把大臣排了一个顺序 假定在这个顺序下我们可以保证 得到奖赏最多的大臣所得奖赏最少 那么我们一旦交换任意两个大臣, ...
- 【流水调度问题】【邻项交换对比】【Johnson法则】洛谷P1080国王游戏/P1248加工生产调度/P2123皇后游戏/P1541爬山
前提说明,因为我比较菜,关于理论性的证明大部分是搬来其他大佬的,相应地方有注明. 我自己写的部分换颜色来便于区分. 邻项交换对比是求一定条件下的最优排序的思想(个人理解).这部分最近做了一些题,就一起 ...
- 洛谷—— P1080 国王游戏
https://www.luogu.org/problem/show?pid=1080 题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整 ...
- 洛谷P1080 国王游戏【大数】【贪心】
题目:https://www.luogu.org/problemnew/show/P1080 题意: 一个国王和n个大臣,每个人左右手上都有一个数值. 现在将国王排在队首,将大臣进行排序.每个大臣的值 ...
- [NOIP2012] 提高组 洛谷P1080 国王游戏
题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右 手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排 成一排,国王站在队伍 ...
- [贪心][高精]P1080 国王游戏(整合)
题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国王站在队伍的最 ...
随机推荐
- 《Android Studio实用指南》7.1 AndroidStudio代码检查工具概述
本文节选自<Android Studio实用指南> 作者: 毕小朋 目前本书已上传到百度阅读, 在百度中搜索[Anroid Studio实用指南]便可以找到本书. Android Stud ...
- Qt Signal and Slot
Qt4中的信号槽 Qt4中的信号槽是通过SIGNAL,SLOT两个宏,将参数转换成字符串.Qt编译前,会从源码的头文件中提取由signal和slot声明的信号和槽的函数, 将其组成一张信号和槽对应的字 ...
- 快速搭建Wordpress
1. 下载:ZentaoPMS作为Mysql Apach Php的基础环境: 2. 下载:Wordpress安装包: 3. 将Wordpress解压,放置于ZentaoPMS的Xampp的htdocs ...
- transform详解
1.简介 该算法用于实行容器元素的变换操作.有如下两个使用原型,一个将迭代器区间[first,last)中元素,执行一元函数对象op操作,交换后的结果放在[result,result+(last-fi ...
- 转载 MYSQL性能优化的最佳20+条经验
转自:https://coolshell.cn/articles/1846.html 今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才 ...
- code1154 能量项链
d[l][i]表示:从第i个珠子开始,连续l个珠子合并后释放的最大能量 状态转移方程d[l][i] = d[j][i] + d[l-j][i+j] + w[i]*w[i+j]*w[i+l],j从1到l ...
- Spring.net ObjectWrapper对象的包装(反射机制)有点明晰方便
Company c = new Company(); List<PropertyInfo> ps = c.GetType().GetProperties().ToList(); var p ...
- javascript总结20: 前端必读,浏览器内部工作原理(转)
目录 一.介绍 二.渲染引擎 三.解析与DOM树构建 四.渲染树构建 五.布局 六.绘制 七.动态变化 八.渲染引擎的线程 九.CSS2可视模型 英文原文:How Browsers Work: Beh ...
- HDU 2602 Bone Collector (01背包DP)
题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/S ...
- linux安装x264 ffmpeg
1. 安装yasm 2. 安装x264 3. 安装ffmpeg 安装网上很多例子,以下是我主要参考的两篇博客: http://www.cnblogs.com/lidabo/p/3987378.html ...