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

中文题就不说题意了~

首先是简单版本

满足$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渲染函数&JSX

    Vue推荐在绝大多数情况下使用template来创建你的HTML.然而在一些场景中,你真的需要JavaScript的完全编程能力,这时你可以使用render函数,它比template跟接近编译器. 虚 ...

  2. ThinkPHP5 高级查询之构建分组条件

    ThinkPHP5 高级查询之构建分组条件 一.在tp5中通过where方法如何构建分组条件, 例如:where user_id=$this->user_id and (status in (4 ...

  3. LeetCode(129) Sum Root to Leaf Numbers

    题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...

  4. Python中的魔法函数__repr__和__str__的实质性区别

    str 和 repr 方法:是自定义类的字符串描述,这两种都是比较 Pythonic 的方式去控制对象转化为字符串的方式. 调用这两个方法,返回的都是字符串.但是这两个方法又有一些区别 ** 1 两种 ...

  5. Win磁盘MBR转换为GUID

    title: Win磁盘MBR转换为GUID date: 2018-09-02 11:52:32 updated: tags: [windows,记录,折腾] description: keyword ...

  6. 模拟 - BZOJ 1510 [POI2006] Kra-The Disks

    BZOJ 1510 [POI2006] Kra-The Disks 描述 Johnny 在生日时收到了一件特殊的礼物,这件礼物由一个奇形怪状的管子和一些盘子组成. 这个管子是由许多不同直径的圆筒(直径 ...

  7. [转载]ExtJs4 笔记(2) ExtJs对js基本语法扩展支持

    作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/) 本篇主要介绍一下ExtJs对JS基本语法的扩展支持,包括动态加载.类的封装等. 一.动态引 ...

  8. python - 接口自动化测试 - ReadExcel - 读取测试数据封装

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: read_excel.py @ide: PyCharm C ...

  9. 初学-BeautifulSoup爬取豆瓣页面

    # -*- coding: utf-8 -*-import osimport urllibimport urllib2from bs4 import BeautifulSoup headers = { ...

  10. 聊聊、Nginx 参数合法性

    我们接着上篇文章来讲讲 ngx_get_options 函数. 这个函数就在 nginx.c 文件中,我们来看看. 参数 argc,argv 我们在前面的文章中都已经提到了,在这里我们看 ngx_ge ...