拉普兰德的愿望【曼哈顿距离转切比雪夫距离】

题目链接


 在上一篇文章中,我们知道了切比雪夫距离

  现在,我们来认识一下,曼哈顿距离转换成切比雪夫距离有什么好处?——更加简单的处理“曼哈顿距离大于等于D的点对数目”。

我们现在的曼哈顿距离假设为(x, y),那么,我们假设有这样的切比雪夫距离(\frac{a + b}{2}, \frac{a - b}{2}) = (x, y)

连立两个不等式,得到a = x + yb = x - y

好了,我们将原来的问题转化为了求切比雪夫距离大于等于D的点对数,这样问题就简单了,我们可以利用单调队列+树状数组等数组结构来维护了。

因为只要保证一维大于等于D,就是点对是合法的。

所以我们首先对x升序,于是,用单调队列O(N)的来维护,然后就是查询y了,树状数组查询,基本操作了。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f3f3f3f3f
#define eps 1e-8
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, D, L, tx, ty, top, tail, Lsan[maxN], _UP;
struct node
{
    int x, y;
    node(int a=0, int b=0):x(a), y(b) {}
    void In()
    {
        scanf("%d%d", &tx, &ty);
        x = tx + ty; y = tx - ty;
    }
    friend bool operator < (node e1, node e2) { return e1.x < e2.x; }
} a[maxN], que[maxN];
struct BIT_Tree
{
    int tree[maxN], all = 0;
    inline void update(int x, int val)
    {
        all += val;
        while(x <= N)
        {
            tree[x] += val;
            x += lowbit(x);
        }
    }
    inline int query(int x)
    {
        int sum = 0;
        while(x)
        {
            sum += tree[x];
            x -= lowbit(x);
        }
        return sum;
    }
} b;
int main()
{
    top = tail = 0;
    scanf("%d%d%d", &N, &D, &L);
    for(int i=1; i<=N; i++) { a[i].In(); Lsan[i] = a[i].y; }
    sort(a + 1, a + N + 1);
    sort(Lsan + 1, Lsan + N + 1);
    _UP = (int)(unique(Lsan + 1, Lsan + N + 1) - Lsan - 1);
    ll ans = 0;
    for(int i=1, id; i<=N; i++)
    {
        while(top < tail && que[top].x <= a[i].x - D)
        {
            id = (int)(lower_bound(Lsan + 1, Lsan + _UP + 1, que[top].y) - Lsan);
            b.update(id, -1);
            top++;
        }
        ans += top;
        id = (int)(upper_bound(Lsan + 1, Lsan + _UP + 1, a[i].y - D) - Lsan - 1);
        ans += b.query(id);
        id = (int)(lower_bound(Lsan + 1, Lsan + _UP + 1, a[i].y + D) - Lsan - 1);
        ans += tail - top - b.query(id);
        que[tail++] = a[i];
        id = (int)(lower_bound(Lsan + 1, Lsan + _UP + 1, a[i].y) - Lsan);
        b.update(id, 1);
    }
    printf("%lld\n", ans);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
牛顿-拉普森迭代算法是一种用于求解逻辑回归的方法。该算法的目标是通过最大化逻辑回归的对数似然函数来找到最优参数。牛顿-拉普森迭代算法结合了牛顿法和拉普拉斯近似的思想。 牛顿-拉普森迭代算法的步骤如下: 1. 初始化参数向量θ为0。 2. 在每次迭代中,计算似然函数的梯度和海塞矩阵。梯度表示函数在某一点的斜率,海塞矩阵表示函数的曲率。 3. 利用海塞矩阵和梯度计算牛顿方向,即在当前位置下降最快的方向。 4. 更新参数向量θ,使其朝着牛顿方向移动一定步长。 5. 重复步骤2-4,直到满足收敛条件。 牛顿-拉普森迭代算法相比于批量梯度下降算法收敛更快,并且通常只需要迭代更少的次数才能达到最小值。然而,一次牛顿-拉普森迭代可能比一次梯度下降代价更高,因为它需要找到并计算一个n x n的海塞矩阵。但是,只要参数维度n不是太大,总体上来说牛顿-拉普森迭代算法仍然更快。 总而言之,牛顿-拉普森迭代算法是一种用于求解逻辑回归的优化算法,它通过最大化对数似然函数来找到最优参数向量θ。该算法结合了牛顿法和拉普拉斯近似的思想,能够更快地收敛到最小值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [CS229 Part2 分类与逻辑回归](https://blog.csdn.net/u010665216/article/details/77620930)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值