HDU 5714 树状数组 + 离散化
来源:互联网 发布:中电云集 php版本太低 编辑:程序博客网 时间:2024/06/07 20:24
题意:
思路:
首先假定向左方向的船都是静止的,那么船要么向右移动要么静止。
对于区间是[L,R]的船,我们根据视角计算出在岸上可以观察这艘船的位置区间[l,r],因为拍摄是可以等待船移动的,因为所有船的速度是一样的,如果我们选定了某个位置,那么这个位置能看到一些静止的船,除此之外,还可以等待该位置左边的且向右行驶的船移动到该位置,所以其实就是在当前位置pos的左边找一个位置pos',使得pos'位置上能看到的向右行驶的船数+pos位置上静止的船数最大即可。
这样就可以利用树状数组来维护,每次区间左端点+1,右端点后一位-1,然后求得的sum(i)就是当前位置能看到的船数,向左和向右的船分别用一个BIT来维护。
另外需要注意到的一点是x,y的区间范围还是很大,需要离散化。
代码:
#include <bits/stdc++.h>using namespace std;const int MAXN = 22222;struct BIT {int n;int C[MAXN];void init(int n) {this->n = n;memset(C, 0, sizeof(C));}int lowbit(int x) {return x & -x;}void add(int x, int y) {while (x <= n) {C[x] += y;x += lowbit(x);}}int sum(int x) {int res = 0;while (x) {res += C[x];x -= lowbit(x);}return res;}} lbit, rbit;struct Boat {int l, r, dir;};vector <Boat> boat;int discre(int n) {int Max = 0;vector <int> vec;for (int i = 0; i < (int)boat.size(); i++) {vec.push_back(boat[i].l);vec.push_back(boat[i].r);}sort(vec.begin(), vec.end());vec.erase(unique(vec.begin(), vec.end()), vec.end());for (int i = 0; i < (int)boat.size(); i++) {boat[i].l = lower_bound(vec.begin(), vec.end(), boat[i].l) - vec.begin() + 1;boat[i].r = lower_bound(vec.begin(), vec.end(), boat[i].r) - vec.begin() + 1;Max = max(Max, max(boat[i].l, boat[i].r));}return Max;}int main() {// freopen("in.txt", "r", stdin);int T, cs = 0;scanf("%d", &T);while (T--) {int n;scanf("%d", &n);boat.clear();for (int i = 1; i <= n; i++) {int l, r, d, dir;scanf("%d%d%d%d", &l, &r, &d, &dir);double len = (d - (r - l) / 2.0), mid = (l + r) / 2.0;if (len >= 0)boat.push_back((Boat) {mid - len, mid + len, dir});}int m = discre(n);lbit.init(m); rbit.init(m); for (int i = 0; i < boat.size(); i++) {if (boat[i].dir == -1) {lbit.add(boat[i].l, 1);lbit.add(boat[i].r + 1, -1);} else {rbit.add(boat[i].l, 1);rbit.add(boat[i].r + 1, -1);} }int maxright = 0, ans = 0;for (int i = 1; i <= m; i++) {int leftcnt = lbit.sum(i);maxright = max(maxright, rbit.sum(i));ans = max(ans, leftcnt + maxright);}printf("Case #%d:\n%d\n", ++cs, ans);}return 0;}
0 0
- HDU 5714 (离散化 树状数组)
- HDU 5714 树状数组 + 离散化
- HDU-4358-树状数组+离散化
- hdu 3874 树状数组+离散化
- HDU 5101 Select --离散化+树状数组
- hdu 4605(树状数组+离散化)
- HDU 3743 (树状数组,离散化)
- HDU 5654 (树状数组 离散化)
- HDU 5372 (树状数组 离散化)
- hdu 5372 离散化加树状数组
- hdu 5792 离散化+树状数组
- 树状数组+离散化(hdu 5862)
- hdu 5877 dfs+离散化+树状数组
- Hdu 4325 Flowers 树状数组+离散化
- hdu 5877 树状数组 +离散化 +树
- hdu 5877(树状数组+离散化)
- hdu 5862(离散化+树状数组)
- 反向树状数组 + 离散化 HDU 5372
- 关于postgresql同一表中的同一列可以填加多个相同索引的问题
- 空间金字塔池化 Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition
- replace()结合正则表达式
- 用自己的域名重定向到csdn博客首页
- 安卓实现自定义View的三种方式
- HDU 5714 树状数组 + 离散化
- Problem--136A--Codeforces--Presents
- pandas数据样本行列选取
- 使用http的post方式与网络交互通信
- 关于从相机界面返回上一级页面出现黑屏的问题
- 自己模拟二级弹窗(附源码)
- 韩信点兵
- 千锋学员自述学习Hadoop历程
- Android材料设计动画之触摸反馈