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****************************************************/
0 0