fork(1) download
  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cassert>
  4. #include <climits>
  5. #include <cmath>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <deque>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <map>
  13. #include <numeric>
  14. #include <queue>
  15. #include <set>
  16. #include <stack>
  17. #include <string>
  18.  
  19. #ifdef PRINTERS
  20. #include "printers.hpp"
  21. using namespace printers;
  22. #define tr(a) cerr<<#a<<": "<<a<<endl;
  23. #else
  24. #define tr(a)
  25. #endif
  26.  
  27. #define ll long long
  28. #define pb push_back
  29. #define mp make_pair
  30. #define pii pair<int,int>
  31. #define vi vector<int>
  32. #define all(a) (a).begin(),(a).end()
  33. #define F first
  34. #define S second
  35. #define sz(x) (int)x.size()
  36. #define hell 1000000007
  37. #define endl '\n'
  38. #define rep(i,a,b) for(int i=a;i<b;i++)
  39. using namespace std;
  40. int test=1;
  41. void solve(){
  42. cout<<"Case #"<<test++<<": ";
  43. int AC,AJ;
  44. cin>>AC>>AJ;
  45. int N=AC+AJ;
  46. vector<pair<pii,int>>v(AC+AJ);
  47. int ACcnt=0,AJcnt=0,othercnt=0,ACmid=0,AJmid=0;
  48. vi AJs,ACs;
  49. rep(i,0,AC){
  50. cin>>v[i].F.F>>v[i].F.S;
  51. v[i].S=1;
  52. ACcnt+=v[i].F.S-v[i].F.F;
  53. }
  54. rep(i,0,AJ){
  55. cin>>v[AC+i].F.F>>v[AC+i].F.S;
  56. v[AC+i].S=2;
  57. AJcnt+=v[AC+i].F.S-v[AC+i].F.F;
  58. }
  59. sort(all(v));
  60. int ans=0;
  61. rep(i,0,N){
  62. auto x=v[i];
  63. auto y=v[(i+1)%N];
  64. if(y.F.F<x.F.S)y.F.F+=1440,y.F.S+=1440;
  65. if(x.S!=y.S){
  66. othercnt+=(y.F.F-x.F.S);
  67. ans++;
  68. }
  69. else if(x.S==1){
  70. ACmid+=(y.F.F-x.F.S);
  71. ACs.pb(y.F.F-x.F.S);
  72. }
  73. else{
  74. AJmid+=(y.F.F-x.F.S);
  75. AJs.pb(y.F.F-x.F.S);
  76. }
  77. }
  78.  
  79. sort(all(ACs),greater<int>());
  80. sort(all(AJs),greater<int>());
  81. if(ACcnt+othercnt+ACmid>=720 and AJcnt+othercnt+AJmid>=720){
  82. cout<<ans<<endl;
  83. return;
  84. }
  85. if(ACcnt+othercnt+ACmid<720){
  86. int reqd=720-(ACcnt+othercnt+ACmid);
  87. for(auto i:AJs){
  88. if(reqd>0)reqd-=i,ans+=2;
  89. }
  90. cout<<ans<<endl;
  91. return;
  92. }
  93. if(AJcnt+othercnt+AJmid<720){
  94. int reqd=720-(AJcnt+othercnt+AJmid);
  95. for(auto i:ACs){
  96. if(reqd>0)reqd-=i,ans+=2;
  97. }
  98. cout<<ans<<endl;
  99. return;
  100. }
  101.  
  102. }
  103.  
  104. int main(){
  105. ios_base::sync_with_stdio(false);
  106. cin.tie(0);
  107. cout.tie(0);
  108. int t=1;
  109. cin>>t;
  110. while(t--){
  111. solve();
  112. }
  113. return 0;
  114. }
Success #stdin #stdout 0s 15248KB
stdin
5
1 1
540 600
840 900
2 0
900 1260
180 540
1 1
1439 1440
0 1
2 2
0 1
1439 1440
1438 1439
1 2
3 4
0 10
1420 1440
90 100
550 600
900 950
100 150
1050 1400
stdout
Case #1: 2
Case #2: 4
Case #3: 2
Case #4: 4
Case #5: 6