publicclassMain{ staticclassRange{ publicint l, r; publicRange(int l, int r){ this.l = l; this.r = r; } } staticint N = 100010; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static Range[] range = new Range[N];
publicstaticvoidmain(String[] args)throws IOException{ int n = Integer.parseInt(br.readLine()); for(int i = 0; i < n; i++) { String[] str = br.readLine().split(" "); int l = Integer.parseInt(str[0]); int r = Integer.parseInt(str[1]); range[i] = new Range(l ,r); } Arrays.sort(range, 0, n, (o1, o2) -> Integer.compare(o1.r, o2.r)); int res = 0; int ed = (int)-2e9; for(int i = 0; i < n; i++) { if(range[i].l > ed) { ed = range[i].r; res++; } } System.out.println(res); } }
最大不相交区间数量
给定 N 个闭区间 [ai,bi],请你在数轴上选择若干区间,使得选中的区间之间互不相交(包括端点)。 输出可选取区间的最大数量。
代码与思路同上~
区间分组
给定 N 个闭区间 [ai,bi],请你将这些区间分成若干组,使得每组内部的区间两两之间(包括端点)没有交集,并使得组数尽可能小。
publicclassMain{ staticclassRange{ publicint l, r; publicRange(int l, int r){ this.l = l; this.r = r; } } staticint N = 100010; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static Range[] range = new Range[N];
publicstaticvoidmain(String[] args)throws IOException{ String[] str = br.readLine().split(" "); int s = Integer.parseInt(str[0]); int t = Integer.parseInt(str[1]); int n = Integer.parseInt(br.readLine()); for(int i = 0; i < n; i++) { str = br.readLine().split(" "); int a = Integer.parseInt(str[0]); int b = Integer.parseInt(str[1]); range[i] = new Range(a, b); } Arrays.sort(range, 0, n, (o1, o2) -> o1.l - o2.l); int res = 0; //双指针 int i = 0; boolean flag = false;//判断长度是否达到 while(i < n){ int r = (int)-2e9; int j = i; while(j < n && range[j].l <= s) { r = Math.max(r, range[j].r); j++; } res++; if(r < s)break; if(r >= t) {//必须有这个条件成立才满足题设 flag = true; break; } s = r; i = j; } if(flag)System.out.println(res); else System.out.println("-1"); } }
publicclassMain{ static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
publicstaticvoidmain(String[] args)throws IOException { int n = Integer.parseInt(br.readLine().trim()); Queue<Integer> q = new PriorityQueue<>(); String[] str = br.readLine().split(" "); for(int i = 0; i < n; i++){ int a = Integer.parseInt(str[i]); q.add(a); } int res = 0; while(q.size() > 1){ int a = q.peek(); q.poll(); int b = q.peek(); q.poll(); res += a + b; q.add(a+b); } System.out.println(res); } }
排序不等式
逆序乘 <= 乱序乘 <= 顺序乘
有 n 个人排队到 1 个水龙头处打水,第 i 个人装满水桶所需的时间是 ti,请问如何安排他们的打水顺序才能使所有人的等待时间之和最小?
publicclassMain{ static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); staticint N = 100010; staticint[] a = newint[N]; publicstaticvoidmain(String[] args)throws IOException { int n = Integer.parseInt(br.readLine()); String[] str = br.readLine().split(" "); for(int i = 0; i < n; i++){ a[i] = Integer.parseInt(str[i]); } Arrays.sort(a, 0, n); long res = 0; for(int i = 0; i < n / 2; i++){ res += a[n - i - 1] - a[i]; } System.out.println(res); } }
推公式
N 头奶牛叠罗汉,其中的每一头都有着自己的重量 Wi 以及自己的强壮程度 Si。 一头牛支撑不住的可能性取决于它头上所有牛的总重量(不包括它自己)减去它的身体强壮程度的值,现在称该数值为风险值,风险值越大,这只牛撑不住的可能性越高。 您的任务是确定奶牛的排序,使得所有奶牛的风险值中的最大值尽可能的小。
publicclassMain{ staticclassPair{ publicint w, s; publicPair(int w, int s){ this.w = w; this.s = s; } } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); staticint N = 50010; static Pair[] p = new Pair[N]; publicstaticvoidmain(String[] args)throws IOException { int n = Integer.parseInt(br.readLine()); for(int i = 0; i < n; i++){ String[] str = br.readLine().split(" "); int w = Integer.parseInt(str[0]); int s = Integer.parseInt(str[1]); p[i] = new Pair(w, s); } Arrays.sort(p, 0, n, (o1,o2) -> (o1.w + o1.s - o2.w - o2.s)); int sum = 0; int res = (int)-2e9; for(int i = 0; i < n; i++){ res = Math.max(res, sum - p[i].s); sum += p[i].w; } System.out.println(res); } }