CSUOJ 1603 Scheduling the final examination

1603: Scheduling the final examination

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 49  Solved: 15

Description

For the most of the university students,what they most want is that they can obtain 60 points from the final examination of every subject. Now, final examination is coming. As an excellent programmer,you are asked for help. The full mark is 100, and it is need greater than or equal to 60 to pass subjects. Given the description of every subject, you should schedule the time of review to every subject in order to pass every subject and at the same time to obtain the higher total scores as possible.

 

Input

The input consists of multiple test cases. For each test case, the first line is an integer n (1<=n<=50), which is the number of subjects. Then n lines follow, each line has four integers si, ti, ai, di to describe the subject. si(0<=si<=100):the score that he can obtained without reviewing,ti(1<=ti<720):the time of examination,
ai(1<=ai<=40):the first hour reviewing on this subject will improve ai scores,di(0<=di<=4):the improving scores will decrease di every reviewing hour. For example,when ai = 10, di = 2, the first hour viewing will improve 10 scores , and the second hour viewing will only improve 8 scores.

 

Output

For each test case, to output in one line. If he can pass all the subjects, please output an integer which is the highest total scores, otherwise please output a string “you are unlucky”.

 

Sample Input

1

58 3 5 3

1

58 1 5 3

4

40 6 10 2

50 9 10 2

60 3 4 2

70 1 4 2

4

42 6 10 2

50 9 10 2

54 3 4 2

70 1 4 2

4

30 6 10 2

50 9 10 2

54 3 4 2

70 1 4 2

Sample Output

65

63

280

274

you are unlucky

HINT

Please noting: every subject’ full scores is 100. So when you get a result of one subject which is bigger than 100, you should regard the result as 100.

解题:贪心

首先要保证都及格,按考试时间从小到大排序,设置一个时间占用的数组用以标记此时间是否被占用,然后对没及格的科目,从截至时间从后往前走,发现一个没被用的时间,就占用该时间,直到及格。

 

然后用优先队列,优先选择当前能获得分数最高的先复习,同样是从截至时间往前走,遇到没占用的,直接占用,跳出循环。如果有多个最高值,直接选择截至时间靠前的。。。

CSUOJ 1603 Scheduling the final examination
 1 #include <bits/stdc++.h>

 2 using namespace std;

 3 const int maxn = 1000;

 4 struct node {

 5     int s,t,a,d;

 6     bool operator<(const node &x) const {

 7         if(a == x.a) return t < x.t;

 8         return a < x.a;

 9     }

10 } course[maxn];

11 int n;

12 bool used[maxn];

13 priority_queue<node>q;

14 bool cmp(const node &x,const node &y) {

15     return x.t < y.t;

16 }

17 int main() {

18     int ret;

19     bool flag;

20     while(~scanf("%d",&n)) {

21         memset(used,false,sizeof used);

22         flag = true;

23         while(!q.empty()) q.pop();

24         for(int i = ret = 0; i < n; ++i)

25             scanf("%d %d %d %d",&course[i].s,&course[i].t,&course[i].a,&course[i].d);

26         sort(course,course+n,cmp);

27         for(int i = 0; i < n; ++i) {

28             for(int j = course[i].t; course[i].a > 0 && j > 0 && course[i].s < 60; --j) {

29                 if(!used[j]) {

30                     used[j] = true;

31                     course[i].s = min(100,course[i].s + course[i].a);

32                     course[i].a = max(0,course[i].a - course[i].d);

33                 }

34             }

35             if(course[i].s < 60) {

36                 flag = false;

37                 break;

38             } else q.push(course[i]);

39         }

40         while(flag && !q.empty()) {

41             node now = q.top();

42             q.pop();

43             if(now.s == 100 || now.a == 0) {

44                 ret += now.s;

45                 continue;

46             }

47             bool mark = false;

48             for(int i = now.t; i > 0; --i) {

49                 if(!used[i]) {

50                     mark = used[i] = true;

51                     now.s = min(100,now.s + now.a);

52                     now.a = max(0,now.a - now.d);

53                     break;

54                 }

55             }

56             if(mark) q.push(now);

57             else ret += now.s;

58         }

59         if(flag) printf("%d\n",ret);

60         else puts("you are unlucky");

61     }

62     return 0;

63 }
View Code

 

你可能感兴趣的:(final)