Jiangxi University of Finance and Economics's 1st Programming Contest G Question Small Q's Pocket Campus

Topic link: https://www.nowcoder.com/acm/contest/115/G

Problem-solving ideas: Problem solving is just a piece of code, greedy thinking. Sort by start time first.

Then keep greedy to get the maximum value of happy[j].

If this topic is greedy, it must be sorted.

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 #include<string>
 5 #include<string.h>
 6 using namespace std;
 7 struct Node{
 8     int s,e,h,g;
 9 }num[205];
10 bool cmp(Node a,Node b){
11     return a.s<b.s;
12 }
13 int happy[30],grade[30];
14 int main(){
15     int T;
16     cin>>T;
17     while(T--){
18         int n;
19         cin>>n;
20         for(int i=0;i<n;i++){
21             cin>>num[i].s>>num[i].e>>num[i].h>>num[i].g;
22         }
23         sort(num,num+n,cmp);
24         memset(happy,0,sizeof(happy));
25         memset(grade,0,sizeof(grade));
26         for(int i=0;i<n;i++){
27             for(int j=num[i].e;j<=24;j++){
28                 if(happy[j]<happy[num[i].s]+num[i].h){
29                     happy[j]=happy[num[i].s]+num[i].h;
30                     grade[j]=grade[num[i].s]+num[i].g;
31                 }else if(happy[j]==happy[num[i].s]+num[i].h){
32                     grade[j]=max(grade[j],grade[num[i].s]+num[i].g);
33                 }
34             }
35         }
36         cout<<happy[24]<<' '<<grade[24]<<endl;
37     }
38     return 0;
39 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324645814&siteId=291194637