题目链接 微软大楼设计方案

中文题就不说题意了~

首先是简单版本

满足$1 <= n, m <= 50$

那么设$c[i][j]$为从第$i$幢楼到第$j$幢楼的最低的那幢楼的高度

计算两个点之间的距离的时候,若两个点分别在第$i$列,第$j$列,那么要根据$c[i][j]$来计算。

暴力即可

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) int n, k;
int a[10010];
int c[201][201];
int x[201], y[201];
int m;
int ans = 0; int main(){ scanf("%d%d", &n, &k);
rep(i, 1, n) scanf("%d", a + i);
rep(i, 1, n){
c[i][i] = a[i];
rep(j, i + 1, n) c[i][j] = min(c[i][j - 1], a[j]), c[j][i] = c[i][j];
} rep(i, 1, n) rep(j, 1, n) if (c[i][j] == 0) c[i][j] = c[j][i]; scanf("%d", &m); rep(i, 1, m) scanf("%d%d", x + i, y + i); rep(i, 1, m - 1){
rep(j, i + 1, m){
int cnt;
if (y[i] > c[x[i]][x[j]] && y[j] > c[x[i]][x[j]])
cnt = y[i] - c[x[i]][x[j]] + y[j] - c[x[i]][x[j]] + abs(x[i] - x[j]);
else cnt = abs(x[i] - x[j]) + abs(y[i] - y[j]);
if (cnt <= k) ++ans;
}
} printf("%d\n", ans);
return 0; }

再是中等版本

满足$1 <= n <= 200000, 1 <= m <= 2000$

$m$的范围让我们还是可以在1秒钟之内两两枚举点对并完成统计

就是$c[i][j]$不能按照刚刚那个方法求了。

我们构建一张ST表,令$f[i][j]$为从$i$开始连续$2^{j}$个数的最小值

于是在$O(1)$内我们可以得到第$i$幢楼到第$j$幢楼的最低的那幢楼的高度

中等版本也解决了

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) int a[200030];
int f[200030][22];
int n, m, k;
int ans = 0; struct node{
int x, y;
void scan(){ scanf("%d%d", &x, &y);}
friend bool operator < (const node &a, const node &b){
return a.x == b.x ? a.y < b.y : a.x < b.x;
}
} p[3010]; inline int solve(int l, int r){
int k = (int)log2((double)(r - l + 1));
return min(f[l][k], f[r - (1 << k) + 1][k]);
} void work(){
rep(i, 1, n) f[i][0] = a[i];
rep(j, 1, 20) rep(i, 1, n)
if ((i + (1 << j) - 1) <= n) f[i][j] = min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
} int main(){ scanf("%d%d", &n, &k);
rep(i, 1, n) scanf("%d", a + i); work(); scanf("%d", &m);
rep(i, 1, m) p[i].scan();
sort(p + 1, p + m + 1); rep(i, 1, m - 1){
rep(j, i + 1, m){
int cnt;
int now = solve(p[i].x, p[j].x);
if (p[i].y > now && p[j].y > now)
cnt = p[i].y - now + p[j].y - now + abs(p[i].x - p[j].x);
else cnt = abs(p[i].x - p[j].x) + abs(p[i].y - p[j].y);
if (cnt <= k) ++ans;
}
} printf("%d\n", ans);
return 0; }

最后是困难版本

满足$1 <= n <= 200000, 1 <= m <= 200000$

这个时候不能两两枚举点对来统计了

注意到$h[i] <= 20$,这是一个很重要的条件

对于当前在第$i$列的某个点,我们发现在他之后的$max(0, k - 40)$列中的所有点

这些点不用考虑,一定符合条件

因位距离最大值为$ k - 40 + max(h[i]) + max(h[j]) <= k$,所以一定符合条件

同理我们也发现,第$i + k$之后的点肯定不符合条件

那么我们只要枚举$i + max(0, k - 40) + 1$ 到 $i + k$ 这些列中的所有点就可以了

做的时候维护一个前缀和即可。

时间复杂度$O(mlogm + mh^{2})$

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 200100; int a[N];
int f[N][22];
int n, m, k;
long long ans = 0;
long long c[N];
int g[N][22]; vector <int> v[N]; struct node{
int x, y;
void scan(){ scanf("%d%d", &x, &y);}
friend bool operator < (const node &a, const node &b){
return a.x == b.x ? a.y < b.y : a.x < b.x;
}
} p[N]; inline int solve(int l, int r){
int k = (int)log2((double)(r - l + 1));
return min(f[l][k], f[r - (1 << k) + 1][k]);
} void work(){
rep(i, 1, n) f[i][0] = a[i];
rep(j, 1, 20) rep(i, 1, n)
if ((i + (1 << j) - 1) <= n) f[i][j] = min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
} int main(){ scanf("%d%d", &n, &k); rep(i, 1, n) scanf("%d", a + i); work(); scanf("%d", &m);
rep(i, 1, m) p[i].scan();
sort(p + 1, p + m + 1);
memset(g, 0, sizeof g); rep(i, 1, m) ++c[p[i].x];
rep(i, 1, n) c[i] += c[i - 1];
rep(i, 1, m){
v[p[i].x].push_back(i);
g[p[i].x][p[i].y] = 1;
} rep(i, 1, m){
rep(j, p[i].y + 1, a[p[i].x]) if (abs(j - p[i].y) <= k && g[p[i].x][j]) ++ans;
int cnt = p[i].x + k - 40;
if (cnt > n) cnt = n; if (cnt > p[i].x){
long long xx = c[cnt], yy = c[p[i].x];
ans += xx - yy;
} int now = p[i].x + k;
if (now > n) now = n; rep(j, max(cnt + 1, p[i].x + 1), now){ for (auto u : v[j]){
int cnt;
int now = solve(p[i].x, p[u].x);
if (p[i].y > now && p[u].y > now)
cnt = p[i].y - now + p[u].y - now + abs(p[i].x - p[u].x);
else cnt = abs(p[i].x - p[u].x) + abs(p[i].y - p[u].y); if (cnt <= k) ++ans; } } } printf("%lld\n", ans);
return 0; }

