暑假训练1贪心之拼木板

Problem Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N

JOHN想在它的牧场周围修建篱笆,经测量他需要块木板,每块都有一个整型长度单元。

20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust

接着他购买了一个单个的长木板可以满足切割成所需木板,他要忽略锯木板丢失的木屑长度。

when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

但是他没有锯,他去问他邻居有没有锯

Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

他邻居的收费标准是锯多长的木板给多少钱

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

计算他要花的钱的最小数目

Input

Line 1: One integer N, the number of planks< br>Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

Sample Input

3 8 5 8

Sample Output

34

首先来翻译这道题,如上。

我的代码:

#include
using namespace std;
int i,n,s,l[1000000];
long long ans=0;
bool cmp(int a,int b)
{
    return a }
int main()
{
    cin>>n;
    for(i=1;i<=n;i++)
    {
       cin>>l[i];
       ans+=l[i];
    }
    sort(l+1,l+n+1,cmp);
    int k=1;
    n--;
    cout<     while(n>1)//终止条件
    {
        ans+=(l[k]+l[k+1]);
        n--;
        cout<         k+=2;
    }
    cout< }

如果这个题正向思维考虑怎么锯的话会很难很麻烦,但是从结果入手,不断拼凑木板长度,就可以利用贪心思想简单算出;

遇到难以思考的问题可以从结论入手,从结论入手来满足条件

你可能感兴趣的:(暑假训练1贪心之拼木板)