用映射表 a 存下此人是否以为自己是抓的(包括正常抓人和非法抓人), true 表示是以为自己是抓的, false 相反。
考虑到有人作弊,所以用 zb 数组存储有谁作弊; cur 表示有几人作弊。
又考虑到有人重复作弊, zb 数组有多个相同元素,还得用一个映射表 iszb 存储此人是否作弊。
const int N = 1e5 + 3;
int n,m,cur;
string s,a1,a2,zb[N];
map <string,bool> a,iszb;
输入
真实情况是,我们不需要存有哪些人加入游戏,只需要存猎人姓名,原因很简单,你存名单有什么用呢?
cin >> n >> m >> s;
a[s] = true;
for (int i = 2;i <= n;i++) cin >> s; // 请特别注意,这里要把名单刷掉
模拟
1. $a1$ 合法抓人
即 $a[a1] = true$ ,此时 $a2$ 是猎人,将 $a[a1]$ 设置为 $false$ 、 $a[a2]$ 设置为 $true$ 。
2. $a1$ 非法抓人
首先, $a2$ 在这种情况下会认为自己是猎人,因此将 $a[a2]$ 设置为 $true$ 。而 $a1$ 作弊,若他没有进入作弊名单,将其加入作弊名单,否则就是多次作弊被记录。
```cpp
for (int i = 1;i <= m;a[a2] = true,i++)
{
cin >> a1 >> s >> a2;
if (!a[a1]) { if (!iszb[a1]) zb[++cur] = a1,iszb[a1] = true; } // 非法抓人
else if (a[a1]) a[a1] = false; // 合法抓人
}
```
4. 输出
先将 $zb$ 名单按字典序排序,然后输出。
```cpp
cout << cur << "\n";
sort(zb + 1,zb + cur + 1);
for (int i = 1;i <= cur;i++) cout << zb[i] << " ";
```
综合代码:
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
int n,m,cur;
string s,a1,a2,zb[N];
map <string,bool> a,iszb;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m >> s;
a[s] = true;
for (int i = 2;i <= n;i++) cin >> s;
for (int i = 1;i <= m;a[a2] = true,i++)
{
cin >> a1 >> s >> a2;
if (!a[a1]) { if (!iszb[a1]) zb[++cur] = a1,iszb[a1] = true; }
else if (a[a1]) a[a1] = false;
}
cout << cur << "\n";
sort(zb + 1,zb + cur + 1);
for (int i = 1;i <= cur;i++) cout << zb[i] << " ";
return 0;
}
```
## 2 海滩 Badstrand
评价:普通·橙
考点:双指针(Two-Pointer) & 前缀和(Prefix Sum)
(注:前缀和为暴力写法所含的解法)
[考试题目链接](https://www.luogu.com.cn/problem/T758280?contestId=325111)
[标准题目链接](https://www.luogu.com.cn/problem/P14063)
请问请问了……
“连续”“最长”不都暗示了双指针吗?!
1. 暴力
直接暴力,枚举开始购买点至结尾购买点,遍历,计算结果;预计时间复杂度 $O(n^3)$ ,能过两个 $Subtask$ 。
(这里不展示暴力写法代码,过度沉淀)
2. 前缀和优化
看到暴力思路,“枚举开始购买点至结尾购买点”,你有没有想起那个算法?
前缀和回来啦!
再加一个前缀和,以快速计算区间之和,时间复杂度 $O(n^2)
然鹅事实是:
还是只能过两个 Subtask 。。。
评测记录,只有最后一个样例点超时
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
int n,m,ans,a[N],s[N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1;i <= n;s[i] = s[i - 1] + a[i],i++) cin >> a[i];
for (int l = 1;l <= n;l++) for (int r = l;r <= n;ans = max(ans,r - l + 1),r++) if (s[r] - s[l - 1] > m) break;
cout << ans << "\n";
return 0;
}
因为最后一个 Subtask 内, N 范围上限是 100'000 ,要超出时间范围。
难道就没有更快的算法了吗?
正解 - 双指针
开头我第一句话就讲了——““连续”“最长”不都暗示了双指针吗?!”
所以,双指针就是正解!
可以这么想:
定义指针 l、r ,初值均为 1 。
这时, $l$ 指针向前跑一格。
重复上面的过程,并用 $ans$ 记录最长长度。
这样, $ans$ 内存的就是最长长度啦!
综合代码:
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
int n,m,l,r,ans,sum,a[N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1;i <= n;i++) cin >> a[i];
l = 1;
while (l <= n && r <= n)
{
ans = max(ans,max(0,r - l + 1));
if (sum + a[r + 1] <= m) sum += a[++r];
else sum -= a[l++];
}
cout << ans << "\n";
return 0;
}
```
## 3 卡牌 Kortlek
评价:顶级·黄
考点:贪心(Greedy Algorithm)
[考试题目链接](https://www.luogu.com.cn/problem/T758281?contestId=325111)
[标准题目链接](https://www.luogu.com.cn/problem/P14064)
读入排序:
```cpp
cin >> n >> m;
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i <= m;i++) cin >> b[i];
sort(a + 1,a + n + 1); sort(b + 1,b + m + 1);
```
分情况讨论:
1. $n = m$ 时
相信大家都会贪心 ~~我同学就不会~~ ,只要将 $a$ 数组与 $b$ 数组排序配对即可。
```cpp
if (n == m) for (int i = 1;i <= n;i++) ans += abs(a[i] - b[i]);
```
2. $n \neq m$ 时
这时, $m$ 张牌中有一张牌会被浪费,我们可以计算每张牌的前缀和和后缀和。
(注:下述代码 $p$ 表示 $pre$ (前缀), $s$ 表示 $suf$ (后缀))
```cpp
for (int i = 1;i <= n;i++) p[i] = p[i - 1] + abs(a[i] - b[i]);
for (int i = n;i >= 1;i--) s[i] = s[i + 1] + abs(a[i] - b[i + 1]);
```
如果第 $i - 1$ 张牌被跳过,则总得分为 $p_i + s_{i + 1}$ ,再用 $ans$ 记录最小值即可。
(注:别忘了 $ans$ 初值赋成最大值!)
```cpp
ans = INF;
for (int i = 0;i <= n;i++) ans = min(ans,p[i] + s[i + 1]);
```
综合代码:
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 3;
const long long INF = 1e18;
int n,m,a[N],b[N];
long long ans,p[N],s[N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i <= m;i++) cin >> b[i];
sort(a + 1,a + n + 1); sort(b + 1,b + m + 1);
if (n == m) for (int i = 1;i <= n;i++) ans += abs(a[i] - b[i]);
else
{
ans = INF;
for (int i = 1;i <= n;i++) p[i] = p[i - 1] + abs(a[i] - b[i]);
for (int i = n;i >= 1;i--) s[i] = s[i + 1] + abs(a[i] - b[i + 1]);
for (int i = 0;i <= n;i++) ans = min(ans,p[i] + s[i + 1]);
}
cout << ans << "\n";
return 0;
}
```
## 4 修桥 Bridge Building
评价:夯爆了·黄
考点:并查集(Merge-Find Set)
[考试题目链接](https://www.luogu.com.cn/problem/T758282?contestId=325111)
[标准题目链接](https://www.luogu.com.cn/problem/P13987)
其实上,这题第一眼就否定了搜索算法。
原因很简单,依旧数据范围:$3 \leq N,M \leq 1e9
你能用搜索算法?
那还有啥算法能代替搜索算法?
emmm……其实到这里,你能想到这就已经很强了,用并查集
(不过并查集还挺好写的)
先把并查集两个基本函数打上:
int find(int w)
{
if (fa[w] == w) return w;
return fa[w] = find(fa[w]);
}
void unity(int u,int v)
{
int a = find(u),b = find(v);
if (a != b) fa[a] = b;
}
方向数组:const int dx[] = {0,0,-1,1},dy[] = {-1,1,0,0};
具体枚举:
```cpp
for (int j = 0;j < 4;j++)
{
nx = x + dx[j],ny = y + dy[j];
if (vis.count({nx,ny})) unity(vis[{nx,ny}],i);
}
```
做完上面的操作,所有点就被连起来了;但是,题目要求“输出一个整数:Rut 至少需要等待的小时数”,所以我们每做一次操作就判断第 0 行与第 n - 1 行是否连通,联通直接返回 i 即可:
```cpp
if (find(0) == find(t + 1))
{
cout << i << "\n";
return 0;
}
```
综合代码:
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3,dx[] = {0,0,-1,1},dy[] = {-1,1,0,0};
int n,m,t,x,y,nx,ny,fa[N];
map <pair<int,int>,int> vis;
int find(int w)
{
if (fa[w] == w) return w;
return fa[w] = find(fa[w]);
}
void unity(int u,int v)
{
int a = find(u),b = find(v);
if (a != b) fa[a] = b;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m >> t;
for (int i = 0;i <= t + 1;i++) fa[i] = i;
for (int i = 1;i <= t;vis[{x,y}] = i,i++)
{
cin >> x >> y;
if (x == 1) unity(i,0);
if (x == n - 2) unity(i,t + 1);
for (int j = 0;j < 4;j++)
{
nx = x + dx[j],ny = y + dy[j];
if (vis.count({nx,ny})) unity(vis[{nx,ny}],i);
}
if (find(0) == find(t + 1))
{
cout << i << "\n";
return 0;
}
}
cout << "nej\n";
return 0;
}