栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

区间选点问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

区间选点问题

代码

package LQB14;

import java.util.Arrays;
import java.util.Scanner;


public class T4 {
	 //区间选点问题
    private static class point implements Comparable{
        private int st;//起点
        private int et;//终点
        public point(int a,int b)//构造器
        {
            this.st=a;
            this.et=b;
        }
         //若要利用sort进行排序就要重写compareto函数
        public int compareTo(point o) {
            int z=this.et-o.et;
             if (z==0)
            {
                int y=this.st-o.st;
                if (y==0)
                    return 0;
                else if (y<0)
                    return -1;
                else
                    return 1;
            }
            else if (z>0)
                return 1;
            else
                return -1;
        }
        
        public String toString() //重写转字符串
        {
            return this.st+" "+this.et;
        }
    }
    public static boolean check(int []a,int i)//判断在数组的i处的位置是否被标记
    {
       if (a[i]==1)
           return true;
       else
           return false;
    }
    public static int solve(point []pp,int [] b)
    {
        int cnt=1;
        int enp=pp[0].et;//记录第一个的结束
        int stp=pp[0].st;//记录第一个开始
         b[enp]=1;//将第一个点标记出来
        for (int i=1;i<=pp.length-1;i++)//循环剩余情况
        {
            if (pp[i].st<=stp)//包含情况
            {
                enp=pp[i].et;
                stp=pp[i].st;
            }
            else if (pp[i].st<=enp)//相交情况
            {
                if (check(b,pp[i].st))//若在开端已经有点,则直接转移
                {
                    stp=pp[i].st;
                    enp=pp[i].et;
                }
                else //反之,加点转移
                {
                    cnt++;
                    b[pp[i].st]=1;
                    stp=pp[i].st;
                    enp=pp[i].et;
                }
            }
            else //既不相交,又不包含的情况
            {
                cnt++;
                b[pp[i].et]=1;
                stp=pp[i].st;
                enp=pp[i].et;
            }
        }
        return cnt;

    }
    public static void main(String[] args) {
        Scanner a=new Scanner(System.in);
        int n=a.nextInt();//记录输入段的个数
        point [] pp=new point[n];//建立对象数组
        for (int i=0;i
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/310173.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号