Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

优先队列 AC +代码

Posted by wylqq312715289 at 2013-09-24 18:56:38 on Problem 3253
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct T{
   int x;
   const bool operator<(T a) const{
       return a.x<x;
   }
};
T a[20019];
int main(){
    int n;
    long long res;
    priority_queue<T> q;
    while(!q.empty()) q.pop();
    while(scanf("%d",&n)!=EOF){
         res = 0;
         for(int i = 0;i<n;i++)  {
               scanf("%d",&a[i].x);
               q.push(a[i]);
         }
         for(int i = 0;i<n-1;i++){
               T temp;
               T x = q.top();q.pop();
               T y = q.top();q.pop();
               res = res + x.x + y.x;
               temp.x = x.x + y.x;
               q.push(temp);
         }
         while(!q.empty()) q.pop();
         cout<<res<<endl;
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator