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

用什么堆啊,用什么stl啊,队列搞定

Posted by sideman at 2010-06-05 22:55:54 on Problem 3253
#include<cstdio>
#include<cstdlib>
using namespace std;
int cmp(const void*a,const void*b)
{
   return *(int*)a-*(int*)b;
}
int main()
{
    unsigned __int64 total=0;
    int qx[20001],hx,tx;
    int qy[20001],hy,ty;
    int n;
    int i,j;
    int t1,t2;
    scanf("%d",&n);
    for(i=1;i<=n;i++) scanf("%d",&qx[i]);
    hx=1; tx=n;
    hy=1; ty=0;
    qsort(qx+1,n,sizeof(int),cmp);
    for(i=1;i<=n-1;i++)
    {
       if(hy>ty)
       {
          t1=qx[hx];
          hx++;
       }
       else if(hx>tx)
       {
          t1=qy[hy];
          hy++;
       }
       else if(qx[hx]>qy[hy])
       {
          t1=qy[hy];
          hy++;
       }
       else
       {
          t1=qx[hx];
          hx++;
       }
       ///
       if(hy>ty)
       {
          t2=qx[hx];
          hx++;
       }
       else if(hx>tx)
       {
          t2=qy[hy];
          hy++;
       }
       else if(qx[hx]>qy[hy])
       {
          t2=qy[hy];
          hy++;
       }
       else
       {
          t2=qx[hx];
          hx++;
       }
       ty++;
       qy[ty]=t1+t2;
       total+=t1+t2;
      // for(j=hx;j<=tx;j++)
//        printf("%d ",qx[j]); puts("");
//        for(j=hy;j<=ty;j++)
//         printf("%d ",qy[j]);puts("");
//         printf("%I64d %d %d %d %d %d/////////////\n",total,hx,tx,hy,ty,is);
    }
    printf("%I64d",total);
    getchar();
    getchar();
    getchar();
    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