company——桶思想

港控/mmm° 2022-06-16 10:44 255阅读 0赞

Think:
1桶思想
2反思:心态不稳,数组越界
3题意:n件物品,每件物品价值vali,每件物品库存cnti,for any goods sold on day i, if its direct benefit is val, the total benefit would be i⋅val.

company
Time Limit: 1000MS Memory Limit: 65536KB

Problem Description
There are n kinds of goods in the company, with each of them has a inventory of 这里写图片描述 and direct unit benefit . Now you find due to price changes, for any goods sold on day i, if its direct benefit is val, the total benefit would be i⋅val.
Beginning from the first day, you can and must sell only one good per day until you can’t or don’t want to do so. If you are allowed to leave some goods unsold, what’s the max total benefit you can get in the end?

Input
The first line contains an integers n(1≤n≤1000).
The second line contains n integers val1,val2,..,valn(−100≤这里写图片描述.≤100).
The third line contains n integers cnt1,cnt2,..,cntn(1≤这里写图片描述≤100).

Output
Output an integer in a single line, indicating the max total benefit.
Example Input

4
-1 -100 5 6
1 1 1 2

Example Output
51

Hint
sell goods whose price with order as -1, 5, 6, 6, the total benefit would be -1*1 + 5*2 + 6*3 + 6*4 = 51.

Author
“浪潮杯”山东省第八届ACM大学生程序设计竞赛(感谢青岛科技大学)

以下为Runtime Error代码——数组越界(1≤n≤1000)

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n, i, j, k, tp, x;
  6. long long sum, ans;
  7. int a[104], v[204];
  8. while(scanf("%d", &n) != EOF)
  9. {
  10. tp = 0;
  11. ans = 0;
  12. memset(v, 0, sizeof(v));///初始化桶思想数组
  13. for(i = 0; i < n; i++)
  14. scanf("%d", &a[i]);
  15. for(i = 0; i < n; i++)
  16. {
  17. scanf("%d", &x);
  18. v[a[i]+100] += x;///桶思想存储负数下标
  19. }
  20. for(i = 0; i <= 200; i++)
  21. {
  22. sum = 0, tp = 0;///初始化记录变量和累加变量
  23. if(v[i])
  24. {
  25. for(j = i; j <= 200; j++)
  26. {
  27. for(k = 0; k < v[j]; k++)///当前货物可能有多件
  28. {
  29. tp++;
  30. sum += (j-100)*tp;///注意当前货物效益为j-100
  31. }
  32. }
  33. ans = max(ans, sum);///记录最优解
  34. }
  35. if(i >= 100 && v[i])///优化:提前结束条件(第一次效益为正获得的总效益一定比之后起始点出发获得的总效益要大)
  36. break;
  37. }
  38. printf("%lld\n", ans);
  39. }
  40. return 0;
  41. }
  42. /***************************************************
  43. User name:
  44. Result: Runtime Error
  45. Take time: 0ms
  46. Take Memory: 0KB
  47. Submit time: 2017-05-13 16:42:01
  48. ****************************************************/

以下为Accepted代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n, i, j, k, tp, x;
  6. long long sum, ans;
  7. int a[1004], v[204];
  8. while(scanf("%d", &n) != EOF)
  9. {
  10. tp = 0;
  11. ans = 0;
  12. memset(v, 0, sizeof(v));///初始化桶思想数组
  13. for(i = 0; i < n; i++)
  14. scanf("%d", &a[i]);
  15. for(i = 0; i < n; i++)
  16. {
  17. scanf("%d", &x);
  18. v[a[i]+100] += x;///桶思想存储负数下标
  19. }
  20. for(i = 0; i <= 200; i++)
  21. {
  22. sum = 0, tp = 0;///初始化记录变量和累加变量
  23. if(v[i])
  24. {
  25. for(j = i; j <= 200; j++)
  26. {
  27. for(k = 0; k < v[j]; k++)///当前货物可能有多件
  28. {
  29. tp++;
  30. sum += (j-100)*tp;///注意当前货物效益为j-100
  31. }
  32. }
  33. ans = max(ans, sum);///记录最优解
  34. }
  35. if(i >= 100 && v[i])
  36. break;
  37. }
  38. printf("%lld\n", ans);
  39. }
  40. return 0;
  41. }
  42. /***************************************************
  43. User name:
  44. Result: Accepted
  45. Take time: 8ms
  46. Take Memory: 216KB
  47. Submit time: 2017-05-13 17:01:13
  48. ****************************************************/

发表评论

表情:
评论列表 (有 0 条评论,255人围观)

还没有评论,来说两句吧...

相关阅读

    相关 借助排序思想完成的一道题

    问题: 数组排序之后的相邻数的最大差值; 嗯,你可以排序,然后找相邻的最大差值。 但是你觉得这么简单我写他干啥。 最优解:时间复杂度O(N),空间O(1) 那我们开始