Luo river valley P1809 Problem Solution

Face questions

This question is a good question greedy + DP's:

First sort is a must to do.

Then we divide circumstances:

1. If a person left, let's come back to pick him up minimum

2. If two people left, let's come back for the minimum, and the remaining two men (ie, the maximum of two people) in the past, so little time back, the smallest in the past two

The above two methods must be optimal, because the greatest man to let the smallest of delivery, or else with a second large;

 

The meaning of the above equation is converted DP:

1.f[i]=f[i-1]+a[1]+a[n];

2.f[i]=f[i-2]+a[1]+a[i-1]+a[2]+a[2];

 

Also pay attention to deal with the border;

#include <bits/stdc++.h>
#define inc(a,b,c) for(register int i=a;i<=b;i+=c)
#define ini 100010
int a[ini],f[ini];
using namespace std;
int main()
{
    int n; cin>>n;
    inc(1,n,1) scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    f[1]=a[1];
    f[2]=a[2];
    inc(3,n,1){
        f[i]=min(f[i-1]+a[1]+a[i],f[i-2]+2*a[2]+a[i]+a[1]);
    }
    cout<<f[n];
}

 

Guess you like

Origin www.cnblogs.com/kamimxr/p/11563445.html