[关闭]
@Archger 2017-03-15T16:31:52.000000Z 字数 1133 阅读 661

dp题集

acm 题集 dp


HDU 2844 Coins

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cmath>
  4. #include<cstdio>
  5. #include<cstdlib>
  6. #include<queue>
  7. #include<map>
  8. #include<set>
  9. #include<stack>
  10. #include<bitset>
  11. #include<numeric>
  12. #include<vector>
  13. #include<string>
  14. #include<iterator>
  15. #include<cstring>
  16. #include<functional>
  17. #define INF 0x3f3f3f3f
  18. #define ms(a,b) memset(a,b,sizeof(a))
  19. #define pi 3.14159265358979
  20. #define mod 1000000007
  21. #define maxn 100010
  22. using namespace std;
  23. typedef pair<int, int> P;
  24. typedef long long ll;
  25. typedef unsigned long long ull;
  26. int n, m, c[maxn], v[maxn], d[maxn];
  27. void zeroonepack(int cost, int weight)
  28. {
  29. for (int i = m; i >= cost; i--)
  30. {
  31. d[i] = max(d[i], d[i - cost] + weight);
  32. }
  33. }
  34. void completepack(int cost, int weight)
  35. {
  36. for (int i = cost; i <= m; i++)
  37. {
  38. d[i] = max(d[i], d[i - cost] + weight);
  39. }
  40. }
  41. void multipack(int cost, int weight, int amount)
  42. {
  43. if (cost*amount >= m)
  44. completepack(cost, weight);
  45. else
  46. {
  47. int k = 1;
  48. while (k <= amount)
  49. {
  50. zeroonepack(k*cost, k*weight);
  51. amount -= k;
  52. k *= 2;
  53. }
  54. zeroonepack(amount*cost, amount*weight);
  55. }
  56. }
  57. int main()
  58. {
  59. while (cin >> n >> m)
  60. {
  61. if (!n && !m) break;
  62. ms(d, 0);
  63. for (int i = 0; i < n; i++)
  64. {
  65. cin >> v[i];
  66. }
  67. for (int i = 0; i < n; i++)
  68. {
  69. cin >> c[i];
  70. }
  71. for (int i = 0; i < n; i++)
  72. {
  73. multipack(v[i], v[i], c[i]);
  74. }
  75. int sum = 0;
  76. for (int i = 1; i <= m; i++)
  77. {
  78. if (d[i] == i)
  79. sum++;
  80. }
  81. cout << sum << endl;
  82. }
  83. }

#

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注