HDU - 1789 Doing Homework again(贪心) ~~~学了一波sort对结构体排序

  题目中因为天数和分数是对应的,所以我们使用一个结构体来存分数和截止如期。

  一开始做这道题的时候,很自然的就想到对天数排序,然后天数一样的分数从大到小排序,最后WA了之后才发现没有做到“舍小取大”的贪心。所以改变一下策略,对分数排序,如果分数一样的话,时间从小到大排序(因为我们的目的就是先做分多的作业,所以分数一样的得放到前几天去做)。

  (具体sort排结构体知识见代码里面,其实也可以写两次for来排序);

  思路:排好序之后,从小到大遍历,每找到一个分数,去寻找对应的天数到第一天中有没有空余的天数(因为要先做分多的),实现寻找空余天数就是从那一天往第一天遍历(((①))),遍历啥子???当然定义一个标记数组来记录你哪一天用来做作业了。如果有空余天就让他在那一天过,如果没有空余天,就是完不成的了,累计加起来,最后输出结果即可。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <queue>
 4
 5 #include <cstdio>
 6 #include <cstdlib>
 7 #include <cmath>
 8 #include <cstring>
 9 using namespace std;
10
11 struct ss {
12     int time, p;
13 }t[1010];
14
15 int cmp (const ss a, const ss b) {
16     // if (a.p>b.p) {
17         // return 1;
18     // } else if (a.p==b.p&&a.time<b.time) {
19         // return 1;
20     // } else {
21         // return 0;
22     // }
23
24     // 两种方法都可以
25
26     if (a.p != b.p) {
27         return a.p > b.p;
28     } else {
29         return a.time < b.time;
30     }
31 }
32
33 bool used[1010];
34
35 int main() {
36     int T, num;
37     cin >> T;
38     while (T--) {
39         int res = 0;
40         cin >> num;
41         for (int i = 0; i < num; ++i) {
42             scanf ("%d", &t[i].time);
43         }
44         for (int i = 0; i < num; ++i) {
45             scanf ("%d", &t[i].p);
46         }
47         sort (t, t+num, cmp);
48         memset (used, false, sizeof(used));
49
50         int j;
51         for (int i = 0; i < num; ++i) {
52             for (j = t[i].time; j > 0; --j) {
53                 if (!used[j]) {
54                     used[j] = true;
55                     break;
56                 }
57             }
58             if (j == 0) {
59                 res += t[i].p;
60             }
61         }
62         cout << res << endl;
63     }
64     return 0;
65 }

思考:①处为什么不可以从第一天遍历?

答:如果从第一天遍历的话,那么很可能把只有一天的交作业时间的科目占用了,后面有空余的天,也是达不到扣最少分的结果的。

时间: 2024-10-28 10:29:35

HDU - 1789 Doing Homework again(贪心) ~~~学了一波sort对结构体排序的相关文章

hdu 1789 Doing HomeWork Again (贪心算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 /*Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7903 Accepted Submission(s): 4680 Problem Description Ignatius has just c

HDU 1789 - Doing Homework again - [贪心+优先队列]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem DescriptionIgnatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Eve

HDU 1789 Doing Homework again(贪心)

Doing Homework again Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadlin

HDU 1789 Doing Homework again

在我上一篇说到的,就是这个,贪心的做法,对比一下就能发现,另一个的扣分会累加而且最后一定是把所有的作业都做了,而这个扣分是一次性的,所以应该是舍弃扣分小的,所以结构体排序后,往前选择一个损失最小的方案直接交换就可以了. #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; struct HomeWork { int de

HDOJ 1009. Fat Mouse&#39; Trade 贪心 结构体排序

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 56784    Accepted Submission(s): 19009 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

HDU 1084 [What Is Your Grade?] 结构体排序

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1084 题目大意:共5道题,N个学生.做出5道满分,0道50分.做出1-4道的同学,若在前50%(向下取整),则获得95.85.75.65,若在后50%则获得90.80.70.60. 关键思想:结构体排序 //结构体排序,考虑边界 #include <iostream> #include <algorithm> #include <cmath> #include <me

HDU 2409 Team Arrangement (结构体排序)

题目链接:HDU 2409 Team Arrangement 题意:给出22个人(编号,名字,踢的位置,在队里的时间),让我们选DD-MM-SS的阵型+一个守门员.在选出队长(时间在最久的就是队长,时间相同编号大为队长),选人的顺序是从编号小的开始. 结构体排序就好了,注意出输出是按队长,D,M,S的顺序,选队长记录队长的编号(而不是下标,我的代码之后还要排序). AC代码: #include<stdio.h> #include<string.h> #include<algo

hdu 5702 Solving Order(结构体排序 水题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5702 Solving Order Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 184    Accepted Submission(s): 135 Problem Description Welcome to HDU to take p

hdu 1070(结构体排序)

Milk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13639    Accepted Submission(s): 3328 Problem Description Ignatius drinks milk everyday, now he is in the supermarket and he wants to choose