首页 > 其他分享 >2024初秋集训——提高组 #38

2024初秋集训——提高组 #38

时间:2024-10-17 22:59:27浏览次数:1  
标签:Node 38 dist int 2024 pq MAXN const 初秋

B. 广告效应

题目描述

有 \(N\) 户人家在一个数轴上,第 \(i\) 户人在 \(x_i\),影响力为 \(p_i\)。你决定把你的书送给一些人并让他们推销。如果一对人 \(i,j\) 满足:你送了 \(i\) 书且 \(|x_i-x_j|\le p_i-p_j\),那么 \(j\) 会买你的书。

求你至少要送几个人书才能让所有人都有你的书。

思路

这个东西很明显有传递性,即 \(i\rightarrow j,j\rightarrow k\),那么一定有 \(i\rightarrow k\)。所以我们一定只会选择哪些无法被别人送书的人,只需排序,并记录最大/小值即可。

空间复杂度 \(O(N)\),时间复杂度 \(O(N\log N)\)。

代码

#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. 易形迷宫

题目描述

有一个 \(N\times M\) 的迷宫,其中有空地和墙壁。你可以在空地中上下左右移动,但是你不一定能直接到达终点。你有一种法术,可以使任意 \(K\times K\) 的矩形中的墙壁消失。求到达终点至少需要使用几次法术。

思路

显然法术只会紧贴着自己的位置使用,并且不会多次经过法术的区域,所以我们可以把它看作临时的。一个法术可以让你接下来 \(k-1\) 步无视墙壁(这里也可以斜着走),我们记录使用了几次法术,当前的法术还剩下多少步。这样跑最短路即可。

空间复杂度 \(O(NM)\),时间复杂度 \(O(NM\log NM)\)。

代码

#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;
}

标签:Node,38,dist,int,2024,pq,MAXN,const,初秋
From: https://www.cnblogs.com/yaosicheng124/p/18473284

相关文章

  • 2024/10/17
    今天还有一道题没做,等以后有时间再来补。abc187EThroughPath每次修改的两个点之间有一条连边,对于每次\(1\)修改:若\(b\)是\(a\)的父亲,只需要将\(a\)子树加\(x\)。若\(a\)是\(b\)的父亲,只需要全局加\(x\),将\(b\)子树减\(x\)。HNOI2018省队集训Day5Pa......
  • Response & web登录操作 -2024/10/17
    响应行设置响应状态码:voidsetStatus(intsc);设置响应头键值对:voidsetHeader(Stringname,Stringvalue);response实现重定向resp.setStatus(302);resp.setHeader("location","https://www.4399.com");前端a.html登录,将结果传给后端,用request接收,用M......
  • 多校A层冲刺NOIP2024模拟赛08 排列
    多校A层冲刺NOIP2024模拟赛08排列一种连续段dp的解法。题面小Y最近在研究组合数学,他学会了如何枚举排列。小Z最近在研究数论,他学会了求最大公约数。于是小Y和小Z联手出了一个有趣的题目:有多少个长度为\(n\)且任意相邻两个数的最大公约数都不为\(k\)的排列?......
  • luogu P3842 [TJOI2007] 线段
    link好题,考虑如何设定状态。设\(dp_{i,0/1}\)表示到了第\(i\)行走完后停在这一行的最左侧/最右侧。设定\(l_i\)表示这一行该线段的最左侧,\(r_i\)表示这一行的最右侧。思考如何转移。1.当我处在这一行的最左侧时,我需要从这一行的右端点转移过来,所以你的贡献要加上这个线段的长......
  • 2024.10.17
    DATE#:20241017ITEM#:DOCWEEK#:THURSDAYDAIL#:玖月拾伍TAGS<BGM=“呼唤落花之名-MoreanP”><oi-contest><[NULL]><[空]><[空]>我携落花与浪漫给予你,给予温柔本身。距2024CSP-S还有\(\textbf{0x8}\)天!!!比赛主页-20241017高一csp模拟赛A......
  • 2024-10-17
    字体属性颜色大小粗细样式设置元素字体示例点击查看代码<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0">......
  • 多校A层冲刺NOIP2024模拟赛08
    GGT1传送(teleport)签到题,但是你怎么知道我场上因为dis[j]=bi[i].w调了一个小时。就是这肯定是一张完全图,但是肯定不能把所有的边都连上然后去跑dij,那么就要考虑那些边是没用的。对于从$(1,2)$到$(5,4)$,最优的是直接通过$Y$轴转过去,但是也可以先到$(3,3)......
  • k8s-NFS系统配置 20241017
    1、NFS服务端安装-master节点192.168.177.133#安装nfs服务端yuminstallnfs-utils-y#创建共享目录mkdir/nfs#配置nfs共享vim/etc/exports#添加以下一行/nfs*(rw,sync,no_root_squash)#指明共享目录和权限设置 #启动nfs服务,并设置开机启动systemctlstartnfs-ser......
  • 2024-2025-1 20241401 《计算机基础与程序设计》 第四周学习总结
    班级链接2024计算机基础与程序设计作业要求作业要求作业目标①门电路②组合电路,逻辑电路③冯诺依曼结构④CPU,内存,IO管理⑤嵌入式系统,并行结构⑥物理安全教材学习内容总结《计算机科学概论》第四章、第五章门:非(NOT)门、与(AND)门、或(OR)门、异或(XOR)......
  • 物理 + 人工智能 = 2024年诺贝尔物理学奖
          ......