1. 程式人生 > 其它 >每日構造/DP(4.22)

每日構造/DP(4.22)

E. Magic Stones

難點主要在想到用差分做。。

\(d\)\(c\) 的差分陣列,由 \(c′_i = c_{i+1} + c_{i−1} − c_i\) 移項可得: \(c′_i + c_{i−1} = c_{i+1} − c_i\)

因此對 \(c_i\) 進行一次操作相當於交換 \(d_i\)\(d_{i+1}\) ,故將 \(c\)\(t\) 的差分陣列排序後判斷是否相同即可。

#include <bits/stdc++.h>
#define IOS                           \
    std::ios::sync_with_stdio(false); \
    std::cin.tie(0);                  \
    std::cout.tie(0);
#define PLL std::pair<ll, ll>
#define val first
#define id second
using ll = long long;

int main()
{
    IOS;
    int n;
    std::cin >> n;
    std::vector<ll> c(n + 1), t(n + 1), a(n + 1), b(n + 1);
    for (int i = 1; i <= n; ++i)
        std::cin >> c[i];
    for (int i = 1; i <= n; ++i)
        std::cin >> t[i];
    for (int i = 1; i <= n; ++i)
        a[i] = c[i] - c[i - 1], b[i] = t[i] - t[i - 1];
    std::sort(a.begin() + 1, a.begin() + n + 1);
    std::sort(b.begin() + 1, b.begin() + n + 1);
    bool flag = true;
    for (int i = 1; i <= n; ++i)
        flag &= a[i] == b[i];
    if (c[1] != t[1] || c[n] != t[n])
        flag = false;
    std::cout << (flag ? "Yes" : "No") << std::endl;
    return 0;
}