BZOJ_3476_[Usaco2014 Mar]The Lazy Cow_扫描线+切比雪夫距离
BZOJ_3476_[Usaco2014 Mar]The Lazy Cow_扫描线+切比雪夫距离
Description
It's a hot summer day, and Bessie the cow is feeling quite lazy. She wants to locate herself at a position in her field so that she can reach as much delicious grass as possible within only a short distance. There are N patches of grass (1 <= N <= 100,000) in Bessie's field. The ith such patch contains g_i units of grass (1 <= g_i <= 10,000) and is located at a distinct point (x_i, y_i) in the field (0 <= x_i, y_i <= 1,000,000). Bessie would like to choose a point in the field as her initial location (possibly the same point as a patch of grass, and possibly even a point with non-integer coordinates) so that a maximum amount of grass is within a distance of K steps from this location (1 <= K <= 2,000,000). When Bessie takes a step, she moves 1 unit north, south, east, or west of her current position. For example, to move from (0,0) to (3,2), this requires 5 total steps. Bessie does not need to take integer-sized steps -- for example, 1 total step could be divided up as half a unit north and half a unit east. Please help Bessie determine the maximum amount of grass she can reach, if she chooses the best possible initial location.
在二维平面有N个点。每个点都有个权值。
现在希望你选择某个点,如果其它点到这个点的距离不超过K。
则牛就可以吃到这个点的东西。
现在希望吃到的东西的权值越大越好。请求出这个值来。
Input
* Line 1: The integers N and K.
* Lines 2..1+N: Line i+1 describes the ith patch of grass using 3 integers: g_i, x_i, y_i.
Output
* Line 1: The maximum amount of grass Bessie can reach within K steps, if she locates herself at the best possible initial position.
Sample Input
7 8 6
3 0 0
4 6 0
1 4 2
INPUT DETAILS: Bessie is willing to take at most 3 steps from her
initial position. There are 4 patches of grass. The first contains 7
units of grass and is located at position (8,6), and so on.
Sample Output
8
曼哈炖距离在处理‘距离不超过K’这种问题时比较麻烦,因为要维护一个斜45度的正方形。
于是逆时针旋转45度,使得x->x-y,y->x+y,转化为切比雪夫距离就只需要维护一个正方形。
然后就是扫描线的套路,设f[i]表示左下的纵坐标为i时的答案,用线段树维护y轴的f值。
每次相当于区间加和全局最大值。
注意这里y的范围是[0,2000000],空间要开够。
代码:
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 2000050
#define inf 2000000
#define ls p<<1
#define rs p<<1|1
struct Point {
int x,y,v;
bool operator < (const Point &p) const {
return x<p.x;
}
}a[100050];
int n,t[N<<2],add[N<<2],K;
void pushdown(int p) {
if(add[p]) {
int d=add[p]; add[ls]+=d; add[rs]+=d;
t[ls]+=d; t[rs]+=d; add[p]=0;
}
}
void update(int l,int r,int x,int y,int v,int p) {
if(x<=l&&y>=r) {
t[p]+=v; add[p]+=v; return ;
}
pushdown(p);
int mid=(l+r)>>1;
if(x<=mid) update(l,mid,x,y,v,ls);
if(y>mid) update(mid+1,r,x,y,v,rs);
t[p]=max(t[ls],t[rs]);
}
int main() {
scanf("%d%d",&n,&K); K<<=1;
int i,x,y;
for(i=1;i<=n;i++) {
scanf("%d%d%d",&a[i].v,&x,&y); a[i].x=x-y; a[i].y=x+y;
}
sort(a+1,a+n+1);
int ans=0;
int j=1;
for(i=1;i<=n;i++) {
while(a[i].x-a[j].x>K) update(0,inf,max(0,a[j].y-K),a[j].y,-a[j].v,1),j++;
update(0,inf,max(0,a[i].y-K),a[i].y,a[i].v,1);
if(ans<t[1]) ans=t[1];
}
printf("%d\n",ans);
}
BZOJ_3476_[Usaco2014 Mar]The Lazy Cow_扫描线+切比雪夫距离的更多相关文章
- BZOJ3476 : [Usaco2014 Mar]The Lazy Cow
旋转坐标系后转化为正方形,$x'=x+y$,$y'=x-y+1000001$,$k'=2k-1$ 两根扫描线从左往右扫 f[i]表示y坐标下边界为i时的价值和 每次加入/删除一个点等价于一段区间加减 ...
- BZOJ3479: [Usaco2014 Mar]Watering the Fields
3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 81 Solved: ...
- BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )
MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...
- BZOJ 3477: [Usaco2014 Mar]Sabotage( 二分答案 )
先二分答案m, 然后对于原序列 A[i] = A[i] - m, 然后O(n)找最大连续子序列和, 那么此时序列由 L + mx + R组成. L + mx + R = sum - n * m, s ...
- BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案
BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案 题意: 约翰的牧场里有 N 台机器,第 i 台机器的工作能力为 Ai.保罗阴谋破坏一些机器,使得约翰的工作效率变低.保罗可 ...
- bzoj 3479: [Usaco2014 Mar]Watering the Fields
3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 174 Solved ...
- BZOJ_3479_[Usaco2014 Mar]Watering the Fields_Prim
BZOJ_3479_[Usaco2014 Mar]Watering the Fields_Prim Description Due to a lack of rain, Farmer John wan ...
- [Usaco2014 Mar]Sabotage
[Usaco2014 Mar]Sabotage 题目 Farmer John"s arch-nemesis, Farmer Paul, has decided to sabotage Far ...
- BZOJ 3170 & 切比雪夫距离
题意: 给出N个点,在这N个点中选一个点使其它的点与这个点的切比雪夫距离和最小. SOL: TJOI真是...厚道还是防水...这种题目如果知道切比雪夫距离是什么那不就是傻逼题...如果不知道那不就懵 ...
随机推荐
- zoj 2932 The Seven Percent Solution
The Seven Percent Solution Time Limit: 2 Seconds Memory Limit: 65536 KB Uniform Resource Identi ...
- POJ3041:Asteroids【二分图匹配】
二分图的最大匹配=最小顶点覆盖(Konig定理)=最大独立集的补集最大匹配经典的三种模型 这题就是最小顶点覆盖,顺便这题留给我的经验就是调试的时候一定要细心细心再细心对模板的各个细节都要熟!! #i ...
- Gym 215177D 母亲节的礼物
Gym 215177D 母亲节的礼物 Problem : 给n个点m条边的无向图,每个点的度数小于等于7,要求用4种不同的颜色给每个点染色,使得每个点相邻的点中最多只有一个相同颜色的点.(n<= ...
- Shell脚本的编写,sed的使用以及一些正则表达式
Shell脚本的简单编写以及sed的使用 标签(空格分隔): 博客文章 前一阵子为了批量修改Web审计规则,故编写了一个Shell脚本,顺便使用了下sed,顺便把正则表达式也重新学习一遍,感觉还是需要 ...
- HDU 3001【状态压缩DP】
题意: 给n个点m条无向边. 要求每个点最多走两次,要访问所有的点给出要求路线中边的权值总和最小. 思路: 三进制状态压缩DP,0代表走了0次,1,2类推. 第一次弄三进制状态压缩DP,感觉重点是对数 ...
- mongodb按照日期分组统计
目录 1.使用时间格式化方法 2.进行时间补偿(默认当前时区是东八区,即8x3600x1000=28800000) mongodb的默认时间是格林尼治时间,如果是要按照日期进行分组需要注意!!!. 解 ...
- Java实验——输出一个数组里面连续子数组最大的和(二)文件操作
在本周的练习中,主要是对上周的实验进行健壮性的完善,即在所能考虑到的情况之中,尽量使自己的程序能够正常地运行. 在上周的实验中,我已经是在编程过程中考虑到用户输入的错误类型的问题,所以这一方面并没有多 ...
- Linux内存管理-内核的shmall和shmmax参数(性能调优)(转)
内核的shmall和shmmax参数 SHMMAX=配置了最大的内存segment的大小:这个设置的比SGA_MAX_SIZE大比较好. SHMMIN=最小的内存segment的大小 SHMMNI=整 ...
- Java基础教程:tutorialspoint-java
来自turorialspoint的Java基础教程(英文),官网:https://www.tutorialspoint.com/java/index.htm 这个教程在国内已经被翻译成中文(不过是属于 ...
- weblogic线程阻塞性能调优(图解)
转自:http://blog.csdn.net/z69183787/article/details/12647539 声明:出现这个问题有程序方面.网络方面.weblogic设置方面等等原因,此文章主 ...