[BZOJ1724][Usaco2006 Nov]Fence Repair 切割木板

题目链接:

BZOJ1724.

思维神题。

直接按照题目去解很难想出思路(贪心?\(DP\)?)

其实,把整个切割过程倒过来,你就会发现,这不就是合并果子吗。。

用小根堆维护所有木板,每次贪心取最小两个木板合并。

时间复杂度 \(O(nlog_2n)\)

#include <queue>
#include <cstdio>
#include <vector>
#include <algorithm>

int n;
std::priority_queue<int,std::vector<int>,std::greater<int> > q;

int main()
{
    scanf("%d",&n);
    for(int i=1,l;i<=n;++i)
        scanf("%d",&l),q.push(l);
    long long Ans=0;
    for(int x,y;(int)q.size()>1;)
    {
        x=q.top(),q.pop();
        y=q.top(),q.pop();
        Ans+=x+y,q.push(x+y);
    }
    printf("%lld\n",Ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/LanrTabe/p/10211550.html