bzoj 2697 贪心

就贪心就行了,首先可以看成n个格子,放物品,那么

一个物品假设放3个,放在1,k,n处,那么价值和放在1,n

是一样的,所以一个物品只放两个就行了,价值大的应该尽量放

在两边,那么排序之后模拟就行了

/**************************************************************
    Problem: 2697
    User: BLADEVIL
    Language: Pascal
    Result: Accepted
    Time:0 ms
    Memory:228 kb
****************************************************************/
 
//By BLADEVIL
var
    n, k                        :longint;
    i                           :longint;
    a                           :array[0..300] of longint;
    ans                         :longint;
     
procedure swap(var a,b:longint);
var
    c                           :longint;
begin
    c:=a; a:=b; b:=c;
end;
     
procedure qs(low,high:longint);
var
    i, j                        :longint;
    xx                          :longint;
begin
    i:=low; j:=high;
    xx:=a[(i+j) div 2];
    while i<j do
    begin
        while a[i]>xx do inc(i);
        while a[j]<xx do dec(j);
        if i<=j then
        begin
            swap(a[i],a[j]);
            inc(i); dec(j);
        end;
    end;
    if i<high then qs(i,high);
    if j>low then qs(low,j);
end;
     
     
begin
    read(n,k);
    for i:=1 to k do read(a[i]);
    qs(1,k);
    ans:=0;
    for i:=1 to k do
    begin
        if n<=1 then break;
        ans:=ans+(n-1)*a[i];
        dec(n,2);
    end;
    writeln(ans);
end.

 

posted on 2014-01-07 09:48  BLADEVIL  阅读(170)  评论(0编辑  收藏  举报