2024初秋集训——提高组 #38
B. 广告效应
题目描述
有
求你至少要送几个人书才能让所有人都有你的书。
思路
这个东西很明显有传递性,即
空间复杂度
代码
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 500001;
struct Node {
int x, p, id;
}s[MAXN];
int n, ans, tot;
bool vis[MAXN];
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> s[i].x >> s[i].p;
s[i].id = i;
}
sort(s + 1, s + n + 1, [](const Node &a, const Node &b) -> bool {
return a.x < b.x || (a.x == b.x && a.p > b.p);
});
for(int i = 1; i <= n; ++i) {
if(s[i].x != s[tot].x || s[i].p != s[tot].p) {
s[++tot] = s[i];
}
}
for(int i = 1, Max = 0; i <= tot; ++i) {
if(Max >= s[i].x + s[i].p) {
vis[i] = 1;
}
Max = max(Max, s[i].x + s[i].p);
}
for(int i = tot, Min = int(2e9) + 1; i >= 1; --i) {
if(Min <= s[i].x - s[i].p) {
vis[i] = 1;
}
Min = min(Min, s[i].x - s[i].p);
}
for(int i = 1; i <= tot; ++i) {
ans += !vis[i];
}
cout << ans;
return 0;
}
C. 易形迷宫
题目描述
有一个
思路
显然法术只会紧贴着自己的位置使用,并且不会多次经过法术的区域,所以我们可以把它看作临时的。一个法术可以让你接下来
空间复杂度
代码
#include<bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAXN = 2505, dx[] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[] = {0, 0, -1, 1, -1, 1, -1, 1}, INF = int(1e9);
struct Node {
int x, y, d, c;
};
struct cmp {
bool operator()(const Node &a, const Node &b) const {
return a.d > b.d || (a.d == b.d && a.c < b.c);
}
};
int n, m, k, sx, sy, tx, ty;
vector<char> ch[MAXN];
vector<bool> vis[MAXN];
vector<pii> dist[MAXN];
priority_queue<Node, vector<Node>, cmp> pq;
void C(int x, int y, int d, int c) {
if(x < 1 || x > n || y < 1 || y > m) {
return;
}
c = 0;
if(ch[x][y] == '#') {
d++, c = k - 1;
}
if(d < dist[x][y].first || (d == dist[x][y].first && c > dist[x][y].second)) {
dist[x][y] = {d, c};
pq.push({x, y, d, c});
}
}
void F(int x, int y, int d, int c) {
if(x < 1 || x > n || y < 1 || y > m) {
return;
}
c--;
if(d < dist[x][y].first || (d == dist[x][y].first && c > dist[x][y].second)) {
dist[x][y] = {d, c};
pq.push({x, y, d, c});
}
}
void dij() {
pq.push({sx, sy, 0, 0}), dist[sx][sy] = {0, 0};
for(; !pq.empty(); ) {
auto [x, y, d, c] = pq.top();
pq.pop();
//cout << x << " " << y << " " << d << " " << c << "\n";
if(vis[x][y]) {
continue;
}
vis[x][y] = 1;
for(int i : {0, 1, 2, 3}) {
C(x + dx[i], y + dy[i], d, c);
}
if(!c) {
continue;
}
for(int i : {0, 1, 2, 3, 4, 5, 6, 7}) {
F(x + dx[i], y + dy[i], d, c);
}
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m >> k >> sx >> sy >> tx >> ty;
for(int i = 1; i <= n; ++i) {
ch[i].resize(m + 1), vis[i].resize(m + 1), dist[i].resize(m + 1);
for(int j = 1; j <= m; ++j) {
cin >> ch[i][j];
dist[i][j] = {INF, -1};
}
}
dij();
cout << dist[tx][ty].first;
return 0;
}
本文作者:yaosicheng124
本文链接:https://www.cnblogs.com/yaosicheng124/p/18473284
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步