# 挑战程序竞赛系列（87）：3.6平面扫描（1）

## 挑战程序竞赛系列（87）：3.6平面扫描（1）

```import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class Main{

String INPUT = "./data/judge/201709/P2932.txt";

public static void main(String[] args) throws IOException {
new Main().run();
}

static final int MAX_N = 40000 + 16;

class E implements Comparable<E>{

double x;
int id;

E(double x, int id){
this.x  = x;
this.id = id;
}

@Override
public int compareTo(E o) {
return Double.compare(x, o.x);
}

@Override
public boolean equals(Object obj) {
return compareTo((E)(obj)) == 0;
}
}

class C{

double x;
double y;
double r;

C(double r, double x, double y){
this.x = x;
this.y = y;
this.r = r;
}
}

int N;
C[] cs = new C[MAX_N];
E[] xs = new E[2 * MAX_N];

// j 是否包含于 i
boolean inside(int i, int j) {
double dx = cs[i].x - cs[j].x;
double dy = cs[i].y - cs[j].y;
return dx * dx + dy * dy <= cs[i].r * cs[i].r;
}

void solve() {

for (int i = 0; i < N; ++i) {
C c = cs[i];
xs[2 * i]     = new E(c.x - c.r, i);
xs[2 * i + 1] = new E(c.x + c.r, i + N);
}

Arrays.sort(xs, 0, 2 * N);

TreeSet<E> set = new TreeSet<E>();
List<Integer> ans = new ArrayList<Integer>();
for (int i = 0; i < 2 * N; ++i) {
E e = xs[i];
int id = e.id % N;
E crtC = new E(cs[id].y, id);
if (e.id < N) {
E up = set.ceiling(crtC);
E dn = set.floor(crtC);
if (up != null && inside(up.id, id)) continue;
if (dn != null && inside(dn.id, id)) continue;
}
else {
set.remove(crtC);
}
}

Collections.sort(ans);
out.println(ans.size());
for (int i = 0; i < ans.size(); ++i) {
out.print((ans.get(i) + 1) + (i + 1 == ans.size() ? "\n" : " "));
}
}

N = ni();
for (int i = 0; i < N; ++i) {
cs[i] = new C(nd(), nd(), nd());
}
solve();
}

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();
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 {
StringTokenizer st;
boolean hasNext;

public FastScanner(InputStream is) throws IOException {
hasNext = true;
}

public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
} 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);
}
}
}```

98 篇文章33 人订阅

0 条评论

## 相关文章

### 2018-10-28 String.valueOf(null) 会报空指针异常

https://stackoverflow.com/questions/4042675/why-string-valueofnull-is-causing-nu...

481

553

### go语言json操作指南

1、Go语言的JSON 库 　　Go语言自带的JSON转换库为 encoding/json 1.1）其中把对象转换为JSON的方法（函数）为 json.Mar...

3838

2007

1669

1909

### go语言json操作指南

1、Go语言的JSON 库 　　Go语言自带的JSON转换库为 encoding/json 1.1）其中把对象转换为JSON的方法（函数）为 json.Mar...

34112

### Go语言struct类型详解

struct Go语言中，也和C或者其他语言一样，我们可以声明新的类型，作为其它类型的属性或字段的容器。例如，我们可以创建一个自定义类型person代表一个人的...

2714

### 函数式编程与面向对象编程[1]: Lambda表达式 函数柯里化 高阶函数函数式编程与面向对象编程[1]: Lambda表达式 函数柯里化 高阶函数.md

For example, in Lisp the 'square' function can be expressed as a lambda expressi...

562

1809