第一题
package ccf201401; import java.util.Scanner; public class p1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] s = new int[n]; int count = 0; for (int i = 0; i < n; i++) { s[i] = sc.nextInt(); for (int j = 0; j < i; j++) { if (s[j] == (s[i] * (-1))) { count++; } } } System.out.println(count); } }
第二题
package ccf201401; import java.util.Scanner; public class p2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[][] xy = new int[n + 1][5]; for (int i = 1; i < n + 1; i++) { for (int j = 0; j < 4; j++) { xy[i][j] = sc.nextInt(); } xy[i][4] = i; } int[][] pq = new int[m + 1][2]; for (int i = 1; i < m + 1; i++) { for (int j = 0; j < 2; j++) { pq[i][j] = sc.nextInt(); } } Select(n, m, xy, pq); } public static void Swap(int n, int[][] xy, int k) { int max = xy[1][4]; System.out.println(k); for (int i = 1; i < n + 1; i++) { if (xy[i][4] > max) { max = xy[i][4]; } } xy[k][4] = max + 1; } public static void Select(int n, int m, int[][] xy, int[][] pq) { for (int i = 1; i < m + 1; i++) { int res = 0; for (int k = 1; k < n + 1; k++) { if (pq[i][0] >= xy[k][0] && pq[i][0] <= xy[k][2] && pq[i][1] >= xy[k][1] && pq[i][1] <= xy[k][3]) { if (xy[res][4] < xy[k][4]) { res = k; } } } if (res > 0) { Swap(n, xy, res); } else { System.out.println("IGNORED"); } } } }