Greedy - Consolidated stones

7-8 combined stone (20 minutes) were combined stones

There are n heap of stones, we want to combine them into a pile. Each combined, two piles can stone (may not be adjacent) were combined together, equal to the weight of the consumed physical and piles of stones.
As can be seen, all of the gravel after merging n-1 times, on the left a pile. When combined total consumption stones combined physical strength is equal to the sum of each consumed.

We want to save energy as much as possible when combining stones. Each assumed that the number of stones 1 are by weight, and a known number of stones per stack heap of stones, a minimum physical requirements consumed.

Input formats:

A total of two lines. The first row is an integer n (1≤n≤100000), represents the heap of stones.

The second line contains n integers, separated by spaces, the integer i ai (0≤ai≤100000) i is the number of stacks of stones.

Output formats:

An integer, i.e. the value of the minimum physical cost.

Sample input:

3 1 2 3

Sample output:

9

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<vector>
#include<string>
#define M 100010
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n;
int main(){
    cin >> n;
    priority_queue<int,vector<int>,greater<int> >q;
    int t;
    while(n--){
        scanf("%d",&t);
        q.push(t);
    }
    ll ans = 0;
    while(q.size()>1){
        ll now = 0;
        now += q.top(); q.pop();
        now += q.top(); q.pop();
        q.push(now);
        ans += now;
    }
    cout << ans << endl;
    return 0;
}

Their original idea: Let's lightest mobile number up to
EG
1 2 3
, respectively, to be moved 221 times
Error: The idea is to start with the lightest start, fixed target stack, followed by being placed in

eg 2 2 3 3
this approach:
(2,2) 3 3 - (4), 3,3 ---- 4
(4,3) 3 - (7,3) ---- 7
(7, 3) --10
Total 21

But in fact the least: each time the dynamic lightest out
(2,2) 3,3--3,3,4 ---- 4
(3,3) 4--4,6 ---- 6
(4,6) ---- 10
total 20

int n;
int a[M];
int main(){
    cin >> n;
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    ll ans = 0;
    for(int i=0; i<n; i++)
        ans += a[i]*(n-i);
    ans -= a[0];
    cout << ans << endl;
    return 0;
}

Merge stones 2

There are n heap of stones, we want to combine them into a pile. Each combined, only the piles of adjacent stones merged together, consume physical strength equal to the product of the weight of the stone piles.
As can be seen, all of the gravel after merging n-1 times, on the left a pile. When combined total consumption stones combined physical strength is equal to the sum of each consumed.

We want to waste strength as much as possible when combining stones. Each assumed that the number of stones 1 are by weight, and a known number of stones per stack heap of stones, most seeking physical consumed.

Input formats:

A total of two lines. The first row is an integer n (1≤n≤100000), represents the heap of stones.

The second line contains n integers, separated by spaces, the integer i ai (0≤ai≤10000) i is the number of stacks of stones.

Data ensure that the final answer within the Long Range.

Output formats:

An integer, which is the most cost physical value.

Sample input:

3 1 2 3

Sample output:

11

To waste time physically merge stones as possible
before the merger or the first light of the combined weight:
EG
1 2 3 10
3,3,10--2
6,10--9
16--60--71

1,2,13——30
1,15——26
16——15——71

Found: No matter how the merger are the same:
reason:
EG abc d ----- AC + AD + ab + bc + bd + cd
every pebble stones are multiplied by all except their own, so no matter how the merger are the same

Enter the operation over to the side

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<vector>
#include<string>
#define M 100010
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int main(){
    ll n,now,pre=0;
    ll ans = 0;
    cin >> n;
    while(n--){
        scanf("%lld",&now);
        ans += now*pre;
        pre += now;
    }
    cout << ans << endl;
    return 0;
}

Published 62 original articles · won praise 0 · Views 1743

Guess you like

Origin blog.csdn.net/jhckii/article/details/104457749