题目链接: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)的更多相关文章

  1. UVALive 7148 LRIP(树的分治+STL)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  2. 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 ...

  3. UVALive 7143 Room Assignment(组合数学+DP)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  4. UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  5. UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  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. ...

  7. hdu5071 2014 Asia AnShan Regional Contest B Chat

    模拟题: add的时候出现过的则不再添加 close的时候会影响到top rotate(Prior.Choose)的时候会影响到top /*============================== ...

  8. 2014 Asia AnShan Regional Contest --- HDU 5073 Galaxy

    Galaxy Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5073 Mean: 在一条数轴上,有n颗卫星,现在你可以改变k颗 ...

  9. 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),现在要从 ...

随机推荐

  1. jQuery学习笔记--JqGrid相关操作 方法列表(上)

    1.获得当前列表行数:$("#gridid").getGridParam("reccount"); 2.获取选中行数据(json):$("#gridi ...

  2. Hibernate学习笔记4

    一.关于联合主键的映射测试实例 实体类: package com.***.comBineKey;public class Person { private Person_pk pk; private ...

  3. 自定义一个字母Button

    package com.example.administrator.yunstore.widget; import android.content.Context; import android.gr ...

  4. 在ubuntu 12.04 x64下编译hadoop2.4

    自己编译hadoop:x64 1.安装依赖包 sudo apt-get install g++ autoconf automake libtool cmake zlib1g-dev pkg-confi ...

  5. 【Java】实战Java虚拟机之五“开启JIT编译”

    今天开始实战Java虚拟机之五“开启JIT编译” 总计有5个系列 实战Java虚拟机之一“堆溢出处理” 实战Java虚拟机之二“虚拟机的工作模式” 实战Java虚拟机之三“G1的新生代GC” 实战Ja ...

  6. sql语句 之聚合函数

      聚合分析 在访问数据库时,经常需要对表中的某列数据进行统计分析,如求其最大值.最小值.平均值等.所有这些针对表中一列或者多列数据的分析就称为聚合分析. 在SQL中,可以使用聚合函数快速实现数据的聚 ...

  7. C语言

    HTML的学习早已落下帷幕,我们已经进入了C语言的学习,这段时间时间主要学了运算符.表达式.循环语句以及数组和字符串,感觉到了一种朦朦胧胧懂得尴尬. 运算符主要包括:算术运算符.赋值运算符.关系运算符 ...

  8. Python强化训练笔记(六)——让字典保持有序性

    python的字典是一个非常方便的数据结构,使用它我们可以轻易的根据姓名(键)来找到他的成绩,排名等(值),而不用去遍历整个数据集. 例如:{'Lee': [1, 100], 'Jane': [2, ...

  9. run time

    http://www.cnblogs.com/yswdarren/p/3619303.html

  10. iOS柱状图的绘制

    前段时间公司要求做一个统计,用swift3.0写的,因此整理了一下demo,直接上图 代码下载地址:https://github.com/minyahui/MYHChartView