山东省第八届ACM省赛J题company(C-DP,贪心)

来源:互联网 发布:php 页面 空格 编辑:程序博客网 时间:2024/06/08 11:20

company

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic Discuss

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 ival.
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 61 1 1 2

Example Output

51


题意:商店买东西,有n种商品,每种商品卖出去以后得到的价值是v,每种商品有c件,第i天卖出v价值的商品,得到的价值是i*v,时间是从第一天开始,每天只能卖一件,对于每件商品你可以选择卖或者不卖;

思路:把所有的物品存在一个序列里,从小到大排列,从最后往前计算,若卖此商品所得价值会使ans变小,则跳过此商品判断下一个,每卖出一个商品,已加入序列的商品价值之和sum需要加入一个val。

C:1、自己做的时候卡在了DP上,没有考虑到从后往前可以优化算法,没有来得及转换思维。

2、ans应为Long long型,自己设成了int.

#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <cstdlib>using namespace std;int val[1005], cnt;int v[100050];int main(){    //freopen("1.txt", "r", stdin);    int n;    while(~scanf("%d", &n))    {       memset(v, 0, sizeof(v));       memset(val, 0, sizeof(val));        long long t = 1;        for(int i = 1; i <= n; i++)            scanf("%d", &val[i]);        for(int i = 1; i <= n; i++)            {                scanf("%d", &cnt);                for(int j = t ; j < t + cnt; j ++)                    v[j] = val[i];                t += cnt;            }        t --;        sort(v+1, v + t+1);//        for(int i = 1; i <= t; i++)//        {//            cout << v[i] <<endl;//        }        long long  ans = 0;        long long sum = 0;        for(int i = t; i >= 1; i--)        {            if( v[i] + sum < 0)                continue;            else            {                ans += v[i] + sum;                sum += v[i];            }         //   cout << ans <<" " << sum <<endl;        }        cout << ans <<endl;    }    return 0;}




阅读全文
0 0