Codeforces-1494 C. 1D Sokoban(推箱子,模拟)

You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.

You start on a position 0. There are 𝑛 boxes, the 𝑖-th box is on a position 𝑎𝑖. All positions of the boxes are distinct. There are also 𝑚 special positions, the 𝑗-th position is 𝑏𝑗. All the special positions are also distinct.

In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can’t go through the boxes. You can’t pull the boxes towards you.

You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.

Input
The first line contains a single integer 𝑡 (1≤𝑡≤1000) — the number of testcases.

Then descriptions of 𝑡 testcases follow.

The first line of each testcase contains two integers 𝑛 and 𝑚 (1≤𝑛,𝑚≤2⋅105) — the number of boxes and the number of special positions, respectively.

The second line of each testcase contains 𝑛 distinct integers in the increasing order 𝑎1,𝑎2,…,𝑎𝑛 (−109≤𝑎1<𝑎2<⋯<𝑎𝑛≤109; 𝑎𝑖≠0) — the initial positions of the boxes.

The third line of each testcase contains 𝑚 distinct integers in the increasing order 𝑏1,𝑏2,…,𝑏𝑚 (−109≤𝑏1<𝑏2<⋯<𝑏𝑚≤109; 𝑏𝑖≠0) — the special positions.

The sum of 𝑛 over all testcases doesn’t exceed 2⋅105. The sum of 𝑚 over all testcases doesn’t exceed 2⋅105.

Output
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.

Example
inputCopy
5
5 6
-1 1 5 11 15
-4 -3 -2 6 7 15
2 2
-1 1
-1000000000 1000000000
2 2
-1000000000 1000000000
-1 1
3 5
-1 1 2
-2 -1 1 2 5
2 1
1 2
10
outputCopy
4
2
0
3
1
Note
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position −1 and push a box to −2. At the end, the boxes are on positions [−2,6,7,11,15], respectively. Among them positions [−2,6,7,15] are special, thus, the answer is 4.

In the second testcase you can push the box from −1 to −109, then the box from 1 to 109 and obtain the answer 2.

The third testcase showcases that you are not allowed to pull the boxes, thus, you can’t bring them closer to special positions.

In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.

In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10.

题意:
n n n个箱子, n n n个坑位,都在 x x x轴上,你要把箱子移到坑位上才有收益,移动箱子的方法就和游戏里面推箱子一下,只可以推,不可以拉。初始点你在(0,0),求最多能有多少个箱子到达坑位。

思路:
模拟一下就好了。正轴负轴分别考虑;推的时候会把箱子堆起来,相当于一定长度的区间去覆盖坑位。

很容易想到,堆起来箱子中的最后一个箱子位置如果就对应坑位,那么结果更优。所以枚举以某个坑位结尾,结果就是,这个位置累计的箱子长度所覆盖的坑位数,加上后面箱子初始位置就对应坑位的箱子数。

#include <cstring>
#include <iostream>
#include <vector>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <map>

using namespace std;
const int maxn = 2e5 + 7;
int sum[maxn];
map<int,int>vis;
int Lower(int x,vector<int>&vec) {
    return lower_bound(vec.begin(),vec.end(),x) - vec.begin();
}
int Upper(int x,vector<int>&vec) {
    return upper_bound(vec.begin(),vec.end(),x) - vec.begin();
}
int solve(vector<int>&per,vector<int>&pos) {
    int n = per.size(),m = pos.size();
    sum[n + 1] = 0;
    vis.clear();
    for(int i = 0;i <= max(n,m);i++) sum[i] = 0;
    for(int i = 0;i < m;i++) vis[pos[i]] = 1;
    for(int i = n - 1;i >= 0;i--) {
        sum[i] = sum[i + 1];
        if(vis[per[i]]) sum[i]++;
    }
    int ans = 0;
    for(int i = 0;i < m;i++) { //以第i个箱子结尾
        int tmp = 0;
        int num = Upper(pos[i],per); //有i个人
        tmp = sum[num];
        int l = Lower(pos[i] - num + 1, pos);
        int r = i;
        tmp += r - l + 1;
        ans = max(ans,tmp);
    }
    return ans;
}
int main() {
    int T;scanf("%d",&T);
    while(T--) {
        int n,m;scanf("%d%d",&n,&m);
        vector<int>per1,pos1; //正数
        vector<int>per2,pos2; //负数
        for(int i = 1;i <= n;i++) {
            int x;scanf("%d",&x);
            if(x < 0) per2.push_back(-x);
            else per1.push_back(x);
        }
        for(int i = 1;i <= m;i++) {
            int x;scanf("%d",&x);
            if(x < 0) pos2.push_back(-x);
            else pos1.push_back(x);
        }
        sort(per2.begin(),per2.end());
        sort(pos2.begin(),pos2.end());
        int ans = solve(per1,pos1) + solve(per2,pos2);
        printf("%d\n",ans);
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值