Advertisement
Guest User

Untitled

a guest
Aug 16th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <stack>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <string>
  10. #include <algorithm>
  11. #include <iostream>
  12. using namespace std;
  13.  
  14. struct T {
  15.     int a, c, n;
  16. } t[610];
  17.  
  18. bool cmpT(T a, T b) {
  19.     return a.c < b.c;
  20. }
  21.  
  22. int n, k, p, b[610], d[610][610], f[610][610], r;
  23.  
  24. int main() {
  25.     freopen("input.txt", "r", stdin);
  26.     freopen("output.txt", "w", stdout);
  27.     for (int i = 0; i < 610; i++)
  28.         for (int j = 0; j < 610; j++)
  29.             d[i][j] = f[i][j] = -1;
  30.     scanf("%d%d%d", &n, &k, &p);
  31.     for (int i = 0; i < k; i++)
  32.         scanf("%d", &b[i]);
  33.     for (int i = 0; i < n; i++) {
  34.         scanf("%d%d", &t[i].a, &t[i].c);
  35.         t[i].c--;
  36.         t[i].n = i + 1;    
  37.     }
  38.     sort(&t[0], &t[n], cmpT);
  39.     for (int i = 0; i < n; i++) {
  40.         if (t[i].a + b[t[i].c] <= p) {
  41.             d[i][1] = t[i].a + b[t[i].c];
  42.             r = 1;
  43.         }
  44.     }
  45.     for (int tCnt = 2; tCnt <= n; tCnt++) {
  46.         for (int tLast = 0; tLast < n; tLast++) {
  47.             for (int tPred = 0; tPred < tLast; tPred++) {
  48.                 if (d[tPred][tCnt - 1] == -1)
  49.                     continue;
  50.                 int pp = d[tPred][tCnt - 1] + t[tLast].a;
  51.                 if (t[tLast].c != t[tPred].c)
  52.                     pp += b[t[tLast].c];
  53.                 if (pp <= p && (d[tLast][tCnt] == -1 || pp < d[tLast][tCnt])) {
  54.                     d[tLast][tCnt] = pp;
  55.                     f[tLast][tCnt] = tPred;
  56.                     r = tCnt;
  57.                 }
  58.             }
  59.         }
  60.     }
  61.     printf("%d\n", r);
  62.     if (r) {
  63.         int tCnt = r, tLast = 0;
  64.         while (d[tLast][tCnt] == -1)
  65.             tLast++;
  66.         while (tCnt) {
  67.             printf("%d ", t[tLast].n);
  68.             tLast = f[tLast][tCnt--];
  69.         }
  70.     }
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement