微软大楼设计方案(困难) 解题报告
来源:互联网 发布:网名生成器软件下载 编辑:程序博客网 时间:2024/05/19 00:43
原题链接:
微软大楼设计方案(困难)
解题思路:
运用动态规划、二分查找。
- 计算每一个hij,i是第i栋楼,j是j层。hij是包括第i栋楼第j层在内以左以下的楼高为j的栋数。
- 运用二分查找、条件不等式求协同值。
- 协同值可能会让int溢出,因此需要long long。
代码:
#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <cstdlib>#include <cassert>#include <cmath>#include <vector>#include <algorithm>using namespace std;struct pos {int x, y;pos(int x = 0, int y = 0) : x(x), y(y) {}bool operator<(const pos p){if (x < p.x) {return true;}else if (x == p.x && y < p.y) {return true;}else {return false;}}};int query(const vector<int>& h1, const vector<int>& h2);int binarySearch(const vector<pos>& posArray, const int value);int main(){int n = 0, k = 0;assert(2 == scanf("%d %d", &n, &k));vector<int> hArray(n + 1);vector<vector<int>> h(n + 1);for (int i = 1; i <= n; i++) {assert(scanf("%d", &hArray[i]) == 1);if (i != 1) {h[i] = h[i - 1];}else {h[i].resize(21);}h[i][hArray[i]]++;}int m = 0;assert(1 == scanf("%d", &m));vector<pos> posArray(m);for (int i = 0; i < m; i++) {assert(2 == scanf("%d %d", &posArray[i].x, &posArray[i].y));}sort(posArray.begin(), posArray.end());long long recouncil = 0;for (int i = 0; i < m; i++) {int right = 0;int rIndex = 0;right = posArray[i].x + k - 38;if (right >= posArray[m - 1].x) {recouncil += m - 1 - i;continue;}else if (right < posArray[i].x) {rIndex = i + 1;}else {rIndex = binarySearch(posArray, right);recouncil += rIndex - 1 - i;}for (int j = rIndex; j < m; j++) {pos start(posArray[i].x, posArray[i].y), end(posArray[j].x, posArray[j].y);if (end < start) {swap(start, end);}if (end.x - start.x > k) {break;}int minY = query(h[posArray[i].x], h[posArray[j].x]);minY = min(minY, start.y);minY = min(minY, end.y);int cost = end.x - start.x + start.y + end.y - 2 * minY;if (cost <= k) {recouncil++;}else if(cost > k + 20) {break;}}}printf("%lld\n", recouncil);return 0;}int query(const vector<int>& h1, const vector<int>& h2){int min = 0;for (int i = 1; i <= 20; i++) {if (h2[i] - h1[i]) {min = i;break;}}return min;}int binarySearch(const vector<pos>& posArray, const int value){int left = 0, right = posArray.size() - 1, mid = 0;while (left < right) {mid = left + ((right - left) >> 1);if (posArray[mid].x <= value) {left = mid + 1;}else {right = mid;}}return left;}
阅读全文
0 0
- 微软大楼设计方案(困难) 解题报告
- 微软大楼的设计方案(解题报告)
- 2017计蒜客第六场 微软大楼设计方案(困难)
- 计蒜客第六场 微软大楼设计方案(困难)
- 微软大楼设计方案(简单)
- 2017 计蒜之道 初赛 第六场 微软大楼设计方案(困难)(rmq+二分优化)@
- 计蒜客 微软大楼设计方案(中等)
- 计蒜客2017 第六场 微软大楼设计方案(中等)
- 【计蒜之道 】微软大楼设计方案
- 微软大楼设计方案 bfs和线段树
- 2017 计蒜之道 初赛 第六场 微软大楼设计方案(中等)【思维+RMQ】
- 2017 计蒜之道 初赛 第六场 微软大楼设计方案(中等)
- 计蒜客-2017 计蒜之道 初赛 第六场-C-微软大楼设计方案(中等)
- 2017计蒜之道初赛第六场-微软大楼设计方案(简单)
- 2017计蒜之道第六场初赛 微软大楼设计方案(中等)(线段树)
- 微软手机信号显示(解题报告)
- JZOJ.4300[NOIP2015模拟11.3]装饰大楼 解题报告
- 【NOI2014】起床困难综合症 解题报告
- view
- php开发App接口
- java中的Collection和Map详解
- r语言矩阵、数据框基本操作及数据读入
- 新兴机器学习算法:迁移学习
- 微软大楼设计方案(困难) 解题报告
- 高斯混合模型视频背景建模的EM算法与Matlab 实现
- PHP开发APP接口(六):APP版本升级以及APP演示
- Android虚线分割Shape
- 打印1到N的最大数
- Android学习路之ProgressBar
- 阿贾克斯Ajax技术
- 关于Class.getResource和ClassLoader.getResource的路径问题
- 操作系统ucore lab6实验报告