java 矩形提取_Java实现分割矩形

给定平面内平行于坐标轴的一个矩形,从矩形内选

择一些点,从这些点向右和向上各射出一条射线,

请问:这些射线将矩形分成了多少份。

数据格式:

输入的第一行包含两个整数x, y,表示矩形是由(0,

0), (x, 0), (x, y), (0, y)四个点围成的。

第二行包含一个整数n,表示矩形内的点的数量。

接下来n行,每个两个整数xi, yi,表示矩形内的一

个点。输入保证所有的点都在矩形内部而且没有两

个点有相同的横坐标或纵坐标。

输出一个整数,表示从给定的点射出的射线将矩形

分成的份数。

例如,输入:

10 10

3

1 3

3 1

2 4

程序应该输出:

6

【数据规模和约定】

对于10%的数据,1<=n<=10,1<=x, y<=100;

对于30%的数据,1<=n<=1000,

1<=x,y<=1000;

对于60%的数据,1<=n<=10000,

1<=x,y<=1000000;

对于100%的数据,1<=n<=100000,

1<=x,y<=1000000000,1资源约定:

峰值内存消耗(含虚拟机) < 256M

CPU消耗 < 1000ms

请严格按要求输出,不要画蛇添足地打印类似:“

请您输入…” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝

提交该源码。

注意:不要使用package语句。不要使用jdk1.7及

以上版本的特性。

注意:主类的名字必须是:Main,否则按无效代码

处理。

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.Comparator;

import java.util.Scanner;

public class Main {

public static long x, y;

public static int n;

public static Point[] P;

public static long count = 1;

public static ArrayListlist = new ArrayList();

class MyComparator implements Comparator{

public int compare(Point arg0, Point arg1) {

if(arg0.x > arg1.x)

return 1;

else if(arg0.x < arg1.x)

return -1;

else if(arg0.x == arg1.x) {

if(arg0.y > arg0.y)

return 1;

else if(arg0.y < arg0.y)

return -1;

else

return 0;

}

return 0;

}

}

static class Point {

public int x;

public int y;

public Point(int x, int y) {

this.x = x;

this.y = y;

}

}

public void getResult() {

Arrays.sort(P, new MyComparator());

list.add(P[n - 1].y);

count++;

for(int i = n - 2;i >= 0;i--) {

if(P[i].x == P[i + 1].x) {

if(P[i + 1].y > P[i].y) {

int j = list.indexOf(P[i + 1].y);

list.remove(j);

list.add(P[i].y);

}

count++;

}

else {

Collections.sort(list);

int j = 0;

for(;j < list.size();j++)

if(list.get(j) >= P[i].y)

break;

count = count + j + 1;

if(!list.contains(P[i].y))

list.add(P[i].y);

}

}

System.out.println(count);

}

public static void main(String[] args) {

Main test = new Main();

Scanner in = new Scanner(System.in);

x = in.nextLong();

y = in.nextLong();

n = in.nextInt();

P = new Point[n];

for(int i = 0;i < n;i++) {

int x = in.nextInt();

int y = in.nextInt();

P[i] = new Point(x, y);

}

test.getResult();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值