POJ3253---切木板(优先队列,贪心)

#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
    long long int ans = 0;
    priority_queue<int, vector<int>, greater<int>>que;
    int N; cin >> N;
    while (N--)
    {
        int x;
        cin >> x;
        que.push(x);
    }
    while (que.size() > 1)
    {
        int l1, l2;
        l1 = que.top(); que.pop();
        l2 = que.top(); que.pop();
        ans += l1 + l2;
        que.push(l1 + l2);
    }
    cout << ans;
    system("pause");
}

你可能感兴趣的:(贪心算法)