Poj 1339 poker card game (哈夫曼树)

poker card game
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 798 Accepted: 253

Description

Suppose you are given many poker cards. As you have already known, each card has points ranging from 1 to 13. Using these poker cards, you need to play a game on the cardboard in Figure 1. The game begins with a place called START. From START, you can walk to left or right to a rectangular box. Each box is labeled with an integer, which is the distance to START. 

 


Figure 1: The poker card game cardboard. 

To place poker cards on these boxes, you must follow the rules below: (1) If you put a card with n points on a box labeled i , you got (n ∗ i) points. (2) Once you place a card on a box b, you block the paths to the boxes behind b. For example, in Figure 2, a player places a queen on the right box of distance 1, he gets 1 ∗ 12 points but the queen also blocks the paths to boxes behind it; i.e., it is not allowed to put cards on boxes behind it anymore. 


 
Figure 2: Placing a queen. 

Your goal: Given a number of poker cards, find a way to place them so that you will get the minimum points. For example, suppose you have 3 cards 5, 10, and K. To get the minimum points, you can place cards like Figure 3, where the total points are 1 * 13 + 2 * 5 + 2 * 10 = 43. 


 
Figure 3: An example to place cards. 

Input

The first line of the input file contains an integer n, n <= 10, which represents the number of test cases. In each test case, it begins with an integer m, m <= 100000, 

which represents the number of poker cards. Next, each card represented by its number are listed consecutively. Note that, the numbers of ace, 2, 3, ..., K are given by integers 1, 2, 3, ..., 13, respectively. The final minimum point in each test case is less than 5000000. 

Output

List the minimum points of each test case line by line.

Sample Input

3
3
5 10 13
4
3 4 5 5
5
7 7 10 11 13

Sample Output

43
34
110
 
思路:
一个简单的哈夫曼编码求最小值的问题,10的6次方的数据直接暴力写O(n^2)肯定超时,用优先队列优化下 复杂度为O(nlogn)
代码如下:
 
#include <queue>
#include <stdio.h>
using namespace std;
priority_queue<int,vector<int>,greater<int>>Q;//最小堆
int main()
{
    int T;
    scanf("%d",&T);
    int n;
    while(T--)
    {
        scanf("%d",&n);
        while(Q.empty()==false) 
            Q.pop();
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);//每个节点的权值
            Q.push(x);
        }
        int ans=0 ;//路径初始化为0
        while(Q.size()>1)
        {
            int a=Q.top();
            Q.pop();
            int b=Q.top();
            Q.pop();
            ans=ans+a+b;
            Q.push(a+b);
        }
        printf("%d\n",ans);
    }
    return 0;
}



                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值