Loading

[ABC274D] Robot Arms 2 题解

思路

比较简单的一道题。

考虑到方向都是确定的,只要考虑每一步走正的还是走负。

很简单就可以想到使用背包来做这个问题。

\(f_{i,j}\) 为第 \(\text{i}\) 步,能否走到坐标 \(j\)

可以对于 \(x,y\) 坐标分别考虑。

但需要注意,坐标有可能是负的,所以需要给整体加上一个极大值。

给一个 \(\text{dp}\) 转移式。

\[f_{i,j} = f_{i,j-a_i} + f_{i,j+a_i} \]

时间复杂度:\(O(nm)\)\(m\) 为值域。

Code

#include <bits/stdc++.h>
using namespace std;

const int N = 1010;
const int inf = 10000;

int a[N] , f[3][2][inf * 2];

inline int read()
{
    int asd = 0 , qwe = 1; char zxc;
    while(!isdigit(zxc = getchar())) if(zxc == '-') qwe = -1;
    while(isdigit(zxc)) asd = asd * 10 + zxc - '0' , zxc = getchar();
    return asd * qwe;
}

int main()
{
    int n = read() , x = read() , y = read();
    for(int i = 1;i <= n;i++)
        a[i] = read();
    int opt = 0 , optl[3];
    for(int k = 1;k <= 2;k++)
    {
        int opt = 0; 
        if(k == 1) f[k][opt][inf + a[1]] = 1;
        else f[k][opt][inf] = 1;
        for(int i = (k == 1 ? 3 : 2);i <= n;i += 2)
        {
            opt ^= 1;
            memset(f[k][opt] , 0 , sizeof f[k][opt]);
            for(int j = 0;j <= 2 * inf;j++)
            {
                if(j - a[i] >= 0) f[k][opt][j] |= f[k][opt ^ 1][j - a[i]];
                if(j + a[i] <= 2 * inf) f[k][opt][j] |= f[k][opt ^ 1][j + a[i]];
            }
        }
        optl[k] = opt;
    }
    if(f[1][optl[1]][x + inf] && f[2][optl[2]][y + inf])
        puts("Yes");
    else puts("No");
    return 0;
}
posted @ 2022-10-24 14:36  JiaY19  阅读(71)  评论(0编辑  收藏  举报