计蒜客 微软大楼设计方案(RMQ)的更多相关文章

  1. 计蒜课/ 微软大楼设计方案/中等(xjb)

    题目链接:https://nanti.jisuanke.com/t/15772 题意:中文题诶- 思路:对于坐标为p1(x1, y1), p2(x2, y2) 的两个核心, 其中 x1 <= x ...

  2. 计蒜客 作弊揭发者(string的应用)

    鉴于我市拥堵的交通状况,市政交管部门经过听证决定在道路两侧安置自动停车收费系统.当车辆驶入车位,系统会通过配有的摄像头拍摄车辆画面,通过识别车牌上的数字.字母序列识别车牌,通过连接车管所车辆信息数据库 ...

  3. 计蒜客的一道题dfs

    这是我无聊时在计蒜客发现的一道题. 题意: 蒜头君有一天闲来无事和小萌一起玩游戏,游戏的内容是这样的:他们不知道从哪里找到了N根不同长度的木棍, 看谁能猜出这些木棍一共能拼出多少个不同的不等边三角形. ...

  4. 计蒜客模拟赛5 D2T1 成绩统计

    又到了一年一度的新生入学季了,清华和北大的计算机系同学都参加了同一场开学考试(因为两校兄弟情谊深厚嘛,来一场联考还是很正常的). 不幸的是,正当老师要统计大家的成绩时,世界上的所有计算机全部瘫痪了. ...

  5. 计蒜客 等边三角形 dfs

    题目: https://www.jisuanke.com/course/2291/182238 思路: 1.dfs(int a,int b,int c,int index)//a,b,c三条边的边长, ...

  6. 计蒜客 方程的解数 dfs

    题目: https://www.jisuanke.com/course/2291/182237 思路: 来自:https://blog.csdn.net/qq_29980371/article/det ...

  7. 计蒜客 买书 dfs

    题目: https://www.jisuanke.com/course/2291/182236 思路: 递归解决,从第一本书开始,每本书都有两种选择: //index是book里面每本书价格的下标, ...

  8. 计蒜客:Entertainment Box

    Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fight ...

  9. 爬虫acm比赛成绩(多页成绩整合在一起、获取复制不了的数据)(hihocoder、计蒜客)

    https://github.com/congmingyige/web-crawler_rank-of-competition-in-JiSuanKe-and-hihocoder 1. 计蒜客(获取复 ...

随机推荐

  1. 不使用脚手架的 vue 应用

    工作中的项目不止有页面繁多的模块化项目,还会只有一两个页面的类似于填写信息参与活动的活动页.这个时候,就可以回归以前的三剑客模式,在 index.html 里引用 vue.js 进行开发. 关键点: ...

  2. GoF23种设计模式之行为型模式之迭代器模式

    一.概述    给定一种语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子.二.适用性1.当访问一个聚合对象的内容而无需暴露它的内部表示的时候.2.当对聚合对象的多 ...

  3. JAVA基础篇—String和StringBuffer

    区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringBuffer类对象为可修改对象,可以通过append() ...

  4. PTA 银行排队问题之单队列多窗口加VIP服务 队列+模拟

    假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选择时,假设顾客总是选择编号最小的窗口. 有些银行会给 ...

  5. XenServer 6.5 安装

    为了方便截图我下面的所有操作都是在VMware Workstation 11 上面完成的,但在之后的所有Citrix产品的操作中都将会在物理环境完成,物理机安装XS的步骤和下面是相同的. 1.打开Wo ...

  6. loj2280 「FJOI2017」矩阵填数

    状压 dp.参考there #include <algorithm> #include <iostream> #include <cstring> #include ...

  7. loj2000 「SDOI2017」数字表格

    there #include <iostream> #include <cstring> #include <cstdio> using namespace std ...

  8. How to install redis server on CentOS 7 / RHEL 7

    在本教程中,我们将学习如何在CentOS 7 / RHEL 7上安装Redis服务器. redis的缩写是REmote DIctionary Server. 它是最流行的开源,高级键值缓存和存储之一. ...

  9. Leetcode 447.回旋镖的数量

    回旋镖的数量 给定平面上 n 对不同的点,"回旋镖" 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序). 找 ...

  10. 顺序表ans链性表

    #include<stdio.h>#include<malloc.h>#include<string.h>typedef int ElemType;typedef ...