题目
描述 Description
chdy 想参加赛车比赛,他的赛车质量是M (1 <= M <= 1,000) ,马力是F (1 <= F <= 1,000,000). 为了提升赛车性能,他可以到加工店添加一些零件。加工店有N(1 <= N <= 10,000)种零件,编号1…N, 每种零件只有一件。
零件P_i 能增加的马力是F_i (1 <= F_i <= 1,000,000) ,它的质量是
M_i (1 <= M_i <= 1,000). 根据牛顿第二定理F =MA,( F 是力, M是质量, A 是加速度). chdy想让他的车的加速度最大 (如果有多种方案,让赛车的质量最小),他应该配置哪些零件呢?
例如:开始赛车的F=1500 ,M=100.有4种零件:
i F_i M_i
1 250 25
2 150 9
3 120 5
4 200 8
假如只配置零件2, 那么加速度将会是:(1500+150)/(100+9) = 1650/109 = 15.13761.
下面的表格用4位二进制数表示配置或不配置零件,得到各种各样的加速度:
状态 F M F/M
0000 1500 100 15.0000
0001 1700 108 15.7407
0010 1620 105 15.4286
0011 1820 113 16.1062
0100 1650 109 15.1376
0101 1850 117 15.8120
0110 1770 114 15.5263
0111 1970 122 16.1475 <-- highest F/M
1000 1750 125 14.0000
1001 1950 133 14.6617
1010 1870 130 14.3846
1011 2070 138 15.0000
1100 1900 134 14.1791
1101 2100 142 14.7887
1110 2020 139 14.5324
1111 2220 147 15.1020
因此,chdy应该配置零件2, 3, 4.
输入格式 Input Format
* 第 1行:三个整数: F, M, N
*第 2…N+1行:第i行有两个整数: F_i 、 M_i
输出格式 Output Format
* 第1…P行: P 是chdy要配置的零件的个数。如果chdy不需要配置,则输出NONE. 否则从小到大输出chdy配置的零件的下标。
注意:答案是唯一的。
样例输入 Sample Input
1500 100 4
250 25
150 9
120 5
200 8
样例输出 Sample Output
2
3
4
时间限制 Time Limitation
1s
注释 Hint
1s
来源 Source
usaco 2010 mar silver sboost
题解
简单的贪心, 可能会用到一点01分数规划的知识
不会01分数规划的点这里
那么, 回到我们的题解, 这道题实际上我们只需将每次新加上零件后的加速度和之前的加速度大小来判断是否该加这个零件。
code
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;inline int read() {int s = 0, w = 1;char ch = getchar();while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }while (isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }return s * w;
}int f, m, n;
struct node {int ff;int mm;int id;int vis;
}c[maxn];
double ans, flag;inline bool cmp(node a, node b) {return (double) a.ff / a.mm > (double) b.ff / b.mm;
}inline bool cmp1(node a, node b) {return a.id < b.id;
}int main() {ans = 0; flag = false;f = read(), m = read(), n = read();for (register int i = 1; i <= n; ++i) {c[i].ff = read();c[i].mm = read();c[i].id = i; }sort(c + 1, c + n + 1, cmp);for (int i = 1; i <= n; ++i) {if ((double)(f + c[i].ff) / (m + c[i].mm) > (double) f / m) {f += c[i].ff;m += c[i].mm;c[i].vis = 1;flag = true;}}if (!flag) {printf("NONE\n");return 0;}else {sort(c + 1, c + n + 1, cmp1);for (int i = 1; i <= n; ++i) {if (c[i].vis) {printf("%d\n", c[i].id);}}}return 0;
}
记录
#01: Accepted (0ms, 3312KB)
#02: Accepted (11ms, 3312KB)
#03: Accepted (15ms, 3312KB)
#04: Accepted (11ms, 3312KB)
#05: Accepted (11ms, 3312KB)
#06: Accepted (15ms, 3312KB)
#07: Accepted (15ms, 3312KB)
#08: Accepted (15ms, 3312KB)
#09: Accepted (15ms, 3312KB)
#10: Accepted (15ms, 3312KB)
Accepted / 100 / 128ms / 33120KB