当前位置:网站首页>Prefix equality [DP | hash]
Prefix equality [DP | hash]
2022-07-19 14:20:00 【Codiplay】
E - Prefix Equality
The question : Give two sequences of integers a,b, Next is q Time to ask , Ask and give two integers x,y, ask a Before x Digit numbers and b Before y Whether the types of digits are exactly the same .
DP Thought !
- We only care about a front x Information stored in digits and b front y Bit stored information
- What does information store ? For this question , The numbers may be the same , We only care about where the numbers first appear , Store in prea Inside
- ansa For a Before x A digital ,b The maximum value of the coordinate where the corresponding number appears , If a Before x In the numbers b There is no certain number in , Set to inf.
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int inf = 0x3f3f3f3f;
int a[200010], b[200010];
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
cin >> n;
std::map<int, int> prea,preb;
for (int i = 1; i <= n; i ++ ) {
cin >> a[i];
if(!prea.count(a[i]))prea[a[i]] = i;
}
for (int i = 1; i <= n; i ++ ) {
cin >> b[i];
if(!preb.count(b[i]))preb[b[i]] = i;
}
std::vector<int> ansa(n + 1, 0), ansb(n + 1, 0);
for (int i = 1; i <= n; i ++ ) {
ansa[i] = max(ansa[i - 1], preb.count(a[i]) ? preb[a[i]] : inf);
ansb[i] = max(ansb[i - 1], prea.count(b[i]) ? prea[b[i]] : inf);
}
int t;
cin >> t;
while(t -- ) {
int x, y;
cin >> x >> y;
cout << ((ansb[y] <= x && ansa[x] <= y) ? "Yes" : "No") << '\n';
}
return 0;
}Hash
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
ll a[200010], b[200010];
set<ll> se;
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
cin >> n;
ll s = 0;
for (int i = 1; i <= n; i ++ ) {
ll x;
cin >> x;
if(se.find(x) == se.end()) {
s = (s + x * (x + 137) % mod * (x + 997) % mod) % mod;
}
a[i] = s;
se.insert(x);
}
se.clear();
s = 0;
for (int i = 1; i <= n; i ++ ) {
ll x;
cin >> x;
if(se.find(x) == se.end()) {
s = (s + x * (x + 137) % mod * (x + 997) % mod) % mod;
}
b[i] = s;
se.insert(x);
}
int t;
cin >> t;
while(t -- ) {
int x, y;
cin >> x >> y;
if(a[x] == b[y]) {
cout << "Yes\n";
}
else cout << "No\n";
}
return 0;
}边栏推荐
- ModuleNotFoundError: No module named ‘_ distutils_ hack‘
- Installation of Topy Library (topology optimization software)
- A review of classical must see for Nonconvex Optimization Problems "from symmetry to geometry", University of Rochester, et al
- 暑期rhcsa培训第三天作业
- 坐标模拟矩阵旋转的公式
- What are the ways to realize load balancing?
- Robotics at google:laura Graesser | i-sim2real: strengthen the learning robot strategy in the close human-computer interaction cycle
- 面试记录
- unity 实现UI-背包装备拖拽功能
- 慎用TongWeb的热部署功能
猜你喜欢

Huawei wireless devices are configured with static load balancing

Interview records

坐标模拟矩阵旋转的公式

Okaleido或杀出NFT重围,你看好它吗?

4 a company has branches in six cities C1, C2, C3... C6. The connection between cities Ci and CJ (I, j=1,2,3,... 6) and the cost are listed in the following weighted adjacency matrix C
![[Flink] Flink will report an error if it fails to set checkpoints once. Setlerablecheckpointfailurenumber does not work](/img/62/5761d8d7b2d8ea1bc74b609b6aa3c7.jpg)
[Flink] Flink will report an error if it fails to set checkpoints once. Setlerablecheckpointfailurenumber does not work

unity 实现UI-背包装备拖拽功能

毕设-基于SSM在线预约挂号系统

Interview with Android development companies, make these three preparations and plan yourself

Huawei wireless device configuration dynamic load balancing
随机推荐
Interview records
TKE(K8S)部署mysql使用CFS存储
Huawei wireless device configuration dynamic load balancing
No.3 advanced assembly
Redis源码与设计剖析 -- 3.字典
Uniapp Gaode map positioning function
What are the ways to realize load balancing?
面额10exp(303)的钞票
Huawei technologies:jonathan Krolikowski | from design to deployment, zero contact deep reinforcement learning WLANs
STL string replication comparison
FreeRTOS personal notes - multi priority support
研二非科班研究生如何备战秋招
树与二分图【思维】
FreeRTOS implementation of idle tasks and blocking delay
How to prepare for the autumn recruitment for the graduate students of the second non science class of Graduate School
matplotlib绘制多折线图(解决matplotlib中文无法显示问题)
华为无线设备配置用户CAC
腾讯云对象存储操作流程
Chrome plug-ins for information collection
Colliding Mice碰撞老鼠工程分析