2558. 【NOIP2011模拟9.9】过河问题 (StandardIO)

    xiaoxiao2026-09-21  0

    2558. 【NOIP2011模拟9.9】过河问题 (StandardIO)

    Description

      在一个大晴天,Oliver与同学们一共N人出游,他们走到一条河的东岸边,想要过河到西岸。而东岸有一条小船。   船太小了,一次只能乘坐两人。每个人都有一个渡河时间T,船划到对岸的时间等于船上渡河时间较长的人所用时间。   现在已知N个人的渡河时间T,Oliver想要你告诉他,他们最少要花费多少时间,才能使所有人都过河。   注意,只有船在东岸(西岸)时东岸(西岸)的人才能坐上船划到对岸。

    Input

      输入文件第一行为人数N,以下有N行,每行一个数。   第i+1行的数为第i个人的渡河时间。

    Output

      输出文件仅包含一个数,表示所有人都渡过河的最少渡河时间。

    Sample Input

    4

    6

    7

    10

    15

    Sample Output

    42

    Hint

    【样例解释】   初始:东岸{1,2,3,4},西岸{}   第一次:东岸{3,4},西岸{1,2} 时间7   第二次:东岸{1,3,4},西岸{2} 时间6   第三次:东岸{1},西岸{2,3,4} 时间15   第四次:东岸{1,2},西岸{3,4} 时间7   第五次:东岸{},西岸{1,2,3,4} 时间7   所以总时间为7+6+15+7+7=42,没有比这个更优的方案。 【数据范围】   对于40%的数据满足N<=8   对于100%的数据满足N<=100000。

     

    思路就是用最短的带最大的,和第二大的,或者用前两小的过河,留一个下来,作为最大两个的回程船夫,比较哪个最好,就用哪个

    const maxn=1000000; var a:array [1..maxn] of longint; i,j,n,ans:longint; procedure qsort(l,r:longint); var i,j,mid,temp:longint; begin i:=l; j:=r; mid:=a[(i+j) div 2]; while i<j do begin while a[i]<mid do inc(i); while a[j]>mid do dec(j); if i<=j then begin temp:=a[i]; a[i]:=a[j]; a[j]:=temp; inc(i); dec(j); end; end; if l<j then qsort(l,j); if r>i then qsort(i,r); end; function cmax(x,y:longint):longint; begin if x>y then exit(y) else exit(x); end; begin readln(n); for i:=1 to n do readln(a[i]); qsort(1,n); i:=n; while i>3 do begin i:=i-2; ans:=ans+cmax(a[i+2]+a[2]+a[1]+a[2],a[1]+a[1]+a[i+1]+a[i+2]); end; if i=1 then ans:=ans+a[1]; if i=2 then ans:=ans+a[2]; if i=3 then ans:=ans+a[2]+a[1]+a[3]; writeln(ans); end.

    转载请注明原文地址: https://ju.6miu.com/read-1312197.html
    最新回复(0)