Advertisement
Guest User

Untitled

a guest
May 6th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2015, Andrew Chen <llkiwi2006@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31.  
  32. #include <cstdio>
  33. #include <cstring>
  34. #include <algorithm>
  35.  
  36. #include <vector>
  37.  
  38. using namespace std;
  39.  
  40. int total_t; // `T`
  41.  
  42. struct item {
  43. int t, a, b;
  44. item(int _t, int _a, int _b) : t(_t), a(_a), b(_b) {}
  45. };
  46.  
  47. bool cmp(const item& a, const item& b) {
  48. // sort according descending to `(b / t)`
  49. return ((double) a.b / a.t) > ((double) b.b / b.t);
  50. }
  51.  
  52. long long f1[5001];
  53. long long f2[5001];
  54.  
  55. int main() {
  56. int n;
  57. vector<item> items_pos;
  58. vector<item> items_neg;
  59. scanf("%d%d", &total_t, &n);
  60. for (int i = 0; i < n; i++) {
  61. int t, a, b;
  62. scanf("%d%d%d", &t, &a, &b);
  63. if (b >= 0) items_pos.push_back(item(t, a, b));
  64. else items_neg.push_back(item(t, a, b));
  65. }
  66.  
  67. sort(items_pos.begin(), items_pos.end(), cmp);
  68. sort(items_neg.begin(), items_neg.end(), cmp);
  69.  
  70. memset(f1, 0, sizeof(f1));
  71. memset(f2, 0, sizeof(f2));
  72.  
  73. for (unsigned int i = 0; i < items_pos.size(); i++) {
  74. for (unsigned int j = 0; j <= total_t; j++) {
  75. if (j + items_pos[i].t <= total_t) {
  76. int value = items_pos[i].a + (j + items_pos[i].t) * items_pos[i].b;
  77. f1[j] = max(f1[j], f1[j + items_pos[i].t] + value);
  78. }
  79. }
  80. }
  81. for (int i = total_t - 1; i >= 0; i--) f1[i] = max(f1[i], f1[i + 1]);
  82.  
  83. for (int i = items_neg.size() - 1; i >= 0; i--) {
  84. for (unsigned int j = 0; j <= total_t; j++) {
  85. if (j + items_neg[i].t <= total_t) {
  86. int value = items_neg[i].a + (total_t - j) * items_neg[i].b;
  87. f2[j] = max(f2[j], f2[j + items_neg[i].t] + value);
  88. }
  89. }
  90. }
  91. for (int i = total_t - 1; i >= 0; i--) f2[i] = max(f2[i], f2[i + 1]);
  92.  
  93. long long the_max = 0;
  94. for (int j = 0; j <= total_t; j++) the_max = max(the_max, f1[j] + f2[total_t - j]);
  95. printf("%lld\n", the_max);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement