迁徙过程中的河流-线性dp

迁徙过程中的河流

思路

规定 f [ i ] f[i] f[i]为前i个人已经过河, a [ i ] , 表 示 第 i 个 人 所 用 时 间 a[i],表示第i个人所用时间 a[i],i
容易想到每次我们用时间消耗小的把船给开会去,这里先从小到大排序;
题目举例中给了我们一种转移方式,利用 f [ i − 2 ] f[i-2] f[i2]进行转移,首先先让第一个人回去,时间消耗为a[1],再让第i-1,i个人开船去对岸时间消耗为a[i],再让第2个人开船回去接第一个人去对岸消耗为2*a[2],总消耗为 f [ i − 2 ] + a [ 1 ] + a [ i ] + a [ 2 ] ∗ 2 f[i-2]+a[1]+a[i]+a[2]*2 f[i2]+a[1]+a[i]+a[2]2;
f [ i − 1 ] f[i-1] f[i1]转移为: f [ i − 1 ] + a [ 1 ] + a [ i ] f[i-1]+a[1]+a[i] f[i1]+a[1]+a[i]
状态转移方程为
f [ i ] = m i n ( f [ i − 1 ] + a [ 1 ] + a [ i ] , f [ i − 2 ] + a [ i ] + a [ 1 ] + a [ 2 ] ∗ 2 ) f[i] = min(f[i-1] + a[1] + a[i],f[i-2] + a[i]+ a[1]+ a[2]*2) f[i]=min(f[i1]+a[1]+a[i],f[i2]+a[i]+a[1]+a[2]2)

/*
    author:@bzdhxs
    date:2022/1/22
    URL:https://ac.nowcoder.com/acm/contest/24213#question
    线性dp;
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
template <typename T>
inline void read(T &s){s = 0;T w = 1, ch = getchar();while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }while (isdigit(ch))  { s = (s << 1) + (s << 3) + (ch ^48); ch = getchar();} s *= w;}
template <typename T>
inline void write(T s){if (s < 0) putchar('-'), s = -s;if (s > 9) write(s / 10);putchar(s % 10 + '0');}
#define int long long
#define _orz ios::sync_with_stdio(false),cin.tie(0)
#define mem(str,num) memset(str,num,sizeof(str))
#define forr(i,a,b) for(int i = a; i <= b;i++)
#define forn(i,n) for(int i = 0; i < n; i++)
#define dbg() cout <<"0k!"<< endl;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e5+10;
int f[N];
signed main()
{
    int n; cin >> n;
    vector<int> a(n+1,0);
    forr(i,1,n) cin >> a[i];
    sort(a.begin(),a.end());

    mem(f,inf);f[0] = 0;
    f[1] = a[1];
    f[2] = a[2];
    forr(i,3,n){
        f[i] = min(f[i-1] + a[1] + a[i],f[i-2] + a[i]+ a[1]+ a[2]*2);
    }
    cout << f[n] << endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值