Patches (思维+搜索)

Patches
Carlos is very concerned with the environment. Whenever possible, he tries to use less polluting
means of transport. He recently got a job close to home and is now using his bike to go to work.
Unfortunately, in the route between his home and his job there is a nail factory, and often some
nails fall from their trucks, and end up puncturing Carlos’ bike tires. Therefore he ends up having to
make several patches on the tires of his bike.
To make the repairs, Carlos uses two different types of patches. Both types are as wide as a bike
tire, but differ in length. As the cost of the patch is proportional to its length, Carlos is trying to find
a way to save money, using the least possible length of patches to make the repairs, without cutting
the patches.
The first step in repairing a tire is making a chalk mark on a position of the tire and then writing
down the distances, measured clockwise, of each of the holes in relation to the chalk mark. Each hole
must be completely covered by a patch. Carl˜ao would like your help to determine, given the positions
of the holes, the most economic way to make the repair.
Input
The input contains two lines. The first line contains four integers N, C, T1 e T2. Integer N indicates
the number of holes in the tire, and C indicates the cirunference length of the tire, in centimeters.
The lengths of the patches in centimeters are given by integers T1 and T2. The second line contains
N integers Fi
, representing the distance, in clockwise direction, from the chalk mark to hole i, in
centimeters.
Output
Your program must print a single line, containing a single integer, the smallest total length of patches
needed to make all the repairs.
Restrictions
• 1 ≤ N ≤ 1000
• 1 ≤ C ≤ 106
• 1 ≤ T1, T2 ≤ C
• 0 ≤ Fi ≤ C − 1, 1 ≤ i ≤ N
• If the distance between two holes is exactly k centimeters, a patch of length k centimeters covers
both holes.
Examples
Input
5 20 2 3
2 5 8 11 15
Output
8
Input
4 20 12 9
1 2 3 13
Output
12

题目给出的情形是环形的,不易处理。可将最大边切除,转化为线性在暴力搜索就可以。
主要要想清为啥去除最大边可以使答案最优。

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int a[N], dp[N];
int n, m, x, y, k;
int dfs(int c) {
    if (c >= k) return 0;
    if (dp[c] != -1) return dp[c];
    int ans = 0, j;
    for (j = c + 1; j < k && a[j] - a[c] <= x; j++)
        ;
    ans = x + dfs(j);
    for (j = c + 1; j < k && a[j] - a[c] <= y; j++)
        ;
    ans = min(ans, y + dfs(j));
    return dp[c] = ans;
}
int main() {
    cin >> n >> m >> x >> y;
    memset(dp, -1, sizeof(dp));
    if (x > y) swap(x, y);
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
        a[n + i] = m + a[i];
    }
    int ans = 0, b = m - a[n] + a[1], c = 1;
    for (int i = 2; i <= n; i++) {
        if (b < a[i] - a[i - 1]) {
            b = a[i] - a[i - 1];
            c = i;
        }
    }
    k = n + c;
    cout << dfs(c) << "\n";
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值