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

并没有用优先队列。暴力居然过了!(附代码)

Posted by oneanother at 2015-04-05 22:22:22 on Problem 3253
看大牛们都在用优先队列,感到十分疑惑
貌似像我这样暴力也可以过诶
优先队列什么的,,还没学到的说。。。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int n;
int l[20010];

void solve()
{
    long long ans=0;
    while(n>1)
    {
        int min1=0,min2=1;
        if(l[min1]>l[min2]) swap(min1,min2);

        for(int i=2;i<n;i++)
        {
            if(l[i]<l[min1]) {min2=min1;min1=i;}
            else if(l[i]<l[min2]){min2=i;}
        }

        int t=l[min1]+l[min2];
        ans+=t;

        if(min1==n-1) swap(min1,min2);
        l[min1]=t;
        l[min2]=l[n-1];
        n--;
    }
    printf("%lld\n",ans);
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        {
            char c;
            scanf("%c%d",&c,&l[i]);
        }
    solve();
    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