CodeForces - 705D (17/600)

Scott Lang is at war with Darren Cross. There are n chairs in a hall where they are, numbered with 1, 2, …, n from left to right. The i-th chair is located at coordinate xi. Scott is on chair number s and Cross is on chair number e. Scott can jump to all other chairs (not only neighboring chairs). He wants to start at his position (chair number s), visit each chair exactly once and end up on chair number e with Cross.

As we all know, Scott can shrink or grow big (grow big only to his normal size), so at any moment of time he can be either small or large (normal). The thing is, he can only shrink or grow big while being on a chair (not in the air while jumping to another chair). Jumping takes time, but shrinking and growing big takes no time. Jumping from chair number i to chair number j takes |xi - xj| seconds. Also, jumping off a chair and landing on a chair takes extra amount of time.

If Scott wants to jump to a chair on his left, he can only be small, and if he wants to jump to a chair on his right he should be large.

Jumping off the i-th chair takes:

ci extra seconds if he’s small.
di extra seconds otherwise (he’s large).
Also, landing on i-th chair takes:

bi extra seconds if he’s small.
ai extra seconds otherwise (he’s large).
In simpler words, jumping from i-th chair to j-th chair takes exactly:

|xi - xj| + ci + bj seconds if j < i.
|xi - xj| + di + aj seconds otherwise (j > i).
Given values of x, a, b, c, d find the minimum time Scott can get to Cross, assuming he wants to visit each chair exactly once.

Input
The first line of the input contains three integers n, s and e (2 ≤ n ≤ 5000, 1 ≤ s, e ≤ n, s ≠ e) — the total number of chairs, starting and ending positions of Scott.

The second line contains n integers x1, x2, …, xn (1 ≤ x1 < x2 < … < xn ≤ 109).

The third line contains n integers a1, a2, …, an (1 ≤ a1, a2, …, an ≤ 109).

The fourth line contains n integers b1, b2, …, bn (1 ≤ b1, b2, …, bn ≤ 109).

The fifth line contains n integers c1, c2, …, cn (1 ≤ c1, c2, …, cn ≤ 109).

The sixth line contains n integers d1, d2, …, dn (1 ≤ d1, d2, …, dn ≤ 109).

Output
Print the minimum amount of time Scott needs to get to the Cross while visiting each chair exactly once.

Example
Input
7 4 3
8 11 12 16 17 18 20
17 16 20 2 20 5 13
17 8 8 16 12 15 13
12 4 16 4 15 7 6
8 14 2 11 17 12 8
Output
139
Note
In the sample testcase, an optimal solution would be . Spent time would be 17 + 24 + 23 + 20 + 33 + 22 = 139.

这个题确实不会….
没想到这种插入的思维
说到底还是对组合理解的不深
小变形可以这种大变形就gg
这个我只能想到全排。。

其实这种插入就是 一种组合。。
差不多也就是全排了
只不过他对每次的当前步骤做出了选择

因为每一个点吃枣要进来
进来就和所有的比了
新进来的也一样
因此它是贪心的没毛病

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
#include<set>
using namespace std;

long long ditu[7000];
long long dd[5001][5001];
long long tu[5001][6];
int main()
{
    long long n, s, t;
    cin >> n >> s >> t;
    for (long long a = 1; a <= n; a++)scanf("%I64d", &tu[a][1]);
    for (long long a = 1; a <= n; a++)scanf("%I64d", &tu[a][2]);
    for (long long a = 1; a <= n; a++)scanf("%I64d", &tu[a][3]);
    for (long long a = 1; a <= n; a++)scanf("%I64d", &tu[a][4]);
    for (long long a = 1; a <= n; a++)scanf("%I64d", &tu[a][5]);
    for (long long a = 1; a <= n; a++)
    {
        for (long long b = 1; b <= n; b++)
        {
            if (a == b)continue;
            if (tu[a][1] > tu[b][1])dd[a][b] = abs(tu[a][1] - tu[b][1]) + tu[a][4] + tu[b][3];
            else dd[a][b] = abs(tu[a][1] - tu[b][1]) + tu[a][5] + tu[b][2];
        }
    }
    long long lu = dd[s][t];
    ditu[1] = s, ditu[2] = t;
    long long a = 0;
    for (long long c=1; c<=n; c++)
    {
        if (c == s || c == t)continue;
        a++;
        long long ss = 1e18;
        long long weizhi;
        for (long long b = 1; b <= a; b++)
        {
            if (ss > dd[ditu[b]][c] + dd[c][ditu[b + 1]]-dd[ditu[b]][ditu[b+1]])
            {
                weizhi = b;
                ss = dd[ditu[b]][c] + dd[c][ditu[b + 1]] - dd[ditu[b]][ditu[b + 1]];
            }
        }
        lu += ss;
        for (long long b = a + 2; b > weizhi+1; b--)ditu[b] = ditu[b - 1];
        ditu[weizhi + 1] = c;
    }
    cout << lu;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值