Guest User

Untitled

a guest
Jan 17th, 2013
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. /*
  2.  * File:     main.cpp
  3.  * Author:   Hrayr [HarHro94]
  4.  * Problem:  acmp.ru
  5.  * IDE:      Visual C++ 2010
  6.  */
  7. #include <algorithm>
  8. #include <iostream>
  9. #include <cstring>
  10. #include <vector>
  11. #include <cstdio>
  12. #include <queue>
  13. #include <set>
  14. using namespace std;
  15.  
  16. const int INF = 0x7FFFFFFF;
  17.  
  18. int n, rate, a[1007], b[1007], dp[1007][1007], pred[1007][1007]; // dp[curMan][curCnt] -> max rate
  19. vector<pair<pair<int, int>, int> > p;
  20. vector<int> ans, m;
  21.  
  22. bool cmp(int i1, int i2);
  23.  
  24. int main()
  25. {
  26.     freopen("input.txt", "r", stdin);
  27.     freopen("output.txt", "w", stdout);
  28.     int i, j, ptr, cnt;
  29.     vector<int> tmp;
  30.     cin >> n >> rate;
  31.     for(i=0;i<n;++i)
  32.     {
  33.         cin >> a[i] >> b[i];
  34.         if (b[i]>=0)
  35.         {
  36.             p.push_back(make_pair(make_pair(a[i], b[i]), i));
  37.         }
  38.         else
  39.         {
  40.             m.push_back(i);
  41.         }
  42.     }
  43.     sort(p.begin(), p.end());
  44.     ptr=0;
  45.     while(ptr<(int)p.size() && p[ptr].first.first<=rate)
  46.     {
  47.         rate+=p[ptr].first.second;
  48.         ans.push_back(p[ptr].second+1);
  49.         ++ptr;
  50.     }
  51.     sort(m.begin(), m.end(), cmp);
  52.     cnt=(int)m.size();
  53.     for(i=0;i<=cnt;++i)
  54.     {
  55.         for(j=0;j<=cnt;++j)
  56.         {
  57.             dp[i][j]=-INF;
  58.         }
  59.     }
  60.     for(i=0;i<=cnt;++i)
  61.     {
  62.         dp[i][0]=rate;
  63.     }
  64.     for(i=1;i<=cnt;++i)
  65.     {
  66.         for(j=1;j<=cnt;++j)
  67.         {
  68.             if (dp[i-1][j]!=-INF && dp[i][j]<dp[i-1][j])
  69.             {
  70.                 dp[i][j]=dp[i-1][j];
  71.                 pred[i][j]=1;
  72.             }
  73.             if (a[m[i-1]]<=dp[i-1][j-1] && dp[i][j]<dp[i-1][j-1]+b[m[i-1]])
  74.             {
  75.                 dp[i][j]=dp[i-1][j-1]+b[m[i-1]];
  76.                 pred[i][j]=2;
  77.             }
  78.         }
  79.     }
  80.     for(j=0;j<=cnt;++j)
  81.     {
  82.         if (dp[cnt][j]!=-INF)
  83.         {
  84.             i=j;
  85.         }
  86.     }
  87.     j=i;
  88.     i=cnt;
  89.     while(j!=0)
  90.     {
  91.         if (pred[i][j]==2)
  92.         {
  93.             tmp.push_back(m[i-1]+1);
  94.             --j;
  95.         }
  96.         --i;
  97.     }
  98.     reverse(tmp.begin(), tmp.end());
  99.     for(i=0;i<(int)tmp.size();++i)
  100.     {
  101.         ans.push_back(tmp[i]);
  102.     }
  103.     cout << ans.size() << endl;
  104.     for(i=0;i<ans.size();++i)
  105.     {
  106.         cout << ans[i] << " ";
  107.     }
  108.     cout << endl;
  109.     return 0;
  110. }
  111.  
  112. bool cmp(int i1, int i2)
  113. {
  114.     return a[i1]+b[i1]>a[i2]+b[i2];
  115. }
Advertisement
Add Comment
Please, Sign In to add comment