1006 Sign In and Sign Out
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int M = scanner.nextInt();
String firstId = null, lastId = null;
String earliestSignIn = "24:00:00", latestSignOut = "00:00:00";
for (int i = 0; i < M; i++) {
String id = scanner.next();
String signInTime = scanner.next();
String signOutTime = scanner.next();
if (signInTime.compareTo(earliestSignIn) < 0) {
earliestSignIn = signInTime;
firstId = id;
}
if (signOutTime.compareTo(latestSignOut) > 0) {
latestSignOut = signOutTime;
lastId = id;
}
}
System.out.println(firstId + " " + lastId);
scanner.close();
}
}
1007 Maximum Subsequence Sum
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int K = scanner.nextInt();
int[] numbers = new int[K];
for (int i = 0; i < K; i++) {
numbers[i] = scanner.nextInt();
}
int maxSum = Integer.MIN_VALUE;
int currentSum = 0;
int start = 0, end = 0, tempStart = 0;
for (int i = 0; i < K; i++) {
currentSum += numbers[i];
if (currentSum > maxSum) {
maxSum = currentSum;
start = tempStart;
end = i;
}
if (currentSum < 0) {
currentSum = 0;
tempStart = i + 1;
}
}
if (maxSum < 0) {
System.out.println(0 + " " + numbers[0] + " " + numbers[K - 1]);
}else{
System.out.println(maxSum + " " + numbers[start] + " " + numbers[end]);
}
}
}
1008 Elevator
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int[] floors = new int[N];
for (int i = 0; i < N; i++) {
floors[i] = scanner.nextInt();
}
int totalTime = 0;
int currentFloor = 0;
for (int i = 0; i < N; i++) {
int targetFloor = floors[i];
if (targetFloor > currentFloor) {
totalTime += (targetFloor - currentFloor) * 6;
} else if (targetFloor < currentFloor) {
totalTime += (currentFloor - targetFloor) * 4;
}
totalTime += 5;
currentFloor = targetFloor;
}
System.out.println(totalTime);
scanner.close();
}
}
1009 Product of Polynomials
import java.util.Scanner;
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int K1 = scanner.nextInt();
Map<Integer, Double> poly1 = new TreeMap<>();
for (int i = 0; i < K1; i++) {
poly1.put(scanner.nextInt(), scanner.nextDouble());
}
int K2 = scanner.nextInt();
Map<Integer, Double> poly2 = new TreeMap<>();
for (int i = 0; i < K2; i++) {
poly2.put(scanner.nextInt(), scanner.nextDouble());
}
Map<Integer, Double> result = new TreeMap<>((a, b) -> b - a);
for (int e1 : poly1.keySet()) {
for (int e2 : poly2.keySet()) {
int exp = e1 + e2;
double coeff = poly1.get(e1) * poly2.get(e2);
result.put(exp, result.getOrDefault(exp, 0.0) + coeff);
}
}
result.entrySet().removeIf(entry -> Math.abs(entry.getValue()) < 1e-9);
StringBuilder output = new StringBuilder();
output.append(result.size());
for (Map.Entry<Integer, Double> entry : result.entrySet()) {
output.append(" ").append(entry.getKey()).append(" ").append(String.format("%.1f", entry.getValue()));
}
System.out.println(output.toString());
scanner.close();
}
}
1010 Radix
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String N1 = scanner.next();
String N2 = scanner.next();
int tag = scanner.nextInt();
int radix = scanner.nextInt();
scanner.close();
if (tag == 2) {
String temp = N1;
N1 = N2;
N2 = temp;
}
long num1 = convertToDecimal(N1, radix);
int minRadix = findMinRadix(N2);
long resultRadix = findRadix(N2, num1, minRadix, Math.max(num1 + 1, minRadix + 1));
if (resultRadix == -1) {
System.out.println("Impossible");
} else {
System.out.println(resultRadix);
}
}
private static long convertToDecimal(String number, int radix) {
long result = 0;
for (char c : number.toCharArray()) {
result = result * radix + (Character.isDigit(c) ? c - '0' : c - 'a' + 10);
if (result < 0) {
return -1; // overflow
}
}
return result;
}
private static int findMinRadix(String number) {
int minRadix = 2;
for (char c : number.toCharArray()) {
int value = Character.isDigit(c) ? c - '0' : c - 'a' + 10;
if (value + 1 > minRadix) {
minRadix = value + 1;
}
}
return minRadix;
}
private static long findRadix(String number, long target, long low, long high) {
while (low <= high) {
long mid = (low + high) / 2;
long converted = convertToDecimal(number, (int) mid);
if (converted == -1 || converted > target) {
high = mid - 1;
} else if (converted < target) {
low = mid + 1;
} else {
return mid;
}
}
return -1;
}
}
注
1007 最大子序列最后一个示例是超时的
代码本身时间复杂度O(n)
在leetcode,相同做法2ms
在攀拓200ms,示例也是不让看的,希望后台输入数据的老师手速再快点^^