Ural 1203 Scientific Conference (Greedy | DP)

Source: Internet
Author: User

Scientific Conference

 

I used to brush the computation of ry. I was able to buy the computation of ry even during the invitational competition. During the summer vacation, I went to multiple schools, supplemented the questions of multiple schools, and refreshed the weak DP. If multiple schools have computational ry, they must be eliminated -. -

 

Question: give you the start time and end time of the N reports and ask you how many reports you can listen. The minimum interval between reports is 1.

 

Idea: it is actually a matter of activity arrangement. You can use greedy or DP. It is easier to write greedy. Because DP is practiced, you can write it again using DP.

 

Greedy is a very simple issue of activity selection. Starting from the end time, find the best choice for each activity.

 1 struct node{ 2     int b, e; 3 } N[100005]; 4  5 int cmp(node x, node y){ 6     if(x.e == y.e) 7         return x.b < y.b; 8     return x.e < y.e; 9 }10 11 int n;12 int main()13 {14     scanf("%d", &n);15     for(int i = 0; i < n; ++i){16         scanf("%d%d", &N[i].b, &N[i].e);17     }18     sort(N, N+n, cmp);19     int ans = 0;20     int t = 0;21     for(int i = 0; i < n; ++i)22     {23         if(N[i].b >= t)24         {25             ans++;26             t = N[i].e+1;27         }28     }29     printf("%d\n", ans);30 31     return 0;32 }
Greedy

 

 

DP:

1 struct node {2 int B, E; 3} n [0, 100005]; 4 5 Int CMP (node X, node y) {6 if (X. E = y. e) 7 return X. B <Y. b; 8 return X. E <Y. e; 9} 10 11 int N; 12 int dp [1, 30005]; 13 int K [2, 30005]; 14 int main () 15 {16 scanf ("% d ", & N); 17 int last =-1; 18 for (INT I = 0; I <n; ++ I) {19 scanf ("% d ", & N [I]. b, & N [I]. e); 20 last = max (last, N [I]. e); 21} 22 sort (N, N + N, CMP); 23 for (INT I = 0; I <n; ++ I) 24 {25 DP [n [I]. e] = 1; // the end time of the record is 26 K [n [I]. e] = N [I]. b; // the start time of the activity that records the end time is 27 // There Is A sorting prior to the start time, so the selection will overwrite the optimal 28} 29 for (INT I = 1; I <= last; ++ I) 30 {// DP time 31 if (K [I]) /// if the current time point has ended activity 32 DP [I] = max (DP [I-1], DP [K [I]-1] + 1 ); 33 DP [I] = max (DP [I], DP [I-1]); /// if activity 34} 35 printf ("% d \ n", DP [last]) does not end at the current time point; 36 37 return 0; 38}
DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.