import java.util.Arrays;
import java.util.Scanner;
class node implements Comparable
{
public int start;
public int end;
public node(int x,int y)
{
this.start=x;
this.end=y;
}
public int compareTo(Object obj)
{
if(obj instanceof node)
{
node bNode=(node)obj;
if(this.end>bNode.end)
return 1;
else
{
return -1;
}
}
return 0;
}
}
public class Main {
/**
* @param args
*/
static node nodes[];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
int time=scanner.nextInt();
while(time--!=0)
{
int number=scanner.nextInt();
nodes=new node[number];
for(int i=0;i
{
int start,end;
start=scanner.nextInt();
end=scanner.nextInt();
nodes[i]=new node(start, end);
}
Arrays.sort(nodes);
int count=1;
for(int j=1;j
{
if(nodes[0].end
{
count++;
nodes[0].end=nodes[j].end;
}
}
System.out.println(count);
}
}
}
优先队列:
import java.util.PriorityQueue;
import java.util.Scanner;
class activity implements Comparable{
int stime,etime;
public activity(int stime, int etime) {
this.stime = stime;
this.etime = etime;
}
public int compareTo(activity atv) {
if(atv instanceof activity)
{
if(this.etime>atv.etime)
return 1;
else return -1;
}
return -1;
}
}
public class Main {
static activity [] atvs;
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int times=scan.nextInt();
PriorityQueue pQueue=new PriorityQueue();
while(times--!=0)
{
int activities=scan.nextInt();
atvs=new activity[activities];
for(int i=0;i
{
atvs[i]=new activity(scan.nextInt(), scan.nextInt());
pQueue.add(atvs[i]);
}
int number=1;
int top=pQueue.poll().etime;
for(int i=1;i
{
if(top
{
top=pQueue.peek().etime;
number++;
}
pQueue.poll();
}
pQueue.clear();
System.out.println(number);
}
}
}