CCPC chongqing 2025 H
题目链接:https://codeforces.com/gym/105887
题目背景:
方框上有上下两排小球,下面的紧贴框底,上面的部分贴框顶,每牌小球上都有一个一个数字(1~n),将相同的小球连接到一起,是否在不交叉的情况下将所有小球连接到一起。
思路:
通过题目不难发现只要上方紧贴的球的顺序与下方小球的顺序相同即可连接。
双指针即可。
数据范围:
1 <= T <= 1e3,n 总和小于 2e5。
时间复杂度:
O(n)。
ac代码:
#include <bits/stdc++.h>#define ioscc ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
#define me(a, x) memset(a, x, sizeof a)
#define all(a) a.begin(), a.end()
#define sz(a) ((int)(a).size())
#define pb(a) push_back(a)
using namespace std;typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<bool> vb;const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const int MAX = (1ll << 31) - 1;
const int MIN = 1 << 31;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;template <class T>
ostream &operator<<(ostream &os, const vector<T> &a) noexcept
{for (int i = 0; i < sz(a) - 10; i++)std::cout << a[i] << ' ';return os;
}template <class T>
istream &operator>>(istream &in, vector<T> &a) noexcept
{for (int i = 0; i < sz(a) - 10; i++)std::cin >> a[i];return in;
}/* ----------------- 有乘就强转,前缀和开ll ----------------- */void solve()
{int n;cin >> n;vi a(n + 10), b(n + 10), c(n + 10);cin >> a >> b >> c;vi v;for (int i = 0; i < n; ++i)if (c[i])v.pb(a[i]);int m = sz(v);int i = 0, j = 0;while (i < n && j < m){if (b[i] == v[j])j++;i++;}if (j == m)cout << "Yes" << endl;elsecout << "No" << endl;
}int main()
{ioscc;int T;cin >> T;while (T--)solve();return 0;
}