【hihoCoder 1416 --- Inventory is Full】优先队列

【hihoCoder 1416 --- Inventory is Full】优先队列

题目来源:点击进入【hihoCoder 1416 — Inventory is Full】

Description

You’re playing your favorite RPG and your character has just found a room full of treasures.

You have N inventory slots. Luckily, items of the same type stack together, with the maximum size of the stack depending on the type (e.g. coins might stack to 10, diamonds to 5, armor to 1, etc.). Each stack (or partial stack) takes up 1 inventory slot. Each item has a selling value (e.g. a single diamond might be worth 10, so a stack of 5 diamonds would be worth 50). You want to maximize the total selling value of the items in your inventory. Write a program to work it out.

Input

The first line contains 2 integers N and M denoting the number of inventory slots and the number of unique item types in the room.

The second line contains M integers, A1, A2, … AM, denoting the amounts of each type.
The third line contains M integers, P1, P2, … PM, denoting the selling value per item of each type.
The fourth line contains M integers, S1, S2, … SM, denoting the maximum stack size of each type.

For 80% of the data: 1 <= Ai <= 100000
For 100% of the data: 1 <= N, M <= 10000 1 <= Ai <= 1000000000 1 <= Pi <= 1000000 1 <= Si <= 100

Output

The maximum total selling value.

Sample Input

5 10
10 10 10 10 10 10 10 10 10 10
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1

Sample Output

146

解题思路

本题可以直接使用优先队列,定义优先规则即可。

或者采用贪心的思想也可以,将所有情况和次数标记出来进行排序。

AC代码(C++):

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e4+5;
const int MOD = 1e9+7;

struct Node{
    int a,p,s;
    ll sum;
    bool operator<(const Node &a)const {
        return sum<a.sum;
    }
}node[MAXN];
priority_queue<Node> q;

int main(){
    SIS;
    int n,m;
    cin >> n >> m;
    for(int i=0;i<m;i++) cin >> node[i].a;
    for(int i=0;i<m;i++) cin >> node[i].p;
    for(int i=0;i<m;i++) {
        cin >> node[i].s;
        node[i].sum=min(node[i].a,node[i].s)*node[i].p;
        q.push(node[i]);
    }
    ll ans=0;
    while(!q.empty() && n--){
        Node nd = q.top();
        q.pop();
        ans+=nd.sum;
        nd.a-=min(nd.a,nd.s);
        nd.sum=min(nd.a,nd.s)*nd.p;
        q.push(nd);
    }
    cout << ans << endl;
    return 0;
}

你可能感兴趣的:(ACM,hihoCoder1416,Inventory,is,Fu)