题目
奶牛们又出去锻炼蹄子去了!
有 N
头奶牛在无限长的单行道上慢跑,且跑步方向为坐标值增大的方向。
每头奶牛在跑道上开始奔跑的位置互不相同,一些奶牛的奔跑速度可能相同,也可能不同。
由于跑道是单行道,十分狭窄,奶牛们无法相互超越。
当一头速度很快的牛追上另一头牛时,她必须减速至与另一头牛速度相同以免发生碰撞,并成为同一跑步小组的一员。此时,两头牛可以视为在同一点上。
最终,再也没有奶牛会撞到(追上)其他奶牛了。
约翰想知道在这种情况下,会剩下多少个跑步小组。
代码
import java.util.*;
public class Main {
static int N = 100010;
static int n;
static node[] q = new node[N];
public static void main(String[] args) {
Scanner inScanner = new Scanner(System.in);
n = inScanner.nextInt();
for(int i = 1; i <= n; i ++)
q[i] = new node(inScanner.nextInt(), inScanner.nextInt());
inScanner.close();
Arrays.sort(q, 1, n + 1);
int ans = 1, pre = q[1].y;
for(int i = 2; i <= n; i ++)
{
if(q[i].y <= pre)
{
pre = q[i].y;
ans ++;
}
}
System.out.println(ans);
}
}
class node implements Comparable<node>
{
int x;
int y;
public node(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public int compareTo(node o) {
return o.x - x;
}
}