zoukankan      html  css  js  c++  java
  • UVA-10954 Add All

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1895

    题目大意:类似于这道题UESTC - 1599 

    就是将所有的数字最后合并为一个数字,但是每次合并需要的消耗值是等于两个数的和,求最小的消耗值。

    当然是贪心,每次排序后合并最小的两个值,再题弹出那两个值,压入两值之和。再继续刚才的步骤,直到只要一个了。

    用sort肯定是超时的,会自动排序的容器当然是最好的选择。用multiset就行。

    AC代码:

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<set>
     4 using namespace std;
     5 int main()
     6 {
     7     int n;
     8     while(cin>>n)
     9     {
    10         if(n==0)break;
    11         multiset<int> s;
    12         multiset<int>::iterator it;
    13         int x;
    14         for(int i=0;i<n;i++)
    15         {
    16             scanf("%d",&x);
    17             s.insert(x);
    18         }
    19         long long int body=0;
    20         int t;
    21         while(s.size()!=1)
    22         {
    23             it=s.begin();
    24             body+=(*it);
    25             t=*it;
    26             s.erase(it);
    27             it=s.begin();
    28             body+=(*it);
    29             t+=(*it);
    30             s.erase(it);
    31             s.insert(t);
    32         }
    33         cout<<body<<endl;
    34     }
    35     return 0;
    36 }
  • 相关阅读:
    IOS UITableView 加载图片 卡的问题
    泛型
    正则表达式
    CSS 实现圆角表格
    CSS 横向滚动广告
    WebService
    CSS 弹出层 支持IE/FF/OP
    XML2DT DT2XML
    HttpWebRequest 调用 WebService 返回 xml
    CSS 自由伸展的三栏式版面
  • 原文地址:https://www.cnblogs.com/ISGuXing/p/7248016.html
Copyright © 2011-2022 走看看