publicstaticvoidsolve() { intm= io.nextInt(); int[] d = newint[m]; inttot=0; for (inti=0; i < m; i++) { d[i] = io.nextInt(); tot += d[i]; } intmid= (tot + 1) / 2; for (inti=0; i < m; i++) { if (mid <= d[i]) { io.println(i + 1 + " " + mid); return; } mid -= d[i]; } }
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
voidsolve(){ int m; cin >> m; int tot = 0; vector<int> d(m); for (int i = 0; i < m; i++) { cin >> d[i]; tot += d[i]; } int mid = (tot + 1) / 2; for (int i = 0; i < m; i++) { if (mid <= d[i]) { cout << i + 1 << " " << mid << "\n"; return; } mid -= d[i]; } }
publicstaticvoidsolve() { intm= io.nextInt(), n = io.nextInt(); String[] arr = newString[m]; for (inti=0; i < m; i++) { arr[i] = io.next(); } int[][] row = newint[m][26]; int[][] col = newint[n][26]; for (inti=0; i < m; i++) { for (intj=0; j < n; j++) { row[i][arr[i].charAt(j) - 'a']++; col[j][arr[i].charAt(j) - 'a']++; } } intr= m, c = n; boolean[] vr = newboolean[m]; boolean[] vc = newboolean[n]; for (intk=0; k < m + n; k++) { List<int[]> mr = newArrayList<>(); List<int[]> mc = newArrayList<>(); for (inti=0; i < m; i++) { if (vr[i]) continue; for (intj=0; j < 26; j++) { if (row[i][j] == c && c >= 2) { mr.add(newint[]{i, j}); } } } for (inti=0; i < n; i++) { if (vc[i]) continue; for (intj=0; j < 26; j++) { if (col[i][j] == r && r >= 2) { mc.add(newint[]{i, j}); } } } for (int[] p : mr) { r--; vr[p[0]] = true; for (inti=0; i < n; i++) { col[i][p[1]]--; } } for (int[] p : mc) { c--; vc[p[0]] = true; for (inti=0; i < m; i++) { row[i][p[1]]--; } } } io.println(r * c); }
publicstaticvoidsolve() { intn= io.nextInt(), x = io.nextInt(), max = 0; for (inti=1; i < n; i++) { max = Math.max(max, io.nextInt()); } io.println(Math.max(max - x + 1, 0)); }
publicstaticvoidsolve() { intn= io.nextInt(); longsum=0L; int[] arr = newint[n]; for (inti=0; i < n; i++) { arr[i] = io.nextInt(); sum += arr[i]; } // 可以替换为快速选择 Arrays.sort(arr); longans=0L, p = sum / n, r = sum % n; for (inti=0; i < n; i++) { ans += Math.abs(arr[i] - (p + (i >= n - r ? 1 : 0))); } io.println(ans / 2); }