Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4883
Solution Report: There are n waves of guests in a restaurant each day. The arrival time of the I wave k guest is S, and the departure time is E. How many people can find the most popular restaurants?
At first, I wrote it using a line segment tree, but re. Later I saw a method that was very clever: all the time in a day was converted into minutes, ranging from 0 to 1440, all are initialized to 0, and if there are K
When the guests arrive, they will be in the s position + K of the array, and then-K at the departure time. Finally, they will scan the array and judge the maximum value during the scanning process.
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 const int maxn = 1500; 7 int que[maxn],T,n; 8 9 int main()10 {11 scanf("%d",&T);12 while(T--)13 {14 scanf("%d",&n);15 memset(que,0,sizeof(que));16 int d,s1,s2,e1,e2;17 while(n--)18 {19 scanf("%d %d:%d %d:%d",&d,&s1,&s2,&e1,&e2);20 s1 = 60 * s1 + s2;21 e1 = 60 * e1 + e2;22 que[s1] += d;23 que[e1] -= d;24 }25 int tot = 0,ans = 0;26 for(int i = 0;i <= 1440;++i)27 {28 tot += que[i];29 ans = max(ans,tot);30 }31 printf("%d\n",ans);32 }33 return 0;34 }
View code