码迷,mamicode.com
首页 > 其他好文 > 详细

[NOIp 2012]国王游戏

时间:2017-10-18 00:07:43      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:clu   +=   desc   结果   als   输入输出   输入   printf   注意   

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

HINT

【输入输出样例说明】

按 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。

【数据范围】

对于 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。

题解

贪心部分:

对于第 $i$个大臣和第 $j$ 个大臣:

如果第 $i$ 个大臣放第 $j$ 个大臣前面对答案的贡献小些,那么第 $i$ 个大臣就放第 $j$ 个大臣前面

所以就是使 $a[i].x/a[j].y<a[j].x/a[i].y$

所以就是$a[i].x*a[i].y<a[j].x*a[j].y$

乘法部分相当于高精度乘低精度

除法部分相当于高精度除低精度

代码打得想学Python,但差不多算一遍过吧...

 1 //It is made by Awson on 2017.10.17
 2 #include <set>
 3 #include <map>
 4 #include <cmath>
 5 #include <ctime>
 6 #include <stack>
 7 #include <queue>
 8 #include <vector>
 9 #include <string>
10 #include <cstdio>
11 #include <cstdlib>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 #define LL long long
16 #define Min(a, b) ((a) < (b) ? (a) : (b))
17 #define Max(a, b) ((a) > (b) ? (a) : (b))
18 #define Abs(x) ((x) < 0 ? (-(x)) : (x))
19 using namespace std;
20 const int N = 1000;
21 
22 int n, a, b;
23 struct tt {
24     int x, y;
25     bool operator < (const tt &b) const{
26         return x*y < b.x*b.y;
27     }
28 }w[N+5];
29 struct BIG_NUM {
30     int a[23333], lenth;
31     BIG_NUM () {
32         memset(a, 0, sizeof(a)); a[1] = lenth = 1;
33     }
34     BIG_NUM (int* _a, int _lenth) {
35         for (int i = 1; i <= _lenth; i++) a[i] = _a[i]; lenth = _lenth;
36     }
37     BIG_NUM operator * (const int &b) const{
38         BIG_NUM ans; ans.a[1] = 0, ans.lenth = lenth;
39         for (int i = 1; i <= lenth; i++) ans.a[i] = a[i]*b;
40         for (int i = 1; i <= ans.lenth; i++) ans.a[i+1] += ans.a[i]/10, ans.a[i] %= 10;
41         while (ans.a[ans.lenth+1]) ans.lenth++, ans.a[ans.lenth+1] += ans.a[ans.lenth]/10, ans.a[ans.lenth] %= 10;
42         return ans;
43     }
44     BIG_NUM operator / (const int &b) const{
45         BIG_NUM ans; ans.a[1] = 0, ans.lenth = lenth; int sum = 0;
46         while (sum < b) sum = sum*10+a[ans.lenth--];
47         for (int i = ans.lenth; i >= 1; i--)
48             ans.a[i+1] = sum/b, sum = (sum%b)*10+a[i];
49         ans.a[1] = sum/b, ans.lenth++;
50         return ans;
51     }
52     bool operator < (const BIG_NUM &b) const{
53         if (lenth < b.lenth) return true;
54         else if (lenth > b.lenth) return false;
55         for (int i = lenth; i >= 1; i--)
56             if (a[i] < b.a[i]) return true;
57             else if (a[i] > b.a[i]) return false;
58         return false;
59     }
60 }tmp, ans;
61 
62 void work() {
63     scanf("%d%d%d", &n, &a, &b);
64     for (int i = 1; i <= n; i++) scanf("%d%d", &w[i].x, &w[i].y);
65     sort(w+1, w+1+n);
66     tmp = tmp*a;
67     for (int i = 1; i <= n; i++) {
68         BIG_NUM tmpp = tmp/w[i].y;
69         if (ans < tmpp) ans = tmpp;
70         tmp = tmp*w[i].x;
71     }
72     for (int i = ans.lenth; i >= 1; i--) printf("%d", ans.a[i]);
73 }
74 int main() {
75     work();
76     return 0;
77 }

 

[NOIp 2012]国王游戏

标签:clu   +=   desc   结果   als   输入输出   输入   printf   注意   

原文地址:http://www.cnblogs.com/NaVi-Awson/p/7684355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!