日志统计
小明维护着一个程序员论坛。现在他收集了一份”点赞”日志,日志共有 N 行。
其中每一行的格式是:
ts id
表示在 ts 时刻编号 id 的帖子收到一个”赞”。
现在小明想统计有哪些帖子曾经是”热帖”。
如果一个帖子曾在任意一个长度为 D 的时间段内收到不少于 K 个赞,小明就认为这个帖子曾是”热帖”。
具体来说,如果存在某个时刻 T 满足该帖在 [T,T+D) 这段时间内(注意是左闭右开区间)收到不少于 K 个赞,该帖就曾是”热帖”。
给定日志,请你帮助小明统计出所有曾是”热帖”的帖子编号。
输入格式
第一行包含三个整数 N,D,K。
以下 N 行每行一条日志,包含两个整数 ts 和 id。
输出格式
按从小到大的顺序输出热帖 id。
每个 id 占一行。
数据范围
1 ≤ K ≤ N ≤ 105,
0 ≤ ts,id ≤ 105,
1 ≤ D ≤ 10000
输入样例:
7 10 2
0 1
0 10
10 10
10 1
9 1
100 3
100 3
输出样例:
1
3
考虑循环方式:
- 循环id 然后 循环时间 统计每个id在每个时间段内点赞数是否大于要求
- 循环时间段 然后 循环id 统计每个时间段内每一个id的热度是否大于要求
双指针思想:
- 需要同时维护一前一后两个下标
- 考虑算法中重复的部份,优化,只动态改变不同的部份
#include <iostream>
#include <cstring>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
int n, d, k;
PII logs[N];
int cnt[N];
bool st[N];
int main()
{
cin >> n >> d >> k;
for (int i = 0; i < n; i ++ ) cin >> logs[i].x >> logs[i].y;
sort(logs, logs + n);
// 双指针 i在j前 维护一段滑动窗口
for (int i = 0, j = 0; i < n; i ++ )
{
cnt[logs[i].y] ++ ;
// 维护窗口
while (logs[i].x - logs[j].x >= d)
{
cnt[logs[j].y] -- ;
j ++ ;
}
// 判断
if (cnt[logs[i].y] >= k)
st[logs[i].y] = true;
}
for (int i = 0; i < N; i ++ )
if (st[i])
cout << i << endl;
return 0;
}
献给阿尔吉侬的花束
阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫。
今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪。
现在研究员们想知道,如果阿尔吉侬足够聪明,它最少需要多少时间就能吃到奶酪。
迷宫用一个 R×C 的字符矩阵来表示。
字符 S 表示阿尔吉侬所在的位置,字符 E 表示奶酪所在的位置,字符 # 表示墙壁,字符 . 表示可以通行。
阿尔吉侬在 1 个单位时间内可以从当前的位置走到它上下左右四个方向上的任意一个位置,但不能走出地图边界。
输入格式
第一行是一个正整数 T,表示一共有 T 组数据。
每一组数据的第一行包含了两个用空格分开的正整数 R 和 C,表示地图是一个 R×C 的矩阵。
接下来的 R 行描述了地图的具体内容,每一行包含了 C 个字符。字符含义如题目描述中所述。保证有且仅有一个 S 和 E。
输出格式
对于每一组数据,输出阿尔吉侬吃到奶酪的最少单位时间。
若阿尔吉侬无法吃到奶酪,则输出“oop!”(只输出引号里面的内容,不输出引号)。
每组数据的输出结果占一行。
数据范围
1 < T ≤ 10,
2 ≤ R,C ≤ 200
输入样例:
3
3 4
.S..
###.
..E.
3 4
.S..
.E..
....
3 4
.S..
####
..E.
输出样例:
5
1
oop!
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 210;
int n;
int r, c;
char g[N][N];
int dist[N][N];
int bfs(int sx, int sy)
{
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
queue<PII> q;
q.push({sx, sy});
memset(dist, -1, sizeof dist);
dist[sx][sy] = 0;
while (q.size())
{
PII t = q.front();
q.pop();
for (int i = 0; i < 4; i ++ )
{
int x = t.first + dx[i];
int y = t.second + dy[i];
// 由于每次g不会清空,故可能会有上一次地图的残留,故需要对地图做判断
if (x < 0 || x >= r || y < 0 || y >= c) continue;
if (g[x][y] == 'E'){
return dist[t.first][t.second] + 1;
}
if (x >= 0 && x < r && y >= 0 && y < c && g[x][y] == '.' && dist[x][y] == -1)
{
q.push({x, y});
dist[x][y] = dist[t.first][t.second] + 1;
}
}
}
return -1;
}
int main()
{
cin >> n;
while (n -- )
{
int x, y;
cin >> r >> c;
for (int i = 0; i < r; i ++ )
for (int j = 0; j < c; j ++ )
{
cin >> g[i][j];
if (g[i][j] == 'S')
{
x = i;
y = j;
}
}
int t = bfs(x, y);
if (t == -1) puts("oop!");
else cout << t << endl;
}
return 0;
}
// 标准写法
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 210;
int n, m;
char g[N][N];
int dist[N][N];
int bfs(PII start, PII end)
{
queue<PII> q;
memset(dist, -1, sizeof dist);
dist[start.x][start.y] = 0;
q.push(start);
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
while (q.size())
{
auto t = q.front();
q.pop();
for (int i = 0; i < 4; i ++ )
{
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 0 || x >= n || y < 0 || y >= m) continue; // 出界
if (g[x][y] == '#') continue; // 障碍物
if (dist[x][y] != -1) continue; // 之前已经遍历过
dist[x][y] = dist[t.x][t.y] + 1;
if (end == make_pair(x, y)) return dist[x][y];
q.push({x, y});
}
}
return -1;
}
int main()
{
int T;
scanf("%d", &T);
while (T -- )
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++ ) scanf("%s", g[i]);
PII start, end;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
if (g[i][j] == 'S') start = {i, j};
else if (g[i][j] == 'E') end = {i, j};
int distance = bfs(start, end);
if (distance == -1) puts("oop!");
else printf("%d\n", distance);
}
return 0;
}
代码学习:
作者:yxc
链接:https://www.acwing.com/activity/content/code/content/174687/
来源:AcWing
红与黑
有一间长方形的房子,地上铺了红色、黑色两种颜色的正方形瓷砖。
你站在其中一块黑色的瓷砖上,只能向相邻(上下左右四个方向)的黑色瓷砖移动。
请写一个程序,计算你总共能够到达多少块黑色的瓷砖。
输入格式
输入包括多个数据集合。
每个数据集合的第一行是两个整数 W 和 H,分别表示 x 方向和 y 方向瓷砖的数量。
在接下来的 H 行中,每行包括 W 个字符。每个字符表示一块瓷砖的颜色,规则如下
1)‘.’:黑色的瓷砖;
2)‘#’:红色的瓷砖;
3)‘@’:黑色的瓷砖,并且你站在这块瓷砖上。该字符在每个数据集合中唯一出现一次。
当在一行中读入的是两个零时,表示输入结束。
输出格式
对每个数据集合,分别输出一行,显示你从初始位置出发能到达的瓷砖数(记数时包括初始位置的瓷砖)。
数据范围
1 ≤ W,H ≤ 20
输入样例:
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
0 0
输出样例:
45
// BFS
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int,int> PII;
const int N = 25;
int w, h, res;
char g[N][N];
bool flag[N][N];
void bfs(PII start)
{
res = 1;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
memset(flag, 0, sizeof flag);
flag[start.first][start.second] = true;
queue<PII> q;
q.push(start);
while (q.size())
{
PII t = q.front();
q.pop();
for (int i = 0; i < 4; i ++ )
{
int x = t.first + dx[i];
int y = t.second + dy[i];
if (x < 0 || x >= h || y < 0 || y >= w) continue;
if (g[x][y] != '.') continue;
if (flag[x][y]) continue;
q.push({x, y});
flag[x][y] = true;
res ++ ;
}
}
}
int main()
{
while (cin >> w >> h, w || h)
{
PII start;
for (int i = 0; i < h; i ++ )
for (int j = 0; j < w; j ++ )
{
cin >> g[i][j];
if (g[i][j] == '@')
start = {i, j};
}
bfs(start);
cout << res << endl;
}
return 0;
}
// DFS
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 25;
int w, h;
char g[N][N];
bool flag[N][N];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int dfs(int sx, int sy)
{
int res = 1;
flag[sx][sy] = true;
for (int i = 0; i < 4; i ++ )
{
int x = sx + dx[i];
int y = sy + dy[i];
if (x < 0 || x >= h || y < 0 || y >= w) continue;
if (g[x][y] != '.') continue;
if (flag[x][y]) continue;
res += dfs(x, y);
}
return res;
}
int main()
{
while (cin >> w >> h, w || h)
{
int x, y;
for (int i = 0; i < h; i ++ )
for (int j = 0; j < w; j ++ )
{
cin >> g[i][j];
if (g[i][j] == '@')
x = i, y = j;
}
memset(flag, 0, sizeof flag);
cout << dfs(x, y) << endl;
}
return 0;
}
交换瓶子
有 N 个瓶子,编号 1∼N,放在架子上。
比如有 5 个瓶子:
2 1 3 5 4
要求每次拿起 2 个瓶子,交换它们的位置。
经过若干次后,使得瓶子的序号为:
1 2 3 4 5
对于这么简单的情况,显然,至少需要交换 2 次就可以复位。
如果瓶子更多呢?你可以通过编程来解决。
输入格式
第一行包含一个整数 N,表示瓶子数量。
第二行包含 N 个整数,表示瓶子目前的排列状况。
输出格式
输出一个正整数,表示至少交换多少次,才能完成排序。
数据范围
1 ≤ N ≤ 10000,
输入样例1:
5
3 1 2 5 4
输出样例1:
3
输入样例2:
5
5 4 3 2 1
输出样例2:
2
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010;
int n;
int a[N];
bool st[N];
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
int cnt = 0;
for (int i = 1; i <= n; i ++ )
if (!st[i])
{
cnt ++ ;
for (int j = i; !st[j]; j = a[j])
st[j] = true;
}
cout << n - cnt << endl;
return 0;
}