package pdd; //第一题双优先队列,头部比较 import java.util.Arrays; import java.util.PriorityQueue; import java.util.Scanner; public class Main1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); for (int j = 0; j < T; j++) { int n = scanner.nextInt(); int m = scanner.nextInt(); int k = scanner.nextInt(); PriorityQueue<Integer> queue1 = new PriorityQueue<>(); PriorityQueue<Integer> queue2 = new PriorityQueue<>(); for (int i = 0; i < n; i++) { queue1.add(scanner.nextInt()); } for (int i = 0; i < m; i++) { queue2.add(scanner.nextInt()); } int ans = 0; while (!queue1.isEmpty() && !queue2.isEmpty()){ if(Math.abs(queue1.peek() - queue2.peek()) <= k){ queue1.remove(); queue2.remove(); ans++; }else if(queue1.peek() < queue2.peek()){ queue1.remove(); }else { queue2.remove(); } } System.out.println(ans); } } }
package pdd; //转化为最小形态比较 import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = Integer.parseInt(scanner.nextLine()); String[] strs = new String[N]; for (int i = 0; i < N; i++) { strs[i] = scanner.nextLine(); } Map<String,String> map = new HashMap<>(); Map<String,Integer> indexMap = new HashMap<>(); for (int i = 0; i < N; i++) { map.put(strs[i],toMinString(strs[i])); indexMap.put(strs[i],i); } Arrays.sort(strs,(o1, o2) -> { String min1 = map.get(o1); String min2 = map.get(o2); if(min1.equals(min2)){ return indexMap.get(o1) - indexMap.get(o2); }else { return min1.compareTo(min2); } }); for (String str : strs) { System.out.println(str); } } private static String toMinString(String s){ char[] chars = s.toCharArray(); int m = (s.length()-1)/2; for (int i = 0; i <= m; i++) { if(chars[i] > chars[s.length()-1-i]){ char temp = chars[i]; chars[i] = chars[s.length()-1-i]; chars[s.length()-1-i] = temp; } } return String.valueOf(chars); } }
package pdd; //先找组成最大的正方形,再依次往右边界,下边界添加 import java.util.Scanner; public class Main3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); for (int i = 0; i < T; i++) { long n = scanner.nextLong(); long L = search(n); long remain = n - 2*L*(L+1); if(remain <= 2L){ System.out.println(L*L); }else if(remain <= 2*L +1){ System.out.println(L*L + (remain-1)/2); }else { System.out.println(L*L + L+(remain-2*L-2)/2); } } } private static long search(long n) { long l = 0,r= (long) Math.sqrt(n/2); while (l<=r){ long m = l + (r-l)/2; if( n >= 2*m*(m+1)){ l = m+1; }else { r = m -1; } } return r; } }
全部评论
(1) 回帖