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<xi<x,1<yi<y。

资源约定:
峰值内存消耗(含虚拟机) < 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 ArrayList<Integer> list = new ArrayList<Integer>();
     
     class MyComparator implements Comparator<Point> {

        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();
     }
}
posted @ 2019-07-20 18:17  南墙1  阅读(103)  评论(0编辑  收藏  举报