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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
s is a fascinating hobby for many people, particularly those interested in studying insects and their behavior. However, it's crucial to follow ethical and legal guidelines, and to ensure that bugs are collected and handled humanely. Here are some tips for collecting bugs: 1. Check local regulations: Before you start collecting bugs, check your local regulations to make sure it is legal to do so. Some areas may have restrictions on collecting certain species or collecting bugs in protected areas. 2. Use ethical and humane methods: It's important to use ethical and humane methods when collecting bugs. Avoid using harmful chemicals, and instead use gentle methods such as nets or traps. Handle the bugs carefully and avoid injuring them. 3. Respect the environment: When collecting bugs, make sure to respect the environment and the other creatures that live there. Avoid damaging plants or other habitats, and leave the area as you found it. 4. Keep accurate records: Keep accurate records of the species you collect, when and where you collected them, and any other relevant information. This can be helpful for scientific research and for tracking changes in insect populations over time. 5. Release the bugs: After you have collected and observed the bugs, release them back into their natural habitat. Make sure to do so in a safe and appropriate location, and handle them carefully to avoid injury. Remember that collecting bugs can be a fun and educational hobby, but it's important to do so responsibly and ethically. By following these guidelines, you can enjoy the hobby while also helping to protect these fascinating creatures and their habitats.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值