9.27双指针进阶总结
T670087 输入的这些数是否对称
题目描述
从键盘输入一个整数
比如有
输入格式
第
第
输出格式
如果是对称的请输出:
正确思路
-
判断回文:用两个指针
i, j ,i 从1 开始,j 从n 开始, 每次循环判断a_i 是否等于a_j , 如果a_i == a_j , 那么就继续循环, 否则就说明不是回文代码如下:
for (int i = 1, j = n; i <= j; i++, j--) if (a[i] != a[j]) f = 0;
正确代码
#include <bits/stdc++.h>
using namespace std;
int a[1000007];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
bool f = 1;
for (int i = 1, j = n; i <= j; i++, j--) if (a[i] != a[j]) f = 0;
if (f) cout << "YES";
else cout << "NO";
return 0;
}
P4995 跳跳!
题目描述
你是一只小跳蛙,你特别擅长在各种地方跳来跳去。
这一天,你和朋友小 F 一起出去玩耍的时候,遇到了一堆高矮不同的石头,其中第
为了给小 F 展现你超级跳的本领,你决定跳到每个石头上各一次,并最终停在任意一块石头上,并且小跳蛙想耗费尽可能多的体力值。
当然,你只是一只小跳蛙,你只会跳,不知道怎么跳才能让本领更充分地展现。
不过你有救啦!小 F 给你递来了一个写着 AK 的电脑,你可以使用计算机程序帮你解决这个问题,万能的计算机会告诉你怎么跳。
那就请你——会写代码的小跳蛙——写下这个程序,为你
输入格式
输入一行一个正整数
输入第二行
输出格式
输出一行一个正整数,表示你可以耗费的体力值的最大值。
说明/提示
数据范围
对于
对于
正确思路
- 由于题目有求体力值的最大值, 所以我们得在低石头与高石头直接来会跳
- 先从第一低的石头跳到第一高的石头;再从第一高的石头跳到第二低的石头;再从第二低的石头跳到第二高的石头;先从第二高的石头跳到第三低的石头;再从第三低的石头跳到第三高的石头......
- 我们可以先把
h 数组从小到大排序, 再用两个指针i, j ,i 从0 开始,j 从n 开始, - 每次循环
ans 先加上(a[j] - a[i]) * (a[j] - a[i]) ,i 再+1 ,ans 再加上(a[j] - a[i]) * (a[j] - a[i]) ,j 再-1 , 如果i < j 就一直循环 - 代码如下:
for (int i = 0, j = n; i < j; j--) { ans += (a[j] - a[i]) * (a[j] - a[i]); i++; ans += (a[j] - a[i]) * (a[j] - a[i]); }
正确代码
#include <bits/stdc++.h>
using namespace std;
const int N = 3e2 + 7;
int n, a[N];
long long ans;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n);
for (int i = 0, j = n; i < j; j--) {
ans += (a[j] - a[i]) * (a[j] - a[i]); i++;
ans += (a[j] - a[i]) * (a[j] - a[i]);
}
cout << ans;
return 0;
}
T670094 数字交换
题目描述
有一组数(设有
输入格式
一个数
一行
两个空格分开的数(表示要交换的其中一段)
两个空格分开的数(表示要交换的其中另一段)
输出格式
交换后的一行数(中间用空格隔开)
正确思路
- 此题非常简单, 在这不多讲
正确代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7, M = 2e3 + 7;
int i = 1, j = 0;
int n, m, a[N], cnt[M];
int st, ed, minn = 1e9, tot = 0;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
for (int i = 1; i <= n; i++) {
if (i >= l1 && i <= r1) cout << a[l2 + (i - l1)] << " ";
else if (i >= l2 && i <= r2) cout << a[l1 + (i - l2)] << " ";
else cout << a[i] << " ";
}
return 0;
}
P14063 [PO Final 2022] 海滩 / Badstrand
题目描述
Maja 厌倦了海岸被大湖占据,她想要修建一个又长又漂亮的公用海滩。现在,她计划买下海岸边的一块土地来建造海滩。
Maja 预算为
输入格式
第一行包含两个整数:
第二行包含
输出格式
输出一个整数:即 Maja 能够买下的最长的一段连续的土地的长度。
说明/提示
子任务
本题采用捆绑测试。
| 子任务编号 | 得分 | 限制 |
|:-:|:-:|---|
|
正确思路
-
用两个指针
i = 1, j = 0 -
用一个
while 循环, 如果j < n 就一直循环:用一个
tot 来记录从i 到j 要多少钱while$如果$tot <= b$ && $j < n$就一直循环, 每次循环前$j$++, $tot$ += $a_j$, 如果$j - i + 1 > ans$ && $tot <= b$, $ans = j - i + 1 -
代码如下:
while (j < n) { while (tot <= b && j < n) { j++; tot += a[j]; if (j - i + 1 > ans && tot <= b) ans = j - i + 1; } while (tot > b) {tot -= a[i]; i++;} }
正确代码
#include <bits/stdc++.h>
using namespace std;
int n, b, ans = -1e9;
int i = 1, j = 0, tot;
int a[100007];
int read() {
int r = 0, f = 1; char c = getchar();
while ((c < '0' || c > '9') && (c ^ '-')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = getchar();
return r * f;
}
int main() {
n = read(), b = read();
for (int i = 1; i <= n; i++) a[i] = read();
while (j < n) {
while (tot <= b && j < n) {
j++;
tot += a[j];
if (j - i + 1 > ans && tot <= b) ans = j - i + 1;
}
while (tot > b) {tot -= a[i]; i++;}
}
cout << ans;
return 0;
}
P1638 逛画展
题目描述
博览馆正在展出由世上最佳的
游客在购买门票时必须说明两个数字,
Sept 希望入场后可以看到所有名师的图画。当然,他想最小化购买门票的价格。
请求出他购买门票时应选择的
若存在多组解,输出
输入格式
第一行两个整数
第二行包含
输出格式
一行两个整数
说明/提示
数据规模与约定
- 对于
100\% 的数据,有1\leq n\le10^6 ,1 \leq a_i \leq m\le2\times10^3 。
正确思路
- 我们可以用双指针来找最小区间:
-
当区间不满足条件, 尝试
r ++, 扩大区间 -
当区间满足条件, 缩小空间,
l ++
- 我们可以定义一个
cnt 数组,cnt_i 表示i 这个数字出现的次数,tot 表示不同数字出现的次数 - 双指针尺取法模板:
while (r < n) { while (tot < m && r < n) { r++; cnt[a[r]]++; if (cnt[a[r]] == 1) tot++; } while (tot == m) { if (r - l + 1 < minn) { minn = r - l + 1; st = l, ed = r; } cnt[a[l]]--; if (cnt[a[l]] == 0) tot--; l++; } }
正确代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100, M = 2e3 + 5;
int n, m, a[N], minn = 1e9, st, ed, tot = 0, cnt[M];
int l = 1, r = 0;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
while (r < n) {
while (tot < m && r < n) {
r++;
cnt[a[r]]++;
if (cnt[a[r]] == 1) tot++;
}
while (tot == m) {
if (r - l + 1 < minn) {
minn = r - l + 1;
st = l, ed = r;
}
cnt[a[l]]--;
if (cnt[a[l]] == 0) tot--;
l++;
}
}
cout << st << " " << ed << endl;
return 0;
}
P1381 单词背诵
题目描述
灵梦有
文章由
输入格式
第
接着是一个数
输出格式
输出文件共
说明/提示
数据规模与约定
- 对于
30\% 的数据,n \le 50 ,m \le 500 ; - 对于
60\% 的数据,n \le 300 ,m \le 5000 ; - 对于
100\% 的数据,1 \le n \le 1000 ,1 \le m \le 10^5 。
正确思路
其实不难, 代码自行体会
正确代码
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int n, m, ans1, ans2 = 1e9, tot;
string a[1007], b[100007];
map<string, bool> vis;
map<string, int> cnt, dis;
int i = 1, j = 0;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {cin >> a[i]; vis[a[i]] = 1;}
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> b[i]; dis[b[i]]++;
if (dis[b[i]] == 1 && vis[b[i]] == true) ans1++;
}
if (ans1 == 0) {cout << 0 << endl << 0 << endl; return 0;}
while (j < m) {
while (tot < ans1 && j < m) {
j++; cnt[b[j]]++;
if (cnt[b[j]] == 1 && vis[b[j]]) tot++;
}
while (tot == ans1) {
if (ans2 > j - i + 1) ans2 = j - i + 1;
cnt[b[i]]--;
if (cnt[b[i]] == 0 && vis[b[i]]) tot--;
i++;
}
}
cout << ans1 << endl << ans2 << endl;
return 0;
}