抱歉,没仔细看题。
我建议 `map` 维护时间缀和权值
by c20251920TBN @ 2024-03-28 20:07:07
@[XuYu0317](/user/1120928)
by weak_in_code @ 2024-03-28 20:07:10
以后别听不得被人劝
```
cpp
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <set>
int a[1145144],k,ans=0;
using namespace std;
set<int> SET;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=m;i++)
{
cin>>k;
SET.insert(k);
}
for(int i=1;i<=n;i++)
{
if(SET.count(a[i]))cout<<a[i]<<" ";
}
return 0;
}
```
by deviance @ 2024-03-28 20:11:23
@[weak_in_code](/user/753640) 感谢 您tql
by XuYu0317 @ 2024-03-28 20:11:41
@[weak_in_code](/user/753640) 可是这种写法如果a只是一个数组的话不是也可以达到一样的效果吗?
by XuYu0317 @ 2024-03-28 20:13:44
```cpp
#include <iostream>
#include <cstdio>
#include <set>
#include <map>
#define pii pair<int,int>
#include <vector>
#define X first
#define Y second
using namespace std;
int n, m, lena, lenb;
set<pii> a;
set<int> b;
int ans[100010];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1, d; i <= n; i ++) { // 读入a
scanf("%d", &d);
a.insert({d,i});
}
for (int i = 1, d; i <= m; i ++) { // 读入b
scanf("%d", &d);
b.insert(d);
}
for (const auto& thi : a) { // 遍历a,与b进行比对
if (b.count(thi.X)) ans[thi.Y]=thi.X;
}
for (int i = 1, d; i <= n; i ++) { // 读入a
if (ans[i]) printf("%d ",ans[i]);
}
return 0;
}
```
@[XuYu0317](/user/1120928)
by weak_in_code @ 2024-03-28 20:17:23
sorry忘了你想要set做法了。
by weak_in_code @ 2024-03-28 20:18:17
@[XuYu0317](/user/1120928) a不能是数组哦, 输入的数据为 $<2\times 10^9$,用数组存不下。
by weak_in_code @ 2024-03-28 20:19:49
@[weak_in_code](/user/753640) 第一次见到pair有点看不懂了((
by XuYu0317 @ 2024-03-28 20:21:34
@[XuYu0317](/user/1120928) 相当于一个存了两个元素的结构体,最开始说的做法就是这么实现的。
by weak_in_code @ 2024-03-28 20:22:36