1.排序不等式
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = 100010;
long res = 0;
int[] t = new int[N];
int n = sc.nextInt();
for(int i = 0; i < n; i ++){
t[i] = sc.nextInt();
}
Arrays.sort(t, 0, n);//排序
for(int i = 0; i < n; i ++){
res += t[i] * (n - 1 - i);//公式
}
System.out.print(res);
}
}
2.绝对值不等式
绝对值不等式
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int N = 100010;
int[] a = new int[N];
for(int i = 0; i < n; i ++){
a[i] = sc.nextInt();
}
int res = 0;
Arrays.sort(a, 0, n);
for(int i = 0; i < n; i ++){
res += Math.abs(a[i] - a[n / 2]);//找出中位点,记得要转换成绝对值
}
System.out.print(res);
}
}
3.推公式
按照wi+si的顺序从小到大排,最大危险系数一定是最小的。
import java.util.*;
class PII implements Comparable<PII>{//结构体
int x, y;
public PII(int x, int y){
this.x = x;
this.y = y;
}
public int compareTo(PII o){
return Integer.compare(x, o.x);
}
}
public class Main{
static int w, s;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = 50010;
PII[] c = new PII[N];//用来存w+s
int n = sc.nextInt();
for(int i = 0; i < n; i ++){
w = sc.nextInt();
s = sc.nextInt();
c[i] = new PII(w + s, w);
}
int res = -0x3f3f3f3f;
Arrays.sort(c, 0, n);
int sum = 0;
for(int i = 0; i < n; i ++){
w = c[i].y;
s = c[i].x - w;
res = Math.max(res, sum - s);
sum += w;//记得加上前面牛牛的体重
}
System.out.print(res);
}
}