TIANKENG’s restaurant HDU - 4883
考察:贪心
错误思路:
用区间选点的模板处理,然后发现很难处理连续有重合区间与不重合区间....没在网上看到用这种解法的,基本都是下面的解法.
正确思路1:
将区间拆成两个时间点,到达时间增加人数,离去时间减去人数.将这些人按时间排序加减即可.
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 const int N = 20010,INF = 0x3f3f3f3f; 7 struct PEO{ 8 int t,nums; 9 bool operator<(const PEO& p)const{ 10 if(this->t==p.t) return this->nums<p.nums; 11 else return this->t<p.t; 12 } 13 }peo[N]; 14 int cnt; 15 int main() 16 { 17 int T; 18 scanf("%d",&T); 19 while(T--) 20 { 21 int n,ans = 0,cnt = 0,sum = 0; 22 scanf("%d",&n); 23 for(int i=1;i<=n;i++) 24 { 25 int h,m,nums; 26 scanf("%d%d:%d",&nums,&h,&m); 27 peo[++cnt].nums = nums; 28 peo[cnt].t = h*60+m; 29 scanf("%d:%d",&h,&m); 30 peo[++cnt].t = h*60+m; 31 peo[cnt].nums = -nums; 32 } 33 sort(peo+1,peo+cnt+1); 34 for(int i=1;i<=cnt;i++) 35 { 36 sum+=peo[i].nums; 37 ans = max(sum,ans); 38 } 39 printf("%d\n",ans); 40 } 41 return 0; 42 }
正确思路2:
同样是拆成两个时间点,但是下面找答案不是for循环遍历集合数.而是从0~1440(24*60).在相应时间点增加人数.
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 const int N = 1480,INF = 0x3f3f3f3f; 7 int guest[N]; 8 int main() 9 { 10 int T; 11 scanf("%d",&T); 12 while(T--) 13 { 14 int n,ans = 0,cnt = 0,sum = 0; 15 scanf("%d",&n); 16 memset(guest,0,sizeof guest); 17 for(int i=1;i<=n;i++) 18 { 19 int h,m,nums; 20 scanf("%d%d:%d",&nums,&h,&m); 21 guest[h*60+m] += nums; 22 scanf("%d:%d",&h,&m); 23 guest[h*60+m] -= nums; 24 } 25 for(int i=0;i<=1440;i++) 26 { 27 sum+=guest[i]; 28 ans = max(sum,ans); 29 } 30 printf("%d\n",ans); 31 } 32 return 0; 33 }