【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 <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章