zoukankan      html  css  js  c++  java
  • Poj3253--Fence Repair、、南阳55--省懒事的小明(哈夫曼树)

    懒省事的小明

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:3
     
    描述
          小明很想吃果子,正好果园果子熟了。在果园里,小明已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。小明决定把所有的果子合成一堆。 因为小明比较懒,为了省力气,小明开始想点子了:
      每一次合并,小明可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n-1次合并之后,就只剩下一堆了。小明在合并果子时总共消耗的体力等于每次合并所耗体力之和。 
      因为还要花大力气把这些果子搬回家,所以小明在合并果子时要尽可能地节省体力。假定每个果子重量都为1,并且已知果子的种类数和每种果子的数目,你的任务是设计出合并的次序方案,使小明耗费的体力最少,并输出这个最小的体力耗费值。 
      例如有3种果子,数目依次为1,2,9。可以先将1、2堆合并,新堆数目为3,耗费体力为3。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为12,耗费体力为12。所以小明总共耗费体力=3+12=15。可以证明15为最小的体力耗费值。
     
    输入
    第一行输入整数N(0<N<=10)表示测试数据组数。接下来每组测试数据输入包括两行,第一行是一个整数n(1<=n<=12000),表示果子的种类数。第二行包含n个整数,用空格分隔,第i个整数ai(1<=ai<=20000)是第i种果子的数目。
    输出
    每组测试数据输出包括一行,这一行只包含一个整数,也就是最小的体力耗费值。
    样例输入
    1
    3 
    1 2 9
    样例输出
    15
    来源
    [hzyqazasdf]原创
    上传者
    hzyqazasdf
    //数组实现优先队列;
     1 #include <stdio.h>
     2 #include <algorithm>
     3 using namespace std;
     4 long long int num[12020];
     5 int main()
     6 {
     7     int t;
     8     scanf("%d",&t);
     9     while(t--)
    10     {
    11         int i, j, m;
    12         scanf("%d", &m);
    13         for(i=0; i<m; i++)
    14         scanf("%lld", &num[i]);
    15         sort(num, num+m);
    16         long long int total = 0;
    17         for(i=0; i<m; i++)
    18         {
    19             if(i == m-1)
    20             break;
    21             long long int temp = num[i]+num[i+1];
    22             num[i+1] = temp;
    23             total += temp;
    24             for(j=i+1; j < m; j++)
    25             {
    26                 if(num[j] < num[j-1])
    27                 {
    28                     long long int s;
    29                     s = num[j]; num[j] = num[j-1]; num[j-1]=s;                    
    30                 }
    31             }
    32         }
    33         printf("%lld
    ",total);
    34     }
    35     return 0;
    36 }
    View Code

    //STL;

     1 #include <queue>
     2 #include <cstdio>
     3 #include <iostream>
     4 using namespace std;
     5 int main()
     6 {
     7     int t;
     8     scanf("%d", &t);
     9     while(t--)
    10     {
    11         priority_queue<int, vector<int>, greater<int> >q;
    12         int i, m;
    13         scanf("%d", &m);
    14         for(i=0; i<m; i++)
    15         {
    16             int num;
    17             scanf("%d", &num);
    18             q.push(num);        
    19         }
    20         long long int total = 0;
    21         while(q.size()!=1)
    22         {
    23             long long int temp = q.top();
    24             q.pop();
    25             temp += q.top();
    26             q.pop();
    27             q.push(temp);
    28             total += temp;        
    29         }
    30         printf("%lld
    ",total);
    31     }
    32     return 0;
    33 }
    View Code
    Fence Repair
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 32357   Accepted: 10397

    Description

    Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li(1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

    FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

    Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

    Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

    Input

    Line 1: One integer N, the number of planks 
    Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

    Output

    Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

    Sample Input

    3
    8
    5
    8

    Sample Output

    34

    Hint

    He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8. 
    The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

    Source

     1 #include <queue>
     2 #include <cstdio>
     3 #include <iostream>
     4 using namespace std;
     5 int main()
     6 {
     7     int t, m;
     8     while(~scanf("%d", &t))
     9     {
    10         priority_queue <int, vector<int>, greater<int> >q;
    11         while(t--)
    12         {
    13             scanf("%d", &m);
    14             q.push(m);
    15         } 
    16         __int64 total = 0;
    17         while(q.size() != 1)
    18         {
    19             __int64 temp = q.top();
    20             q.pop();
    21             temp += q.top();
    22             q.pop();
    23             q.push(temp);
    24             total += temp;
    25         //    printf("%d
    ",temp);
    26         }
    27         printf("%I64d
    ", total);
    28     }
    29     return 0;
    30 }
    View Code
  • 相关阅读:
    远程、标签
    NUnit单元测试资料汇总
    jdk1.6下载页面
    javac: cannot execute binary file
    how to remove MouseListener / ActionListener on a JTextField
    Linux下chkconfig命令详解(转)
    如何让vnc控制由默认的twm界面改为gnome?(转)
    winzip15.0注冊码
    微服务的优缺点
    站点建设10个最好的响应的HTML5滑块插件
  • 原文地址:https://www.cnblogs.com/soTired/p/4684585.html
Copyright © 2011-2022 走看看