箭无虚发
Time Limit: 1000 MS | Memory Limit: 65536 KB |
Total Submissions: 128 | Accepted: 21 |
Description
对于每发一次箭,YZ给他4种成绩(优、良、中、差),JH有三种拉弓以及瞄准时间a ,b,c(a>=b>=c)分别能拿优,良,中等级,如果不拉弓不瞄(直接射),只能拿差(不能中靶)了。
现在JH想知道,在保证自己弹无虚发(不获得差)的情况下,最多能拿多少个优。
如果JH不能做到弹无虚发,输出Oh,my god!
Input
对于每组数据,第一行为一个整数n,表示总共有n支箭。
(0<n<=1000)
之后n行,每行包含三个数字a,b,c,分别表示拿对应等级所需要花的时间。
(0<c<=b<=a<=1000)
之后一个数字t,表示JH有考核总时间为t
(0<=t<=1e6)
Output
Sample Input
1
3 2 1
1
2
3 2 1
3 2 1
4
2
3 2 1
3 2 1
1
Sample Output
1
Oh,my god!
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct J
{
int a, b, c;
}a[1002];
bool cmp(J a, J b)
{
return a.a - a.c < b.a - b.c;
}
int main(int argc, char const *argv[])
{
int T, n, t, s;
scanf("%d", &T);
while (T--)
{
s = 0;
scanf("%d", &n);
for (int i=0; i<n; i++) {
scanf("%d%d%d", &a[i].a, &a[i].b, &a[i].c);
s += a[i].c;
}
scanf("%d", &t); //如果全得“中”的时间(最小花费)>考核时间,就不能做到箭无虚发
if (s > t) printf("Oh,my god!\n");
else {
sort(a, a+n, cmp);//排序用到技巧,(得优的时间-得中的时间)按升序排序
int cnt = 0;
for (int i=0; i<n; i++) {
if (s - a[i].c + a[i].a <= t) {
s = s - a[i].c + a[i].a;//加一个优并减去一个中的时间,贪心策略
cnt++;
}
else break;
}
printf("%d\n", cnt);
}
}
return 0;
}