UVALive 7141 BombX(离散化+线段树)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=648&page=show_problem&problem=5153
In an infinite chess board, some pawns are placed on some cells.
You have a rectangular bomb that is W width and H height.
The bomb’s orientation is fixed, you can’t rotate it. The bomb
can only be placed on an entirely unoccupied area. The bomb
explodes both horizontally and vertically, killing all pawns that
are in the cross shape (see picture on the right).
Your mission is to choose the placement of the bomb, and
maximize the number of bombed pawns.
The picture corresponds to the first test case in the sample.
Input
The first line of the input gives the number of test cases, T. T
test cases follow. Each test case starts with a line containing N,
W, H, indicating number of pawns, width of the bomb, height of
the bomb, respectively.
N lines follow. Each line contains 2 integers: x, y, indicating there is a pawn on cell (x, y). No two
pawns are in the same cell.
Output
For each test case, output one line containing ‘Case #x: y’, where x is the test case number (stating
from 1) and y is the maximum number of bombed pawns.
题目大意:无穷大的地图上有n个兵,现给出一个W*H的炸弹(不可以旋转),一次能炸死它所在的行和列的所有小兵,不能放在小兵的上面。问:一次最多能干掉多少个小兵?
思路:最初的想法就是,枚举所有可以放炸弹的位置,看看哪个位置可以炸的小兵最多。如果用a[x, y]来表示炸弹放在以(x, y)为左下角的矩阵里,能炸多少小兵(不能放的时候为0)。那么显然结果就是a[x, y]的最大值。(作用:对拍)
然而这并不能AC。我们现在改成只考虑a数组的一个维度,让x一步一步往右移动,修改a数组。当我们在x位置的时候,用a[y]来表示,炸弹放在以(x, y)为左下角的矩阵里,能炸多少小兵。假设我们不考虑能不能放的问题,当x不断往右移动(不断+1)的时候,a数组并不会发生任何变化。我们可以另开一个数组cnt[],当我们遇到一个点(x0, y0)的时候,给cnt[y0-h+1..y0]都加上一个1,表示有一个点导致这个区间不能放入炸弹(同理当某个点离开区间[x..x+w-1]的时候,对应的区间都减去1)。那么我们在移动x的时候,要求的就是max{a[y] | cnt[y]=0}。
然而这还是不能AC。注意到在x往右移动的过程中,若没有点进出区间[x..x+w-1]的时候,cnt[]数组是不会发生变化的,这时,离散化x坐标的作用就发挥出来了。同时,y坐标也是可以离散化的,因为每次更改的区间都是可以提前预知的。
此时复杂度已经变成了O(n^2),当然还是不够的。注意到每次修改cnt[]数组都是区间修改,这里使用线段树优化,复杂度变为O(nlogn),可以AC。
此题细节多多,大家多想想。
PS:范围写的是w, h≥0,于是我加了一个assert,目前数据是没有w, h=0的情况的。如果rejudge RE了我就知道怎么回事啦!
代码(0.386S):
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <tuple>
#include <cassert>
using namespace std;
typedef long long LL; const int MAXN = ;
const int MAXL = ;
const int MAXT = ; struct Node {
int x, y;
void read() {
scanf("%d%d", &x, &y);
}
} p[MAXN]; int val[MAXL]; vector<int> ytmp; int T, n, w, h; void inithash() {
ytmp.clear();
for(int i = ; i < n; ++i) {
ytmp.push_back(p[i].y - h + );
ytmp.push_back(p[i].y + );
}
sort(ytmp.begin(), ytmp.end());
ytmp.erase(unique(ytmp.begin(), ytmp.end()), ytmp.end());
} int yhash(int y) {
return lower_bound(ytmp.begin(), ytmp.end(), y) - ytmp.begin();
} void initval() {
memset(val, , ytmp.size() * sizeof(int));
for(int i = ; i < n; ++i) {
val[yhash(p[i].y - h + )]++;
val[yhash(p[i].y + )]--;
}
partial_sum(val, val + ytmp.size(), val);
} int root(int l, int r) {
return (l + r) | (l != r);
}
#define mid ((l + r) >> 1)
#define rt root(l, r)
#define ll root(l, mid)
#define rr root(mid + 1, r)
int ban[MAXT], maxt[MAXT]; void update(int l, int r) {
if(!ban[rt]) maxt[rt] = (l == r ? val[l] : max(maxt[ll], maxt[rr]));
else maxt[rt] = ;
} void build(int l, int r) {
if(l == r) {
maxt[rt] = val[l];
} else {
build(l, mid);
build(mid + , r);
update(l, r);
}
} void modify_ban(int l, int r, int a, int b, int val) {
if(a <= l && r <= b) {
ban[rt] += val;
} else {
if(a <= mid) modify_ban(l, mid, a, b, val);
if(mid < b) modify_ban(mid + , r, a, b, val);
}
update(l, r);
} int query() {
return maxt[root(, ytmp.size() - )];
} vector<int> allx;
vector<tuple<int, int, int> > xtmp;
#define gpos(t) get<0>(t)
#define gval(t) get<1>(t)
#define gy(t) get<2>(t) int solve() {
xtmp.clear();
for(int i = ; i < n; ++i) {
xtmp.push_back(make_tuple(p[i].x - w + , , p[i].y));
xtmp.push_back(make_tuple(p[i].x + , -, p[i].y));
}
sort(xtmp.begin(), xtmp.end()); allx.clear();
for(int i = ; i < n; ++i) {
allx.push_back(p[i].x - w + );
allx.push_back(p[i].x + );
}
sort(allx.begin(), allx.end());
allx.erase(unique(allx.begin(), allx.end()), allx.end()); int res = query(), cnt = ;
size_t i = ;
for(int x : allx) {
while(i < xtmp.size() && gpos(xtmp[i]) <= x) {
auto t = xtmp[i++];
modify_ban(, ytmp.size() - , yhash(gy(t) - h + ), yhash(gy(t) + ) - , gval(t));
cnt += gval(t);
}
res = max(res, query() + cnt);
}
return res;
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d%d", &n, &w, &h);
assert(w > && h > );
for(int i = ; i < n; ++i)
p[i].read();
inithash();
initval();
build(, ytmp.size() - );
printf("Case #%d: %d\n", t, solve());
}
}
UVALive 7141 BombX(离散化+线段树)(2014 Asia Shanghai Regional Contest)的更多相关文章
- UVALive 7148 LRIP(树的分治+STL)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7143 Room Assignment(组合数学+DP)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...
- hdu5071 2014 Asia AnShan Regional Contest B Chat
模拟题: add的时候出现过的则不再添加 close的时候会影响到top rotate(Prior.Choose)的时候会影响到top /*============================== ...
- 2014 Asia AnShan Regional Contest --- HDU 5073 Galaxy
Galaxy Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5073 Mean: 在一条数轴上,有n颗卫星,现在你可以改变k颗 ...
- dp --- 2014 Asia AnShan Regional Contest --- HDU 5074 Hatsune Miku
Hatsune Miku Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5074 Mean: 有m种音符(note),现在要从 ...
随机推荐
- java基础-包
浏览以下内容前,请点击并阅读 声明 为了使类型更容易查找和使用,避免命名冲突,以及可视范围的控制,程序员一般将相关的一些类型组合到一个包中.组合的类型包括类,接口,枚举和注释,枚举是一种特殊的类,而注 ...
- 一个叫vtysh的命令行shell
目前的工作就是在这个叫vtysh的工具上进行修改,终极目的是把它作为一个代替shell的东西.以此来屏蔽用户和系统之间的接触,减少用户对系统的操作权限.
- bzoj刷水
因为最近的生活太颓废总是不做题而被老师D了一番, 所以今天晚上到bzoj上去刷了几道水题..... bzoj 4320: ShangHai2006 Homework 题目大意 维护一个支持两个操作 ...
- ArcEngine奇怪异常:HRESULT:0x80040351
错误如图 根据该博客,http://blog.csdn.net/u011609113/article/details/51155116, 显示该错误为Duplicate Field Names wit ...
- javascript平时例子⑩(表情发送)
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- CNUOJ 535 黑魔法师之门
先摆出题 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 背景: 经过了16个工作日的紧张忙碌,未来的人类终于收集到了足够的能源 ...
- 对接第三方支付接口-获取http中的返回参数
这几天对接第三方支付接口,在回调通知里获取返回参数,有一家返回的json格式,请求参数可以从标准输入流中获取. //1.解析参数 , 读取请求内容 BufferedReader br; String ...
- P1041 传染病控制
#include <bits/stdc++.h> using namespace std; const int maxn = 301; std::vector<int> son ...
- maven 依赖
依赖排除 当一个项目A依赖项目B,而项目B同时依赖项目C,如果项目A中因为各种原因不想引用项目C,在配置项目B的依赖时,可以排除对C的依赖. 示例(假设配置的是A的pom.xml,依赖关系为:A -- ...
- Angular工作笔记
$watch中进行深层次操作时,配合radio或者checkbox的ng-click或ng-change操作,会使checkbox失效,不知原因