Advertisement
Guest User

Untitled

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