P1781 宇宙总统
https://www.luogu.com.cn/problem/P1781
字符串比较
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception{
int n = Integer.parseInt(in.readLine());
int idx = -1;
String ans = "0";
for (int i = 1; i <= n; i++) {
String ss = in.readLine();
int len = ss.length(), m = ans.length();
if (len > m) {
idx = i;
ans = ss;
} else if (len < m) {
continue;
} else if (ss.compareTo(ans) > 0){
idx = i;
ans = ss;
}
}
out.println(idx);
out.println(ans);
out.flush();
in.close();
}
}
P1223 排队接水
https://www.luogu.com.cn/problem/P1223
贪心
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception{
int n = Integer.parseInt(in.readLine().trim());
int[][] arr = new int[n][2];
String[] ss = in.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i][0] = i + 1;
arr[i][1] = Integer.parseInt(ss[i]);
}
Arrays.parallelSort(arr, (a, b) -> (a[1] - b[1]));
double tot = 0, cur = 0;
for (int i = 0; i < n; i++) {
out.print(arr[i][0] + " ");
tot += cur;
cur += arr[i][1];
}
out.printf("\n%.2f", tot / n);
out.flush();
in.close();
}
}
P3817 小A的糖果
https://www.luogu.com.cn/problem/P3817
贪心
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception{
String[] nx = in.readLine().split(" ");
int n = Integer.parseInt(nx[0]), x = Integer.parseInt(nx[1]);
int[] arr = new int[n];
String[] ss = in.readLine().split(" ");
long ans = 0;
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(ss[i]);
if (i > 0 && arr[i] + arr[i - 1] > x) {
ans += arr[i] - x + arr[i - 1];
arr[i] = x - arr[i - 1];
}
}
out.println(ans);
out.flush();
in.close();
}
}
P1007 独木桥
https://www.luogu.com.cn/problem/P1007
贪心
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception{
int l = Integer.parseInt(in.readLine()), n = Integer.parseInt(in.readLine());
int mn = 0, mx = 0;
String[] ss = null;
if (n > 0) ss = in.readLine().split(" ");
for (int i = 0; i < n; i++) {
int t = Integer.parseInt(ss[i]);
mn = Math.max(mn, Math.min(t, l + 1 - t));
mx = Math.max(mx, Math.max(t, l + 1 - t));
}
out.println(mn + " " + mx);
out.flush();
in.close();
}
}