您好,欢迎来到飒榕旅游知识分享网。
搜索
您的当前位置:首页Collecting Stamps 3(区间dp)

Collecting Stamps 3(区间dp)

来源:飒榕旅游知识分享网

Collecting Stamps 3(区间dp)

题目描述
Republic of IOI, where JOI-kun lives, is famous for a large lake. Today, a stamp rally event takes place around the lake.
There are N types of stamps situated around the lake. These stamps are numbered from 1 to N, in clockwise order. The perimeter of the lake is L, and the i-th stamp (1 ≤ i ≤ N) is located at Xi meters clockwise from the starting point of the stamp rally.
Each participant stands at the starting point of the stamp rally. After the rally starts, each participant can move around the lake, both clockwise and counter-clockwise. Each participant can collect the i-th stamp (1 ≤ i ≤ N) only if they arrive at where the i-th stamp is located within Ti seconds (inclusive) since the rally starts.
JOI-kun is a participant of the stamp rally. He takes 1 second to move 1 meter. You can ignore all the other times which he takes.
Write a program that, given the number of types of stamps, the perimeter of the lake, where each stamp is located, and the time until which JOI-kun can collect each stamp, calculates the maximum number of types of stamps he can collect in total.
输入
Read the following data from the standard input. Given values are all integers.
N L
X1 . . . XN
T1 . . . TN

Constraints
• 1 ≤ N ≤ 200.
• 2 ≤ L ≤ 1 000 000 000.
• 1 ≤ Xi < L (1 ≤ i ≤ N).
• Xi < Xi + 1 (1 ≤ i ≤ N − 1).
• 0 ≤ Ti ≤ 1 000 000 000 (1 ≤ i ≤ N).

输出
Write the answer in one line to the standard output.
样例输入 Copy
【样例1】
6 25
3 4 7 17 21 23
11 7 17 10 8 10
【样例2】
5 20
4 5 8 13 17
18 23 15 7 10
【样例3】
4 19
3 7 12 14
2 0 5 4
【样例4】
10 87
9 23 33 38 42 44 45 62 67 78
15 91 7 27 31 53 12 91 89 46
样例输出 Copy
【样例1】
4
【样例2】
5
【样例3】
0
【样例4】
5

提示

样例1解释
JOI-kun can collect 4 types of stamps as described below:
1. Walk 2 meters counter-clockwise. He can collect the 6th stamp as it is 2 minutes since the rally starts.
2. Walk 2 meters counter-clockwise. He can collect the 5th stamp as it is 4 minutes since the rally starts.
3. Walk 7 meters clockwise. He can collect the 1st stamp as it is 11 minutes since the rally starts.
4. Walk 1 meter clockwise. He cannot collect the 2nd stamp as it is 12 minutes since the rally starts.
5. Walk 3 meters clockwise. He can collect the 3rd stamp as it is 15 minutes since the rally starts.
It is impossible for JOI-kun to collect 5 or more stamps, so the answer is 4.
样例2解释
JOI-kun can collect all the stamps by walking around the lake counter-clockwise.
样例3解释

Unfortunately, JOI-kun cannot collect any stamps, no matter how he moves.

思路:区间dp。dp[i][j][k][l]表示i-j区间,k为0,表示往左,k为1表示往右。l表示答案,dp值表示达到这个状态的最小时间。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
 
using namespace std;
   
#define rep(i , a , b) for(register int i=(a);i<=(b);i++)
#define per(i , a , b) for(register int i=(a);i>=(b);i--)
#define ms(s) memset(s, 0, sizeof(s))
   
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int , int> pi;
typedef unordered_map<int,int> un_map;
template<class T>
inline void read (T &x) {
    x = 0;
    int sign = 1;
    char c = getchar ();
    while (c < '0' || c > '9') {
        if ( c == '-' ) sign = - 1;
        c = getchar ();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar ();
    }
    x = x * sign;
}
   
const int maxn = 1e6+10;
const int inf = 0x3f3f3f3f;
const ll INF = ll(1e18);
const int mod = 1e9+7;
const double PI = acos(-1);
 
   
 
ll n,len;
ll x[222],t[222];
ll dp[222][222][2][222];
 
 
void init() {
    rep(i,0,n+1) {
        rep(j,0,n+1) {
            rep(k,0,1) {
                rep(l,0,n) {
                    dp[i][j][k][l]=INF;
                }
            }
        }
    }
}
int main(int argc, char * argv[])
{
    read(n);read(len);
    rep(i,1,n) read(x[i]);
    rep(i,1,n) read(t[i]);
    x[0]=0;x[n+1]=len;
    init();
    dp[0][n+1][0][0]=0;
    dp[0][n+1][1][0]=0;
    rep(i,0,n-1) {
        per(j,n+1,i+2) {
            rep(l,0,n) {
                ll cost = min(dp[i][j][0][l]+x[i+1]-x[i],dp[i][j][1][l]+len-x[j]+x[i+1]);
                dp[i+1][j][0][l+(cost<=t[i+1])]=min(dp[i+1][j][0][l+(cost<=t[i+1])],cost);
                cost = min(dp[i][j][0][l]+len-x[j-1]+x[i],dp[i][j][1][l]+x[j]-x[j-1]);
                dp[i][j-1][1][l+(cost<=t[j-1])]=min(dp[i][j-1][1][l+(cost<=t[j-1])],cost);
            }
        }
    }
 
    int ans = 0;
    per(l,n,0) {
        rep(i,0,n+1) {
            rep(j,0,n+1) {
                rep(k,0,1) {
                    if(dp[i][j][k][l]!=INF) {
                        printf("%d\n",l);
                        return 0;
                    }
                }
            }
        }
    }
    return 0;
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- sarr.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务