Guest User

Untitled

a guest
Apr 8th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cassert>
  5. using namespace std;
  6.  
  7. const int INF = (int)1e6;
  8. int m = 1010;
  9. int del, k;
  10. int n;
  11. int it1, it2;
  12. int a[55];
  13. int dp[1010][52][52][3];
  14. int ans[55][2];
  15.  
  16. #define check(x, y, z, v) {assert((x) >= 0 && (y) >= 0&& (z) >= 0 && (v) >=0 && (x) < 1010 && (y) < 52 && (z) < 52 && (v) < 3);}
  17.  
  18. int main()
  19. {
  20.     scanf("%d%d", &del, &k);
  21.     scanf("%d", &n);
  22.     for (int i = 0; i < n; i++)
  23.         scanf("%d", &a[i]);
  24.     for (int i = 0; i < m; i++)
  25.         for (int j = 0; j <= n; j++)
  26.             for (int h = 0; h <= n; h++) {
  27. check(i,j,h, 0);
  28.                 dp[i][j][h][0] = INF;
  29. }
  30.     dp[0][0][0][0] = 0;
  31.     for (int tm = 0; tm < m; tm++)
  32.     {
  33.         while (it1 < n && a[it1] - del <= tm)
  34.             it1++;
  35.         while (it2 < n && a[it2] <= tm)
  36.             it2++;
  37.         for (int frst = 0; frst <= it1; frst++)
  38.             for (int scnd = it2; scnd <= frst; scnd++)
  39.             {
  40. check(tm, frst, scnd, 0);
  41.                 if (dp[tm][frst][scnd][0] == INF)
  42.                     continue;
  43. check(tm + 1, frst, scnd, 0);
  44. check(tm, frst, scnd, 0);
  45.                 if (dp[tm + 1][frst][scnd][0] > dp[tm][frst][scnd][0])
  46.                 {
  47. check(tm + 1, frst, scnd, 0);
  48. check(tm + 1, frst, scnd, 1);
  49. check(tm + 1, frst, scnd, 2);
  50.                     dp[tm + 1][frst][scnd][0] = dp[tm][frst][scnd][0];
  51.                     dp[tm + 1][frst][scnd][1] = frst;
  52.                     dp[tm + 1][frst][scnd][2] = scnd;
  53.                 }
  54. check(tm, frst, scnd, 0);
  55.                 int w = dp[tm][frst][scnd][0] + 1;
  56.                 if (k >= it1 - scnd)
  57.                 {
  58. check(tm + 1, it1, frst, 0);
  59.                     if (dp[tm + 1][it1][frst][0] > w)
  60.                     {
  61. check(tm + 1, it1, frst, 0);
  62.                         dp[tm + 1][it1][frst][0] = w;
  63.                         dp[tm + 1][it1][frst][1] = frst;
  64.                         dp[tm + 1][it1][frst][2] = scnd;
  65.                     }
  66.                 }
  67.                 else
  68.                 {
  69.                     for (int i = max( 0, k - (it1 - frst) ); i <= min(k, frst - scnd)  ; i++)
  70.                     {
  71. check(tm + 1, frst + k - i, scnd + i, 0);
  72.                         if (dp[tm + 1][frst + k - i][scnd + i][0] > w)
  73.                         {
  74.                             dp[tm + 1][frst + k - i][scnd + i][0] = w;
  75.                             dp[tm + 1][frst + k - i][scnd + i][1] = frst;
  76.                             dp[tm + 1][frst + k - i][scnd + i][2] = scnd;
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.     }
  82.  
  83.     m--;
  84. check(m,n,n,0);
  85.     if (dp[m][n][n][0] == INF)
  86.     {
  87.         printf("-1\n");
  88.         return 0;
  89.     }
  90.     printf("%d\n", dp[m][n][n][0]);
  91.  
  92.     int f = n, s = n;
  93.     for (; m > 0; m--)
  94.     {
  95. check(m,f,s,1);
  96.         int nf = dp[m][f][s][1];
  97.         int ns = dp[m][f][s][2];
  98.         for (int i = nf; i < f; i++)
  99.             ans[i][0] = m - 1;
  100.         for (int i = ns; i < s; i++)
  101.             ans[i][1] = m - 1;
  102.         f = nf;
  103.         s = ns;
  104.     }
  105.     for (int i = 0; i < n; i++)
  106.         printf("%d %d\n", ans[i][0], ans[i][1]);
  107.  
  108.     cin >> n;
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment