挑战程序竞赛系列(62):4.6平面上的分治法(2)
来源:互联网 发布:teradata待遇知乎 编辑:程序博客网 时间:2024/06/06 12:20
挑战程序竞赛系列(62):4.6平面上的分治法(2)
传送门:Codeforces 97B: Superset
题意:(参考hankcs)
http://www.hankcs.com/program/algorithm/codeforces-97b-superset.html
点集:给定n个点,请添加一些点,使任意两点满足①在同一条水平线或竖直线上②或构成一个矩形框住其他点。
直接看hankcs的解释吧,通俗易懂。
指出一点:新的点一定由这些坐标的横纵坐标生成,所以求出投影即能满足条件①,条件②在求解①的过程中自然满足。
注意Java中Set的使用方法,需要重写hashCode 和 equals方法,因为hashCode可能相同,所以判断两个对象是否相等还需要加入eqauls方法。
代码如下:
import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.HashSet;import java.util.List;import java.util.Set;import java.util.StringTokenizer;public class Main{ String INPUT = "./data/judge/201709/C097B.txt"; public static void main(String[] args) throws IOException { new Main().run(); } static final int MAX_N = 10000 + 16; class Pair implements Comparable<Pair>{ int x; int y; Pair(int x, int y){ this.x = x; this.y = y; } @Override public String toString() { return x + " " + y; } @Override public int compareTo(Pair o) { return this.x - o.x; } @Override public int hashCode() { return x * 17 + y * 13; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Pair p = (Pair) obj; return x == p.x && y == p.y; } } Set<Pair> ans = new HashSet<>(); void solve(Pair[] p, int l, int r) { if (r - l <= 1) return; int m = (r + l) / 2; int x = p[m].x; for (int i = l; i < r; ++i) { ans.add(new Pair(x, p[i].y)); } solve(p, l, m); solve(p, m, r); } void read() { int N = ni(); Pair[] p = new Pair[N]; for (int i = 0; i < N; ++i) { p[i] = new Pair(ni(), ni()); } Arrays.sort(p); for (Pair ps : p) ans.add(ps); solve(p, 0, N); out.println(ans.size()); for (Pair pp : ans) { out.println(pp); } } FastScanner in; PrintWriter out; void run() throws IOException { boolean oj; try { oj = ! System.getProperty("user.dir").equals("F:\\java_workspace\\leetcode"); } catch (Exception e) { oj = System.getProperty("ONLINE_JUDGE") != null; } InputStream is = oj ? System.in : new FileInputStream(new File(INPUT)); in = new FastScanner(is); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); read(); out.flush(); if (!oj){ System.out.println("[" + (System.currentTimeMillis() - s) + "ms]"); } } public boolean more(){ return in.hasNext(); } public int ni(){ return in.nextInt(); } public long nl(){ return in.nextLong(); } public double nd(){ return in.nextDouble(); } public String ns(){ return in.nextString(); } public char nc(){ return in.nextChar(); } class FastScanner { BufferedReader br; StringTokenizer st; boolean hasNext; public FastScanner(InputStream is) throws IOException { br = new BufferedReader(new InputStreamReader(is)); hasNext = true; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { hasNext = false; return "##"; } } return st.nextToken(); } String next = null; public boolean hasNext(){ next = nextToken(); return hasNext; } public int nextInt() { if (next == null){ hasNext(); } String more = next; next = null; return Integer.parseInt(more); } public long nextLong() { if (next == null){ hasNext(); } String more = next; next = null; return Long.parseLong(more); } public double nextDouble() { if (next == null){ hasNext(); } String more = next; next = null; return Double.parseDouble(more); } public String nextString(){ if (next == null){ hasNext(); } String more = next; next = null; return more; } public char nextChar(){ if (next == null){ hasNext(); } String more = next; next = null; return more.charAt(0); } } static class ArrayUtils { public static void fill(int[][] f, int value) { for (int i = 0; i < f.length; ++i) { Arrays.fill(f[i], value); } } public static void fill(int[][][] f, int value) { for (int i = 0; i < f.length; ++i) { fill(f[i], value); } } public static void fill(int[][][][] f, int value) { for (int i = 0; i < f.length; ++i) { fill(f[i], value); } } }}
阅读全文
0 0
- 挑战程序竞赛系列(62):4.6平面上的分治法(2)
- 挑战程序竞赛系列(61):4.6平面上的分治法(1)
- 挑战程序竞赛系列(57):4.6数列上的分治法
- 挑战程序竞赛系列(59):4.6树上的分治法(2)
- 挑战程序竞赛系列(58):4.6树上的分治法(1)
- 挑战程序竞赛系列(60):4.6树上的分治法(3)
- 挑战程序竞赛系列(88):3.6平面扫描(2)
- 挑战程序竞赛系列(64):4.7字符串上的动态规划(2)
- 挑战程序竞赛系列(87):3.6平面扫描(1)
- 挑战程序竞赛系列(89):3.6平面扫描(3)
- 挑战程序竞赛系列(63):4.7字符串上的动态规划(1)
- 挑战程序竞赛系列(65):4.7字符串上的动态规划(3)
- 挑战程序竞赛系列(30):3.4矩阵的幂
- 挑战程序竞赛系列(82):4.3 LCA(2)
- 挑战程序竞赛系列(39):4.1模运算的世界(2)
- 挑战程序竞赛系列(81):4.3 LCA(1)
- 挑战程序竞赛系列(1):2.3动态规划
- 挑战程序竞赛系列(4):2.1深度优先搜索
- 英语的时间表达法
- log4j2配置
- 对于psr4自动加载规范和命名空间规范
- 转载文章积累
- PHP 把源文件加密到 BMP 位图中
- 挑战程序竞赛系列(62):4.6平面上的分治法(2)
- NOI2002-银河英雄传说
- HTTP——了解Web及网络基础
- mac上截图快捷键
- 词向量源码解析:(4.4)hyperwords源码解析之word2vecf
- 心得体会
- PHP通过反射实现自动注入参数
- DFT,STFT,MFCC总结
- 常见排序算法以及时间复杂度和空间复杂度