当前位置: 代码迷 >> 综合 >> company[山东省第八届ACM大学生程序设计竞赛]
  详细解决方案

company[山东省第八届ACM大学生程序设计竞赛]

热度:16   发布时间:2023-11-22 14:12:31.0

题目链接
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≤vali≤100).

The third line contains n integers cnt1,cnt2,…,cntn(1≤cnti≤100).
Output

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

4
-1 -100 5 6
1 1 1 2
Sample Output

51
Hint

sell goods whose price with order as -1, 5, 6, 6, the total benefit would be -11 + 52 + 63 + 64 = 51.

解题报告:
1.将所有物品的价值按照从大到小进行排序
2.设置ans和sum变量,ans代表当前已经选择的物品的总价值(i * val),sum代表当前已经选择的物品的价值(val)
3.从前往后搜索,一开始一直不理解的难点在于第几天卖的不能确定,后来仔细看代码,巧妙的地方在于sum,只要满足条件sum就加一次(相当于把所有的物品往后推迟一天),一直到不满足条件为止,这样就不用专门统计天数了

#include<bits/stdc++.h>
using namespace std;
typedef struct Node{
    int value;int cnt;bool friend operator < (Node a,Node b){
    return a.value > b.value;}
}Node;
int main(){
    long long int n,sum,ans;//必须设置为long long类型Node p[1001];scanf("%lld",&n);memset(p,0,sizeof(p));for(int i = 0;i < n;i++)scanf("%d",&p[i].value);for(int i = 0;i < n;i++)scanf("%d",&p[i].cnt);sum = 0,ans = 0;sort(p,p + n);for(int i = 0;i < n;i++)for(int j = 0;j < p[i].cnt;j++)if((sum + ans + p[i].value) > ans){
    ans += sum + p[i].value;sum += p[i].value;}printf("%lld\n",ans);return 0;
}
  相关解决方案