company——桶思想
来源:互联网 发布:淘宝退款在哪里看 编辑:程序博客网 时间:2024/05/16 17:03
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)
#include <bits/stdc++.h>using namespace std;int main(){ int n, i, j, k, tp, x; long long sum, ans; int a[104], v[204]; while(scanf("%d", &n) != EOF) { tp = 0; ans = 0; memset(v, 0, sizeof(v));///初始化桶思想数组 for(i = 0; i < n; i++) scanf("%d", &a[i]); for(i = 0; i < n; i++) { scanf("%d", &x); v[a[i]+100] += x;///桶思想存储负数下标 } for(i = 0; i <= 200; i++) { sum = 0, tp = 0;///初始化记录变量和累加变量 if(v[i]) { for(j = i; j <= 200; j++) { for(k = 0; k < v[j]; k++)///当前货物可能有多件 { tp++; sum += (j-100)*tp;///注意当前货物效益为j-100 } } ans = max(ans, sum);///记录最优解 } if(i >= 100 && v[i])///优化:提前结束条件(第一次效益为正获得的总效益一定比之后起始点出发获得的总效益要大) break; } printf("%lld\n", ans); } return 0;}/***************************************************User name: Result: Runtime ErrorTake time: 0msTake Memory: 0KBSubmit time: 2017-05-13 16:42:01****************************************************/
以下为Accepted代码
#include <bits/stdc++.h>using namespace std;int main(){ int n, i, j, k, tp, x; long long sum, ans; int a[1004], v[204]; while(scanf("%d", &n) != EOF) { tp = 0; ans = 0; memset(v, 0, sizeof(v));///初始化桶思想数组 for(i = 0; i < n; i++) scanf("%d", &a[i]); for(i = 0; i < n; i++) { scanf("%d", &x); v[a[i]+100] += x;///桶思想存储负数下标 } for(i = 0; i <= 200; i++) { sum = 0, tp = 0;///初始化记录变量和累加变量 if(v[i]) { for(j = i; j <= 200; j++) { for(k = 0; k < v[j]; k++)///当前货物可能有多件 { tp++; sum += (j-100)*tp;///注意当前货物效益为j-100 } } ans = max(ans, sum);///记录最优解 } if(i >= 100 && v[i]) break; } printf("%lld\n", ans); } return 0;}/***************************************************User name: Result: AcceptedTake time: 8msTake Memory: 216KBSubmit time: 2017-05-13 17:01:13****************************************************/
- company——桶思想
- POJ3216——Repairing Company
- poj1416——Shredding Company
- poj1416——Shredding Company(dfs)
- company
- company
- company
- company
- company
- LSM6DS3驱动编写——learn form sparkfun company
- 思考——思想
- 累加思想与计数器思想——诺诺"涂鸦"记忆
- 《JAVA编程思想》读书笔记1——OOP思想
- OC基础—面向对象思想(思想很重要)
- 释道儒——道家思想
- RankNet——基本思想
- 【自考】——思想篇
- ORM——基本思想
- mfc的控件与响应顺序
- Spring学习笔记之使用对象-关系映射持久化数据
- VS2015 使用GDI+ 和 easyX的安装
- 2017年华东师范大学网络赛 A
- KMP算法 Java实现
- company——桶思想
- 模块化开发
- JAVA String、StringBuilder和StringBuffer区别
- 树莓派笔记(二)--“闪烁灯”
- windows程序设计(二)
- Jquery UI实践系列(二)Accordion
- Watson Explorer 入门(12):词典
- 创建函数计算目标元素到浏览器窗口的距离
- 平方数