D. Set To Max
题目:
Easy
思路:
简单题
由于题目的数据给的很小,所以我们可以用 n² 的复杂度过,那我们来观察一下我们应该怎么操作
显然,如果 a[i] > b[i] 时是无法构造的,同时 a[i] = b[i] 时就不用管了,所以我们直接看 a[i] < b[i]
既然我们要使得这一点可以变成 b[i],那么我们向左右两边寻找一下即可,显然第一个 a[j] = b[i] 的点 j 是最优的,因为如果 j 不行,那么之后的点肯定也不符合,所以我们只要向左和向右找是否存在 a[j] = b[i] 的点 j 即可
但是没有什么特殊条件吗?
显然肯定有的,如果遇到了 a[j] > b[i],那我们就不能继续找了,否则到时候会把 a[i] 变成比 b[i] 还大的 a[j],所以肯定不行
那么还有吗?
当然,我们再想想,如果我们向左找的时候遇到一个点有 b[j] < b[i] 会发生什么,如果我们要替换,无论此时 a[j] = b[j] 还是 a[j] < b[j] ,既然我们包括了这个区间,那么最后 a[j] 一定会变成 比b[j] 更大的 b[i],因此我们遇到 b[j] < b[i] 时也要停止
为什么这样找是可行的呢?
因为这一题我们可以按照任意顺序执行,所以我们找到对应的点后只需要从小到大操作即可,虽然这一题并没有让我们输出操作
代码:
#include <iostream>
#include <algorithm>
#include<cstring>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <memory>
using namespace std;
#define int long long
#define yes cout << "YES\n"
#define no cout << "NO\n"void solve()
{int n;cin >> n;vector<int> a(n), b(n);set<int> ahas;for (int i = 0; i < n; i++){cin >> a[i];ahas.insert(a[i]);}int flag = 0;for (int i = 0; i < n; i++){cin >> b[i];if (a[i] > b[i] || !ahas.count(b[i])){flag = 1;}}if (flag){no;return;}for (int i = 0; i < n; i++){if (a[i] == b[i])continue;int can = 0;for (int j = i- 1; j >= 0; j--){if (a[j] > b[i] || b[j] < b[i])break;if (a[j] == b[i]){can = 1;break;}}for (int j = i + 1; j < n; j++){if (a[j] > b[i] || (a[j] == b[j] && b[j] < b[i]))break;if (a[j] == b[i]){can = 1;break;}}if (!can){no;return;}}yes;
}signed main()
{cin.tie(0)->sync_with_stdio(false);int t = 1;cin >> t;while (t--){solve();}return 0;
}
Hard
思路:
考优化,二分 + ST表
由于困难版 n 变大了,因此我们不能左右依次枚举 j 了,那我们怎么找呢?
我们其实可以将 a[i] 的位置 i 存下来,那我们查找的时候只需要使用二分去 pos[b[i]] 中寻找 第一个大于 i 的位置 j 和 最后一个小于 i 的位置 j
找是找到了,但是那些判断怎么写?
我们观察easy我们找到的条件,第一个是不能存在 b[i] > b[j] 第二个不能存在 a[j] > b[i],所以我们需要快速找到 j ~ i 这个区间的最大值和最小值,只要 MAX 和 MIN 都满足条件,那么其他点肯定也是满足条件的,对于 区间最大值/最小值 (RMQ) 问题,我们可以使用 ST 表 或者 树状数组,这里使用 ST 表实现
至此优化问题就解决了
代码:
#include <iostream>
#include <algorithm>
#include<cstring>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <memory>
using namespace std;
#define int long long
#define yes cout << "YES\n"
#define no cout << "NO\n"
const int MAXN = 2e5 + 10;int lg[MAXN];
int n;
int stMX[MAXN][20], stMI[MAXN][20];
void InitLog()
{lg[1] = 0;for (int i = 2; i <= 2e5; i++)lg[i] = lg[i >> 1] + 1;
}void InitST()
{for (int j = 1; j <= lg[n]; j++){for (int i = 1; i + (1LL << j) - 1 <= n; i++){stMX[i][j] = max(stMX[i][j - 1], stMX[i + (1LL << (j - 1))][j - 1]);stMI[i][j] = min(stMI[i][j - 1], stMI[i + (1LL << (j - 1))][j - 1]);}}
}int getMax(int l,int r)
{int len = lg[r - l + 1];return max(stMX[l][len], stMX[r - (1LL << len) + 1][len]);
}int getMin(int l, int r)
{int len = lg[r - l + 1];return min(stMI[l][len], stMI[r - (1LL << len) + 1][len]);
}int erfengetSmall(int x,vector<int>& pos)
{int l = 0, r = pos.size() - 1;int res = -1;while (l <= r){int mid = l + r >> 1;if (pos[mid] < x){res = mid;l = mid + 1;}else{r = mid - 1;}}return res;
}void solve()
{cin >> n;vector<int> a(n+1), b(n+1);vector<vector<int>> pos(n + 1);for (int i = 1; i <= n; i++){cin >> a[i];stMX[i][0] = a[i];pos[a[i]].push_back(i);}for (int i = 1; i <= n; i++){cin >> b[i];stMI[i][0] = b[i];}InitST();for (int i = 1; i <= n; i++){int flag = 0;if (a[i] == b[i])continue;if (a[i] > b[i]){no;return;}if (!pos[b[i]].empty()){auto it = lower_bound(pos[b[i]].begin(), pos[b[i]].end(), i);if (it != pos[b[i]].end()){int r = *it;if (getMax(i, r) <= b[i] && getMin(i, r) >= b[i]){flag = 1;}}int L = erfengetSmall(i, pos[b[i]]);if (L >= 0){int l = pos[b[i]][L];if (getMax(l, i) <= b[i] && getMin(l, i) >= b[i]){flag = 1;}}}if (!flag){no;return;}}yes;
}signed main()
{InitLog();cin.tie(0)->sync_with_stdio(false);int t = 1;cin >> t;while (t--){solve();}return 0;
}