Atcoder 091/092 C 2D Plane 2N Points[扫描线]

昨晚打了第一场atcoder...这题卡了1h...今天听课的时候听到了一个极相似的题。。。

题意:给出2n个点的坐标(x,y) 前n个是红点,剩下是蓝点,当一个红点的横纵坐标都小于一个蓝点的时候,他们可以匹配,求最大的匹配对数

按照横坐标排序,排序后从右往左扫描,发现蓝点将其纵坐标存入set中(因为已经按照横坐标排序,所以不需要考虑横坐标),发现红点从set中找一个能跟这个点匹配的最小的点(lower_bound),注意set::lower_bound是O(logn)的,std::lower_bound在set中是O(n)的,因为set不支持随机访问

还能用二分图匹配,但是数据范围大了似乎只能用这种方法

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int, int> pii;
 4 set<int>s;
 5 struct Node {
 6   int x, y, color;
 7   inline bool operator < (const Node & rhs) const {return x < rhs.x;}
 8 }Node[300];
 9 int n, ans;
10 int main(void){
11   scanf("%d", &n);
12   for(int i = 1; i <= n; ++i) scanf("%d%d", &Node[i].x, &Node[i].y), Node[i].color = 1;
13   for(int i = 1; i <= n; ++i) scanf("%d%d", &Node[i+n].x, &Node[i+n].y), Node[i+n].color = 2;
14   sort(Node+1, Node+1+n*2);
15   for(int i = 2*n; i >= 1; --i) {
16     if (Node[i].color == 2) s.insert(Node[i].y);
17     else  {
18       auto it = s.lower_bound(Node[i].y);
19       if (it != s.end()) s.erase(it), ans++;
20     }
21   }
22   cout << ans;
23   return 0;
24 }

原文地址:https://www.cnblogs.com/Ycrpro/p/8595348.html

时间: 2024-10-11 21:52:27

Atcoder 091/092 C 2D Plane 2N Points[扫描线]的相关文章

AtCoder Regular Contest 092 2D Plane 2N Points AtCoder - 3942 (匈牙利算法)

Problem Statement On a two-dimensional plane, there are N red points and N blue points. The coordinates of the i-th red point are (ai,bi), and the coordinates of the i-th blue point are (ci,di). A red point and a blue point can form a friendly pair w

arc 092C 2D Plane 2N Points

题意: 有n个红色的点和n个蓝色的点,如果红色的点的横坐标和纵坐标分别比蓝色的点的横坐标和纵坐标小,那么这两个点就可以成为一对友好的点. 问最多可以形成多少对友好的点. 思路: 裸的二分图匹配,对于满足条件的两个点连边. wa了两发,板子错了,还是得用果苣的!. 代码: 1 #include <stdio.h> 2 #include <string.h> 3 4 const int N = 105; 5 6 int link[N]; 7 bool mp[N][N]; 8 bool

[LeetCode OJ] Max Points on a Line—Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

//定义二维平面上的点struct Point { int x; int y; Point(int a=0, int b=0):x(a),y(b){} }; bool operator==(const Point& left, const Point& right) { return (left.x==right.x && left.y==right.y); } //求两个点连接成的直线所对应的斜率 double line_equation(const Point&

Microsoft - Find the K closest points to the origin in a 2D plane

Find the K closest points to the origin in a 2D plane, given an array containing N points. 用 max heap 做 /* public class Point { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } } */ public List<Point> findKClosest(P

Find the total area covered by two rectilinear rectangles in a 2D plane. 208MM

Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area is never beyond the maximum possible value of int. pu

AtCoder Grand Contest 025 Problem D - Choosing Points

题目大意:输入$n,d1,d2$,你要找到$n^2$个整点 x, y 满足$0 \leqslant x, y<2n$.并且找到的任意两个点距离,既不是$\sqrt{d1}$,也不是 $\sqrt{d2}$. 题解:如果$d mod 2=1$,如果$a^2+b^2=d$,a和b一定一奇一偶,按国际象棋黑白染色即可.如果$d mod 4=2$,如果$a^2+b^2=d$,a和b一定都是奇数,一行黑色,一行白色即可.如果$d mod 4=0$,把$2×2$的区域看成一个大格子,对$d/4$进行如上考虑

149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ class Solutio

LeetCode-Max Points On a Line题解

一. 题意描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 二. 题目分析 看到这道题想到的第一种方法是暴力枚举法,时间复杂度为O(n3),显然是不够好的. 于是我们需要进行一些优化,注意到对于一条直线上的任意两点,他们连线的斜率是相同的.于是我们可以算出指定点到其他所有点的斜率,先使得包含指定点的直线上点数最多,这个过程中可以建立斜率和直线

149. Max Points on a Line (Array; Greedy)

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 思路:对于某一点来说,在经过该点的直线中选取节点数量最多的直线:对于全局来说,必定是某个局部点满足条件的直线之一=>局部最优解也是全局最优解=>贪心法. struct Point { int x; int y; Point() : x(0), y(0) {} Point(int a, int b)