CodeForces - 650A(104/600)

博士曼哈顿和朋友丹尼尔必须尽快警告处于危险中的监视者们。他们需要计算监视者之间的距离,但两人的计算方式不同。博士曼哈顿使用绝对差值,而丹尼尔使用欧几里得距离。题目要求找出两者计算结果相同的监视者对数。输入包含监视者数量和坐标,输出符合条件的对数。示例展示了计算过程和结果。

Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi).

They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers the distance between watchmen i and j to be |xi - xj| + |yi - yj|. Daniel, as an ordinary person, calculates the distance using the formula .

The success of the operation relies on the number of pairs (i, j) (1 ≤ i < j ≤ n), such that the distance between watchman i and watchmen j calculated by Doctor Manhattan is equal to the distance between them calculated by Daniel. You were asked to compute the number of such pairs.

Input
The first line of the input contains the single integer n (1 ≤ n ≤ 200 000) — the number of watchmen.

Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≤ 109).

Some positions may coincide.

Output
Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel.

Example
Input
3
1 1
7 5
1 5
Output
2
Input
6
0 0
0 1
0 2
-1 1
0 1
1 1
Output
11
Note
In the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances.

很容易能证明这种点必须是同一条x或者y

然后只要map大力去重就可以了

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<functional>
#include<vector>
#include<cstring>
#include<string>
#include<map>
#include<cmath>
#include<stdio.h>
#include<stack>
#include<set>
using namespace std;
map<long long , long long >lsx, lsy;
long long  xx[200001], yy[200001],lisx[200001],lisy[200001],xg=0,yg=0;
long long  xxi[200001], yyi[200001];
map<long long , long long >mp[200001];
int main()
{
    long long  n,q,w;
    cin >> n;
    for (long long  a = 1; a <= n; a++)
    {
        scanf("%I64d%I64d", &q, &w);
        xx[a] = q, yy[a] = w;
        if (!lsx[q])
        {
            lisx[++lisx[0]] = q;
            lsx[q] = 1;
        }
        if (!lsy[w])
        {
            lisy[++lisy[0]] = w;
            lsy[w] = 1;
        }
    }
    lsx.clear(), lsy.clear();
    xg = lisx[0], yg = lisy[0];
    sort(lisx + 1, lisx + xg + 1);
    sort(lisy + 1, lisy + yg + 1);
    for (long long  a = 1; a <= xg; a++)lsx[lisx[a]] = a;
    for (long long  a = 1; a <= yg; a++)lsy[lisy[a]] = a;
    for (long long  a = 1; a <= n; a++)
    {
        xxi[lsx[xx[a]]]++;
        yyi[lsy[yy[a]]]++;
    }
    long long dan = 0;
    for (long long  a = 1; a <= xg; a++)dan += xxi[a]*(xxi[a] - 1)/2;
    for (long long  a = 1; a <= yg; a++)dan += yyi[a]*(yyi[a] - 1)/2;
    for (long long  a = 1; a <= n; a++)
    {
        long long  qq = lsx[xx[a]], ww = lsy[yy[a]];
        mp[qq][ww]++;
    }
    for (long long  a = 1; a <= xg; a++)
    {
        for (auto b : mp[a])
        {
            dan -= b.second*(b.second - 1) / 2;
        }
    }
    printf("%I64d", dan);
}
### 问题分析 Codeforces Contest 1215 Problem D,题名为 "Ticket Exchange System",主要涉及图论和贪心策略的应用。题目要求在特定条件下,寻找最优路径,使得某种目标函数最大化或最小化。 ### 解题思路 1. **问题建模** 本题通常可以建模为一个图上的最短路径问题,但目标函数可能不是传统的路径长度之和,而是某种组合函数。例如,题目可能要求在路径中选择某些节点,使得它们的某些属性满足特定条件,例如最大化最小值、最小化最大值等。 2. **使用 Dijkstra 算法变体** 由于目标函数可能涉及最大化某个属性(例如路径上最小边权的最大值),Dijkstra 算法可以被修改以适应这种需求。传统的 Dijkstra 用于求解最短路径,但在这种情况下,可以维护一个优先队列,每次扩展当前节点时,更新目标函数的值[^1]。 3. **状态表示与更新** 状态通常表示为 `(current_node, current_value)`,其中 `current_value` 是到达 `current_node` 时的目标函数值。如果通过某条边能够得到更优的 `current_value`,则更新对应的状态。这类似于松弛操作,但目标函数的更新方式可能不同[^2]。 4. **初始化与终止条件** 初始化时,起点的状态被设置为一个初始值(例如 0 或无穷大),其余节点的状态设置为无效或不可达。当终点的状态被取出时,可以提前终止算法,以减少不必要的计算[^3]。 5. **数据结构优化** 使用优先队列(如 `priority_queue`)来维护待处理的状态,确保每次取出的是当前最优的状态。此外,可以使用哈希表或数组来记录每个节点当前的最优值,避免重复处理[^4]。 6. **特殊情况处理** 在某些情况下,可能存在多个相同的边或自环边,需要确保这些情况不会导致死循环或错误的状态更新。可以通过判断新状态是否优于已有状态来决定是否将其加入队列[^5]。 ### 示例代码 以下是一个基于 Dijkstra 变体的伪代码实现: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; vector<pair<int, int>> adj[MAXN]; // 邻接表 int dist[MAXN]; // 存储每个节点的最优值 void dijkstra(int start, int n) { priority_queue<pair<int, int>> pq; fill(dist, dist + n + 1, -1); // 初始化为 -1 表示未访问 dist[start] = 0; pq.push({0, start}); while (!pq.empty()) { int u = pq.top().second; int cost = pq.top().first; pq.pop(); if (cost < dist[u]) continue; // 已找到更优解,跳过 for (auto &edge : adj[u]) { int v = edge.first; int w = edge.second; int new_cost = min(cost, w); // 以最小值为目标函数 if (new_cost > dist[v]) { dist[v] = new_cost; pq.push({new_cost, v}); } } } } ``` ### 相关问题 1. 如何在 Codeforces Contest 1215 Problem D 中建模图结构? 2. Dijkstra 算法的哪些变体适用于最大化路径上的最小值? 3. 在 Codeforces Contest 1215 Problem D 中,如何处理多个相同边的情况? 4. 为什么在某些情况下需要提前终止 Dijkstra 算法? 5. 如何验证 Codeforces Contest 1215 Problem D 的解是否正确?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值