Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. #define MAXM 1000010
  8.  
  9. struct arc_t {
  10.     int m, f;
  11. };
  12.  
  13. vector<arc_t> archi;
  14. vector<arc_t> presi;
  15.  
  16. int N, M, F;
  17.  
  18.  
  19. int main() {
  20.     freopen("input.txt", "r", stdin);
  21.     freopen("output.txt", "w", stdout);
  22.  
  23.     scanf("%d%d%d", &M, &F, &N);
  24.  
  25.     for (int i = 0; i < N; i++) {
  26.         int m, f;
  27.         scanf("%d%d", &m, &f);
  28.         archi.push_back({m, f});
  29.     }
  30.  
  31.     vector<arc_t> tail(N);
  32.     tail[0] = archi[0];
  33.     int length = 1;
  34.  
  35.     for (int i = 0; i < N; i++) {
  36.         /*
  37.         printf("(%d, %d)\n", archi[i].m, archi[i].f);
  38.  
  39.         /// Mostro la sequenza corrente
  40.         for (int j = 0; j < length; j++) {
  41.             printf("[%d, %d]", tail[j].m, tail[j].f);
  42.         }
  43.         printf("\n");
  44.         */
  45.  
  46.         if (max(archi[i].m, archi[i].f) < min(tail[0].m, tail[0].f)) {
  47.             //printf("\tNuova sequenza\n");
  48.             tail[0] = archi[i];
  49.         }
  50.         else if (min(archi[i].m, archi[i].f) > max(tail[length-1].m, tail[length-1].f)) {
  51.             //printf("\tEstendo\n");
  52.             tail[length++] = archi[i];
  53.         }
  54.         else {
  55.             int lo = 0, hi = length-1;
  56.             while (lo <= hi) {
  57.                 int mid = (lo+hi) / 2;
  58.                 /// Se tail[mid] si trova prima o si interseca con archi[i]
  59.                 if (max(tail[mid].m, tail[mid].f) <= min(archi[i].m, archi[i].f)) {
  60.                     lo = mid + 1;
  61.                 }
  62.                 else {
  63.                     hi = mid - 1;
  64.                 }
  65.             }
  66.             if (lo < length)
  67.                 tail[lo] = archi[i];
  68.         }
  69.         //printf("\n");
  70.     }
  71.     printf("%d\n", length*2);
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement