Advertisement
PikMike

Untitled

Apr 30th, 2017
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define pb push_back
  4. #define mp make_pair
  5. #define sz(x) (int)(x).size()
  6. #define li long long
  7. #define ld long double
  8. #define x first
  9. #define y second
  10. #define pt pair<int, int>
  11. #define pll pair<li, li>
  12. #define forn(i, t) for(int i = 0; i < (t); i++)
  13. #define fore(i, f, t) for(int i = (f); i < (t); i++)
  14. #define forr(i, f, t) for(int i = (f) - 1; i >= (t); i--)
  15. #define all(x) (x).begin(), (x).end()
  16. #define ins insert
  17.  
  18. using namespace std;
  19.  
  20.  
  21. const int INF = 1e9;
  22. const int MOD = 1e9 + 7;
  23. const li INF64 = 1e18;
  24. const ld EPS = 1e-7;
  25.  
  26. mt19937 myrand(time(NULL));
  27.  
  28. const int N = 100 + 7;
  29. const int M = 720;
  30.  
  31. int n, m;
  32. pt a[N], b[N];
  33.  
  34.  
  35. bool read(){
  36.     if(scanf("%d%d", &n, &m) != 2)
  37.         return 0;
  38.     forn(i, n)
  39.         scanf("%d%d", &a[i].x, &a[i].y);
  40.     forn(i, m)
  41.         scanf("%d%d", &b[i].x, &b[i].y);
  42.     return 1;
  43. }
  44.  
  45.  
  46. int dp[2][M + 7][M + 7];
  47.  
  48.  
  49. void solve(){
  50.     memset(dp, 127, sizeof(dp));
  51.     dp[0][0][0] = dp[1][0][0] = 0;
  52.     forn(i, M + 1){
  53.         forn(j, M + 1){
  54.             bool fll = false, flr = false;
  55.             forn(l, n)
  56.                 if (i + j >= a[l].x && i + j < a[l].y){
  57.                     fll = true;
  58.                     break;
  59.                 }
  60.             forn(r, m)
  61.                 if (i + j >= b[r].x && i + j < b[r].y){
  62.                     flr = true;
  63.                     break;
  64.                 }
  65.             if (!fll)
  66.                 dp[0][i + 1][j] = min(dp[0][i + 1][j], min(dp[0][i][j], dp[1][i][j] + 1));
  67.             if (!flr)
  68.                 dp[1][i][j + 1] = min(dp[1][i][j + 1], min(dp[0][i][j] + 1, dp[1][i][j]));
  69.         }
  70.     }
  71.    
  72.     if (dp[0][M][M] & 1)
  73.         dp[0][M][M]++;
  74.     if (dp[1][M][M] & 1)
  75.         dp[1][M][M]++;
  76.     printf("%d\n", min(dp[0][M][M], dp[1][M][M]));
  77. }
  78.  
  79.  
  80. int main(){
  81.     #ifdef _DEBUG
  82.         freopen("input.txt", "r", stdin);
  83.     #endif
  84.     int n;
  85.     scanf("%d\n", &n);
  86.     forn(i, n){
  87.         read();
  88.         printf("Case #%d: ", i + 1);
  89.         solve();
  90.     }
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement