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+ ...
随机推荐
- 20179209《Linux内核原理与分析》第十一周作业
Nmap配合Metasploit进行端口扫描 1.Nmap扫描器基本使用 1.1简介 Nmap(Network Mapper)最早是Linux下的网络扫描嗅探器.其基本功能有三个: 探测一组主机是否在 ...
- IntelliJ IDEA(2017)安装和破解(转发)
IntelliJ IDEA(2017)安装和破解 IDEA 全称 IntelliJ IDEA,是Java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手 ...
- linux sort按照指定列排序
sort怎样按指定的列排序0000 27189 41925425065f 15 419254250663 7 419254250675 5 419254250691 76 419254250693 2 ...
- MySQL查看和修改字符集的方法
一.查看字符集 1.查看MYSQL数据库服务器和数据库字符集 方法一:show variables like '%character%';方法二:show variables like 'collat ...
- 使用JavaScript定义一个微信小程序插件样例
var wxTimer = new wxTimer({ beginTime: "00:00:20", complete: function () { wx.redirectTo({ ...
- 装箱问题【STL】
7-9 装箱问题(20 分) 假设有N项物品,大小分别为s1.s2.-.si.-.sN,其中si为满足1≤si≤100的整数.要把这些物品装入到容量为100的一批箱 ...
- QCon2016 上海会议汇总(2) - 团队管理
QCon 2016上海日程:http://2016.qconshanghai.com/schedule <当你的团队还支撑不起梦想时> - 链尚网技术合伙人 杨荣伟 Figo讲述了如何训练 ...
- JS中如何获取<Select>中value和text的值
原文地址:JS中如何获取<Select>中value和text的值 html代码: <select id = "city" onchange="chan ...
- 【leetcode刷题笔记】Linked List Cycle
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- nginx配置大全
nginx配置大全