zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    nj07---npm
    nj06---包
    nj05---模块
    nj04---事件回调函数
    nj03---阻塞和线程
    nodejs02---demo
    nodejs简介
    【转贴】内存系列一:快速读懂内存条标签
    【转贴】4个你未必知道的内存小知识
    Linux上面mount 域控的目录 超时 然后提示 error的解决办法
  • 原文地址:https://www.cnblogs.com/BLADEVIL/p/3508247.html
Copyright © 2011-2022 走看看