九度oj 题目1416:猴子吃坚果

题目描述:

动物园的猴子吃坚果的顺序都是按强壮程度来定的,最强壮的吃完才能轮到下一个,现在我们给出各个猴子的名字,强壮程度,吃饱的量,然后查询对应的猴子必须要扔多少坚果才可以轮到。

输入:

输入有多组,对于每组输入:

输入的第一行包括两个整数n(1<=n<=10000),代表动物园中猴子的个数,以及m(1<=m<=10000),代表我们要查询的次数。

        接下来的n行,每行包括一个字符串(代表猴子的名字,长度1<=len<=100),一个整数a(1<=a<=10000),代表猴子的强壮程度,一个整数b(1<=b<=10000),代表猴子吃饱所需要的食物量。

输出:

         对于每次查询,输出最少需要多少食物,被查询的这只猴子才能吃到食物。

样例输入:
3 2
monkey_a 4 2
monkey_b 3 4
monkey_c 5 3
monkey_a
monkey_c
4 2
monkey_a 4 2
monkey_b 3 4
monkey_c 5 3
monkey_d 5 3
monkey_a
monkey_c

样例输出:
4
1
7
1


这题没啥,就是个排序
代码如下
 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 struct Monkey
 7 {
 8     char name[102];
 9     int strong, cost;
10 };
11 Monkey mon[10002];
12 
13 int cmp(Monkey a, Monkey b) {
14     if(a.strong == b.strong) {
15         return strcmp(a.name, b.name);
16     }
17     else {
18         return a.strong > b.strong;
19     }
20 }
21 
22 int n,m;
23 int dp[10002];
24 int main(int argc, char const *argv[])
25 {
26     freopen("input.txt","r",stdin);
27     while(scanf("%d %d",&n,&m) != EOF) {
28         for(int i = 0; i < n; i++) {
29             scanf("%s %d %d",mon[i].name, mon[i].strong, mon[i].cost);
30         }
31         sort(mon, mon+n, cmp);
32         memset(dp, 0, sizeof(dp));
33         dp[0] = 1;
34         for(int i = 1; i < n; i++) {
35             dp[i] = dp[i-1] + mon[i-1].cost;
36         }
37         while(m--) {
38             char tmp[102];
39             scanf("%s",tmp);
40             int ans = 0;
41             for(int i = 0; i < n; i++) {
42                 if(strcmp(tmp,mon[i].name) == 0) {
43                     ans = dp[i];
44                     break;
45                 }
46             }
47             printf("%d
",ans);
48         }
49     }
50     return 0;
51 }
原文地址:https://www.cnblogs.com/jasonJie/p/5837120.html