码迷,mamicode.com
首页 > 其他好文 > 详细

1037. Magic Coupon (25)

时间:2015-12-06 11:15:39      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

1,注意加法越界的问题,用long long可以存一次和,相加之后要写加法器来做
2,注意vector 中是用 size() 函数来表示其内部的个数的。  结尾附上常用vector函数

The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the shop also offers some bonus product for free. However, if you apply a coupon with a positive N to this bonus product, you will have to pay the shop N times the value of the bonus product... but hey, magically, they have some coupons with negative N‘s!

For example, given a set of coupons {1 2 4 -1}, and a set of product values {7 6 -2 -3} (in Mars dollars M$) where a negative value corresponds to a bonus product. You can apply coupon 3 (with N being 4) to product 1 (with value M$7) to get M$28 back; coupon 2 to product 2 to get M$12 back; and coupon 4 to product 4 to get M$3 back. On the other hand, if you apply coupon 3 to product 4, you will have to pay M$12 to the shop.

Each coupon and each product may be selected at most once. Your task is to get as much money back as possible.

Input Specification:

Each input file contains one test case. For each case, the first line contains the number of coupons NC, followed by a line with NC coupon integers. Then the next line contains the number of products NP, followed by a line with NP product values. Here 1<= NC, NP <= 105, and it is guaranteed that all the numbers will not exceed 230.

Output Specification:

For each test case, simply print in a line the maximum amount of money you can get back.

Sample Input:
4
1 2 4 -1
4
7 6 -2 -3
Sample Output:
43

 

  1. #pragma warning(disable:4996)
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. vector<long long int> Nina0, Nouta0,Ninu0,Noutu0;
  7. char result[100] = { ‘0 ‘};
  8. bool cmp1(int a, int b) {
  9. return a > b;
  10. }
  11. bool cmp2(int a, int b) {
  12. return a < b;
  13. }
  14. void AddSum(long long int a) {
  15. int jinwei = 0;
  16. int temp;
  17. int i = 0;
  18. while (a!=0||jinwei!=0) {
  19. temp = a % 10;
  20. a = a / 10;
  21. if (temp + result[i] + jinwei <= ‘9‘) {
  22. result[i] = temp + result[i] + jinwei;
  23. jinwei = 0;
  24. }
  25. else {
  26. result[i] = temp + result[i] + jinwei - 10;
  27. jinwei = 1;
  28. }
  29. i++;
  30. }
  31. }
  32. int main(void) {
  33. //long long int k = 1073741825;
  34. //long long int p;
  35. //p = k*k *10000;
  36. //cout << p;
  37. int nc, np;
  38. cin >> nc;
  39. for (int i = 0; i < 99; i++)
  40. result[i] = ‘0‘;
  41. for (int i = 0; i < nc; i++) {
  42. long long int temp;
  43. cin >> temp;
  44. if (temp > 0)
  45. Nina0.push_back(temp);
  46. else
  47. Ninu0.push_back(temp);
  48. }
  49. cin >> np;
  50. for (int i = 0; i < np; i++) {
  51. int temp2;
  52. cin >> temp2;
  53. if (temp2 > 0)
  54. Nouta0.push_back(temp2);
  55. else
  56. Noutu0.push_back(temp2);
  57. }
  58. if(Nina0.empty()==false)
  59. sort(Nina0.begin(), Nina0.end(), cmp1);
  60. if(Nouta0.empty()==false)
  61. sort(Nouta0.begin(), Nouta0.end(), cmp1);
  62. if (Ninu0.empty() == false)
  63. sort(Ninu0.begin(), Ninu0.end(), cmp2);
  64. if (Noutu0.empty() == false)
  65. sort(Noutu0.begin(), Noutu0.end(), cmp2);
  66. int index;
  67. long long int sum = 0;
  68. if (Nina0.empty() == false && Ninu0.empty() == false && Nouta0.empty() == false && Noutu0.empty() == false) {
  69. if (Nina0.size() >= Nouta0.size()) {
  70. index = Nouta0.size();
  71. }
  72. else
  73. index = Nina0.size();
  74. for (int i = 0; i < index; i++) {
  75. sum = Nina0[i] * Nouta0[i];
  76. AddSum(sum);
  77. }
  78. if (Ninu0.size() <= Noutu0.size()) {
  79. index = Ninu0.size();
  80. }
  81. else
  82. index = Noutu0.size();
  83. for (int i = 0; i < index; i++) {
  84. sum = Ninu0[i] * Noutu0[i];
  85. AddSum(sum);
  86. }
  87. }
  88. else if (Nina0.empty() == true || Nouta0.empty() == true) {
  89. if (Ninu0.size() <= Noutu0.size()) {
  90. index = Ninu0.size();
  91. }
  92. else
  93. index = Noutu0.size();
  94. for (int i = 0; i < index; i++) {
  95. sum = Ninu0[i] * Noutu0[i];
  96. AddSum(sum);
  97. }
  98. }
  99. else if (Ninu0.empty() == true || Noutu0.empty() == true) {
  100. if (Nina0.size() >= Nouta0.size()) {
  101. index = Nouta0.size();
  102. }
  103. else
  104. index = Nina0.size();
  105. for (int i = 0; i < index; i++) {
  106. sum = Nina0[i] * Nouta0[i];
  107. AddSum(sum);
  108. }
  109. }
  110. int flag = 0;
  111. for (int i = 99; i >= 0; i--) {
  112. if (result[i] > ‘0‘) {
  113. flag = i;
  114. break;
  115. }
  116. }
  117. for (int i = flag; i >= 0; i--)
  118. cout << result[i];
  119. return 0;
  120. }


1.push_back 在数组的最后添加一个数据
2.pop_back 去掉数组的最后一个数据
3.at 得到编号位置的数据
4.begin 得到数组头的指针
5.end 得到数组的最后一个单元+1的指针
6.front 得到数组头的引用
7.back 得到数组的最后一个单元的引用
8.max_size 得到vector最大可以是多大
9.capacity 当前vector分配的大小
10.size 当前使用数据的大小
11.resize 改变当前使用数据的大小,如果它比当前使用的大,者填充默认值
12.reserve 改变当前vecotr所分配空间的大小
13.erase 删除指针指向的数据项
14.clear 清空当前的vector
15.rbegin 将vector反转后的开始指针返回(其实就是原来的end-1)
16.rend 将vector反转构的结束指针返回(其实就是原来的begin-1)
17.empty 判断vector是否为空
18.swap 与另一个vector交换数据


 



1037. Magic Coupon (25)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!