1117 Smart Carpenter

An old carpenter needs to cut a long wooden stick into N pieces. The length of each segment is L1, L2, ..., LN (1 <= L1, L2, ..., LN <= 1000, and all are integers) length units. We consider cutting only at integer points without loss of wood.
The carpenter found that the physical strength spent on each cut is proportional to the length of the stick, so it may take 1 unit of physical strength to cut a stick with a length of 1. For example: if N=3, L1 = 3, L2 = 4, L3 = 5, the original length of the stick is 12, and the carpenter can cut it in various ways, such as: first cut 12 into 3+9. It costs 12 stamina , then cut 9 into 4+5, spend 9 stamina, and spend a total of 21 stamina; you can also cut 12 into 4+8, spend 12 stamina, then cut 8 into 3+5, spend 8 stamina, and spend a total of 20 physical strength. Obviously, the latter is more energy efficient than the former.
So, how much physical effort does a carpenter have to spend at least to complete the cutting task?
Input
Line 1: 1 integer N (2 <= N <= 50000)
Lines 2 - N + 1: 1 integer Li per line (1 <= Li <= 1000).
Output
Output with minimal physical exertion.
Input example
3
3
4
5
Output example
19

I think it's easier to understand this question backwards. I will give you some sticks to connect them. You can only connect two sticks at a time (the stamina spent is the total length of the two sticks). The solution to changing the meaning of the question must be to connect the two shortest wooden sticks each time. After the connection is completed, put the connected wooden stick as a wooden stick and put it into all the wooden sticks, and then find the shortest two wooden sticks to connect, only After the last stick is left, getting the least amount of physical effort is the result we want.

Example: Three sticks: 3 4 5

First connection: 5, 7 Stamina cost: 7

Second connection: 12 Stamina cost: 12

Total cost 19;

The priority queue (priority_queue) is used in the following reference code

priority_queue< int > q; // int ascending priority queue
priority_queue <int, vector<int>, greater<int> > // int descending priority queue

If you want to know more about the priority queue, you can go to the information.

#include <iostream>
#include <vector>
#include <queue>

using namespace std;
intmain()
{
     priority_queue <int, vector<int>, greater<int> > Q;
     // Build a descending priority queue, that is, the head of the queue is the minimum value in the queue
     int n;
     cin >> n;
     int temp;
     for(int i=1; i<=n; i++) {
        cin >> temp;
        Q.push( temp );
     } // enter the queue


     long long ans = 0;
     int time = 0;
     while( !Q.empty() ){
        have = 0;
        has += Q.top();
        Q.pop();
        has += Q.top();
        Q.pop();
        // Continue to connect the smallest two sticks until the last one is left
        ans += has;
        if(Q.empty()) {
            break;
        }
        Q.push( tem );
     }
     cout << ans << endl;
     return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326078642&siteId=291194637