update
This commit is contained in:
52
1/A/Main.java
Normal file
52
1/A/Main.java
Normal file
@@ -0,0 +1,52 @@
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
StringTokenizer st = new StringTokenizer(br.readLine());
|
||||
int n = Integer.parseInt(st.nextToken());
|
||||
int k = Integer.parseInt(st.nextToken());
|
||||
|
||||
Map<Character, TreeMap<Integer, TreeSet<String>>> index = new HashMap<>();
|
||||
Map<String, Integer> counts = new HashMap<>();
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
String word = br.readLine().trim();
|
||||
char c = word.charAt(0);
|
||||
counts.put(word, 0);
|
||||
index
|
||||
.computeIfAbsent(c, x -> new TreeMap<>())
|
||||
.computeIfAbsent(0, x -> new TreeSet<>())
|
||||
.add(word);
|
||||
}
|
||||
|
||||
for (int i = 0; i < k; i++) {
|
||||
String letter = br.readLine().trim();
|
||||
char c = letter.charAt(0);
|
||||
|
||||
TreeMap<Integer, TreeSet<String>> byCount = index.get(c);
|
||||
|
||||
Map.Entry<Integer, TreeSet<String>> minEntry = byCount.firstEntry();
|
||||
int minCount = minEntry.getKey();
|
||||
TreeSet<String> minWords = minEntry.getValue();
|
||||
String bestWord = minWords.first();
|
||||
|
||||
sb.append(bestWord).append('\n');
|
||||
|
||||
minWords.remove(bestWord);
|
||||
if (minWords.isEmpty()) {
|
||||
byCount.remove(minCount);
|
||||
}
|
||||
int newCount = minCount + 1;
|
||||
byCount
|
||||
.computeIfAbsent(newCount, x -> new TreeSet<>())
|
||||
.add(bestWord);
|
||||
counts.put(bestWord, newCount);
|
||||
}
|
||||
|
||||
System.out.print(sb);
|
||||
}
|
||||
}
|
||||
11
1/A/test1.in
Normal file
11
1/A/test1.in
Normal file
@@ -0,0 +1,11 @@
|
||||
4 5
|
||||
peterburg
|
||||
murmansk
|
||||
perm
|
||||
moscow
|
||||
p
|
||||
m
|
||||
m
|
||||
p
|
||||
p
|
||||
|
||||
10
1/A/test2.in
Normal file
10
1/A/test2.in
Normal file
@@ -0,0 +1,10 @@
|
||||
5 3
|
||||
int
|
||||
main
|
||||
void
|
||||
double
|
||||
string
|
||||
v
|
||||
m
|
||||
v
|
||||
|
||||
6
1/A/test3.in
Normal file
6
1/A/test3.in
Normal file
@@ -0,0 +1,6 @@
|
||||
1 3
|
||||
python
|
||||
p
|
||||
p
|
||||
p
|
||||
|
||||
72
1/B/Main.java
Normal file
72
1/B/Main.java
Normal file
@@ -0,0 +1,72 @@
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class Main {
|
||||
static Map<String, String> parent = new HashMap<>();
|
||||
|
||||
static String find(String i) {
|
||||
if (!parent.containsKey(i)) {
|
||||
parent.put(i, i);
|
||||
return i;
|
||||
}
|
||||
if (parent.get(i).equals(i))
|
||||
return i;
|
||||
String root = find(parent.get(i));
|
||||
parent.put(i, root);
|
||||
return root;
|
||||
}
|
||||
|
||||
static boolean union(String a, String b) {
|
||||
String rootA = find(a);
|
||||
String rootB = find(b);
|
||||
|
||||
if (rootA.equals(rootB))
|
||||
return true;
|
||||
|
||||
boolean isNumA = isNumber(rootA);
|
||||
boolean isNumB = isNumber(rootB);
|
||||
|
||||
if (isNumA && isNumB) {
|
||||
return rootA.equals(rootB);
|
||||
}
|
||||
|
||||
if (isNumA) {
|
||||
parent.put(rootB, rootA);
|
||||
} else {
|
||||
parent.put(rootA, rootB);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
if (!sc.hasNextInt())
|
||||
return;
|
||||
int n = sc.nextInt();
|
||||
|
||||
String[] seq1 = new String[n];
|
||||
String[] seq2 = new String[n];
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
seq1[i] = sc.next();
|
||||
for (int i = 0; i < n; i++)
|
||||
seq2[i] = sc.next();
|
||||
|
||||
boolean possible = true;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (!union(seq1[i], seq2[i])) {
|
||||
possible = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(possible ? "YES" : "NO");
|
||||
}
|
||||
|
||||
static boolean isNumber(String s) {
|
||||
if (s == null || s.isEmpty())
|
||||
return false;
|
||||
char first = s.charAt(0);
|
||||
return (first >= '0' && first <= '9') || (first == '-' && s.length() > 1);
|
||||
}
|
||||
}
|
||||
3
1/B/test1.in
Normal file
3
1/B/test1.in
Normal file
@@ -0,0 +1,3 @@
|
||||
3
|
||||
3 1 2
|
||||
3 1 3
|
||||
3
1/B/test2.in
Normal file
3
1/B/test2.in
Normal file
@@ -0,0 +1,3 @@
|
||||
4
|
||||
4 5 igrek igrek
|
||||
4 iks 3 iks
|
||||
3
1/B/test3.in
Normal file
3
1/B/test3.in
Normal file
@@ -0,0 +1,3 @@
|
||||
5
|
||||
x 3 x y 3
|
||||
x y 2 z 3
|
||||
54
1/C/Main.java
Normal file
54
1/C/Main.java
Normal file
@@ -0,0 +1,54 @@
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
int n = Integer.parseInt(br.readLine().trim());
|
||||
|
||||
long[][] pts = new long[n][2];
|
||||
for (int i = 0; i < n; i++) {
|
||||
StringTokenizer st = new StringTokenizer(br.readLine());
|
||||
pts[i][0] = Math.abs(Long.parseLong(st.nextToken()));
|
||||
pts[i][1] = Math.abs(Long.parseLong(st.nextToken()));
|
||||
}
|
||||
|
||||
Arrays.sort(pts, (p, q) -> p[0] != q[0] ? Long.compare(q[0], p[0]) : Long.compare(q[1], p[1]));
|
||||
|
||||
List<long[]> front = new ArrayList<>();
|
||||
long maxY = 0;
|
||||
int i = 0;
|
||||
while (i < n) {
|
||||
int j = i;
|
||||
long curX = pts[i][0];
|
||||
long curMaxY = 0;
|
||||
while (j < n && pts[j][0] == curX) {
|
||||
curMaxY = Math.max(curMaxY, pts[j][1]);
|
||||
j++;
|
||||
}
|
||||
if (curMaxY > maxY) {
|
||||
front.add(new long[] { curX, curMaxY });
|
||||
maxY = curMaxY;
|
||||
}
|
||||
i = j;
|
||||
}
|
||||
|
||||
int m = front.size();
|
||||
long[] dp = new long[m + 1];
|
||||
dp[m] = 0;
|
||||
|
||||
for (int ii = m - 1; ii >= 0; ii--) {
|
||||
long a = front.get(ii)[0];
|
||||
long b = 0;
|
||||
dp[ii] = Long.MAX_VALUE;
|
||||
for (int jj = ii; jj < m; jj++) {
|
||||
b = Math.max(b, front.get(jj)[1]);
|
||||
long area = 4L * a * b;
|
||||
long total = area + dp[jj + 1];
|
||||
dp[ii] = Math.min(dp[ii], total);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(dp[0]);
|
||||
}
|
||||
}
|
||||
3
1/C/test1.in
Normal file
3
1/C/test1.in
Normal file
@@ -0,0 +1,3 @@
|
||||
2
|
||||
1 1
|
||||
-1 -1
|
||||
4
1/C/test2.in
Normal file
4
1/C/test2.in
Normal file
@@ -0,0 +1,4 @@
|
||||
3
|
||||
-7 19
|
||||
9 -30
|
||||
25 10
|
||||
7
1/C/test3.in
Normal file
7
1/C/test3.in
Normal file
@@ -0,0 +1,7 @@
|
||||
6
|
||||
1 20
|
||||
3 17
|
||||
5 15
|
||||
8 12
|
||||
9 11
|
||||
10 10
|
||||
139
1/D/Main.java
Normal file
139
1/D/Main.java
Normal file
@@ -0,0 +1,139 @@
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class Main {
|
||||
static int m;
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
StringTokenizer st = new StringTokenizer(br.readLine());
|
||||
int n = Integer.parseInt(st.nextToken());
|
||||
m = Integer.parseInt(st.nextToken());
|
||||
|
||||
int totalMasks = 1 << m;
|
||||
|
||||
int[][] maskPatterns = new int[totalMasks][];
|
||||
int[] maskSize = new int[totalMasks];
|
||||
int[] maskCount = new int[totalMasks];
|
||||
|
||||
char[][] patterns = new char[n][m];
|
||||
int[] masks = new int[n];
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
String line = br.readLine().trim();
|
||||
int mask = 0;
|
||||
for (int j = 0; j < m; j++) {
|
||||
patterns[i][j] = line.charAt(j);
|
||||
if (patterns[i][j] != '?')
|
||||
mask |= (1 << j);
|
||||
}
|
||||
masks[i] = mask;
|
||||
maskCount[mask]++;
|
||||
}
|
||||
|
||||
for (int mask = 0; mask < totalMasks; mask++) {
|
||||
maskPatterns[mask] = new int[maskCount[mask]];
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
int mask = masks[i];
|
||||
int code = encode(patterns[i], mask);
|
||||
maskPatterns[mask][maskSize[mask]++] = code;
|
||||
}
|
||||
|
||||
long answer = 0;
|
||||
|
||||
int[] projA = new int[n];
|
||||
int[] projB = new int[n];
|
||||
|
||||
for (int a = 0; a < totalMasks; a++) {
|
||||
if (maskPatterns[a].length == 0)
|
||||
continue;
|
||||
for (int b = a; b < totalMasks; b++) {
|
||||
if (maskPatterns[b].length == 0)
|
||||
continue;
|
||||
|
||||
int conflict = a & b;
|
||||
|
||||
if (a == b) {
|
||||
int sz = maskPatterns[a].length;
|
||||
for (int i = 0; i < sz; i++) {
|
||||
projA[i] = reproject(maskPatterns[a][i], a, conflict);
|
||||
}
|
||||
Arrays.sort(projA, 0, sz);
|
||||
int i = 0;
|
||||
while (i < sz) {
|
||||
int j = i + 1;
|
||||
while (j < sz && projA[j] == projA[i])
|
||||
j++;
|
||||
long cnt = j - i;
|
||||
answer += cnt * (cnt - 1) / 2;
|
||||
i = j;
|
||||
}
|
||||
} else {
|
||||
int sza = maskPatterns[a].length;
|
||||
int szb = maskPatterns[b].length;
|
||||
for (int i = 0; i < sza; i++)
|
||||
projA[i] = reproject(maskPatterns[a][i], a, conflict);
|
||||
for (int i = 0; i < szb; i++)
|
||||
projB[i] = reproject(maskPatterns[b][i], b, conflict);
|
||||
Arrays.sort(projA, 0, sza);
|
||||
Arrays.sort(projB, 0, szb);
|
||||
int i = 0, j = 0;
|
||||
while (i < sza && j < szb) {
|
||||
if (projA[i] == projB[j]) {
|
||||
int cntA = 1, cntB = 1;
|
||||
while (i + cntA < sza && projA[i + cntA] == projA[i])
|
||||
cntA++;
|
||||
while (j + cntB < szb && projB[j + cntB] == projB[j])
|
||||
cntB++;
|
||||
answer += (long) cntA * cntB;
|
||||
i += cntA;
|
||||
j += cntB;
|
||||
} else if (projA[i] < projB[j])
|
||||
i++;
|
||||
else
|
||||
j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(answer);
|
||||
}
|
||||
|
||||
static int encode(char[] pattern, int mask) {
|
||||
int code = 0;
|
||||
for (int i = 0; i < m; i++) {
|
||||
if ((mask & (1 << i)) != 0) {
|
||||
code = code * 27 + (pattern[i] - 'a' + 1);
|
||||
}
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
static int reproject(int code, int srcMask, int targetMask) {
|
||||
int[] vals = new int[6];
|
||||
int idx = 0;
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if ((srcMask & (1 << i)) != 0) {
|
||||
vals[idx++] = code % 27;
|
||||
code /= 27;
|
||||
}
|
||||
}
|
||||
int result = 0, mul = 1;
|
||||
int[] srcBits = new int[6];
|
||||
int srcCount = 0;
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if ((srcMask & (1 << i)) != 0)
|
||||
srcBits[srcCount++] = i;
|
||||
}
|
||||
for (int k = 0; k < srcCount; k++) {
|
||||
if ((targetMask & (1 << srcBits[k])) != 0) {
|
||||
result += vals[srcCount - 1 - k] * mul;
|
||||
mul *= 27;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
4
1/D/test1.in
Normal file
4
1/D/test1.in
Normal file
@@ -0,0 +1,4 @@
|
||||
3 3
|
||||
??b
|
||||
c??
|
||||
c?c
|
||||
5
1/D/test2.in
Normal file
5
1/D/test2.in
Normal file
@@ -0,0 +1,5 @@
|
||||
4 6
|
||||
ab??c?
|
||||
??kll?
|
||||
a?k??c
|
||||
?bcd??
|
||||
6
1/D/test3.in
Normal file
6
1/D/test3.in
Normal file
@@ -0,0 +1,6 @@
|
||||
5 2
|
||||
??
|
||||
b?
|
||||
c?
|
||||
?g
|
||||
cg
|
||||
174
1/E/Main.java
Normal file
174
1/E/Main.java
Normal file
@@ -0,0 +1,174 @@
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
static class FastReader {
|
||||
BufferedReader br;
|
||||
StringTokenizer st;
|
||||
|
||||
public FastReader() {
|
||||
br = new BufferedReader(new InputStreamReader(System.in));
|
||||
}
|
||||
|
||||
String next() {
|
||||
while (st == null || !st.hasMoreElements()) {
|
||||
try {
|
||||
st = new StringTokenizer(br.readLine());
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return st.nextToken();
|
||||
}
|
||||
|
||||
int nextInt() {
|
||||
return Integer.parseInt(next());
|
||||
}
|
||||
|
||||
long nextLong() {
|
||||
return Long.parseLong(next());
|
||||
}
|
||||
}
|
||||
|
||||
static int upperBound(long[] arr, long key) {
|
||||
int low = 0, high = arr.length - 1;
|
||||
int ans = arr.length;
|
||||
while (low <= high) {
|
||||
int mid = (low + high) >>> 1;
|
||||
if (arr[mid] > key) {
|
||||
ans = mid;
|
||||
high = mid - 1;
|
||||
} else {
|
||||
low = mid + 1;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
static void add(long[] bit, int idx, long val) {
|
||||
for (; idx < bit.length; idx += idx & -idx)
|
||||
bit[idx] += val;
|
||||
}
|
||||
|
||||
static long query(long[] bit, int idx) {
|
||||
long sum = 0;
|
||||
for (; idx > 0; idx -= idx & -idx)
|
||||
sum += bit[idx];
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
FastReader sc = new FastReader();
|
||||
String firstToken = sc.next();
|
||||
if (firstToken == null)
|
||||
return;
|
||||
int n = Integer.parseInt(firstToken);
|
||||
|
||||
long[] a = new long[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
a[i] = sc.nextLong();
|
||||
}
|
||||
|
||||
long[] u = new long[n];
|
||||
long[] v = new long[n];
|
||||
long min_val = Long.MAX_VALUE;
|
||||
long max_val = Long.MIN_VALUE;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
u[i] = a[i] - (i + 1);
|
||||
v[i] = a[i] + (i + 1);
|
||||
if (u[i] < min_val)
|
||||
min_val = u[i];
|
||||
if (v[i] < min_val)
|
||||
min_val = v[i];
|
||||
if (u[i] > max_val)
|
||||
max_val = u[i];
|
||||
if (v[i] > max_val)
|
||||
max_val = v[i];
|
||||
}
|
||||
|
||||
long[] sorted_u = u.clone();
|
||||
Arrays.sort(sorted_u);
|
||||
int u_unique = 1;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (sorted_u[i] != sorted_u[i - 1])
|
||||
sorted_u[u_unique++] = sorted_u[i];
|
||||
}
|
||||
sorted_u = Arrays.copyOf(sorted_u, u_unique);
|
||||
|
||||
long[] sorted_v = v.clone();
|
||||
Arrays.sort(sorted_v);
|
||||
int v_unique = 1;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (sorted_v[i] != sorted_v[i - 1])
|
||||
sorted_v[v_unique++] = sorted_v[i];
|
||||
}
|
||||
sorted_v = Arrays.copyOf(sorted_v, v_unique);
|
||||
|
||||
long[] bit_L_count = new long[u_unique + 1];
|
||||
long[] bit_L_sum = new long[u_unique + 1];
|
||||
long[] bit_R_count = new long[v_unique + 1];
|
||||
long[] bit_R_sum = new long[v_unique + 1];
|
||||
|
||||
long total_S_L = 0;
|
||||
long total_S_R = 0;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
int idx = Arrays.binarySearch(sorted_v, v[i]) + 1;
|
||||
add(bit_R_count, idx, 1);
|
||||
add(bit_R_sum, idx, v[i]);
|
||||
total_S_R += v[i];
|
||||
}
|
||||
|
||||
long ans = Long.MAX_VALUE;
|
||||
int targetCount = (n + 1) / 2;
|
||||
|
||||
for (int p = 1; p <= n; p++) {
|
||||
int idxU = Arrays.binarySearch(sorted_u, u[p - 1]) + 1;
|
||||
add(bit_L_count, idxU, 1);
|
||||
add(bit_L_sum, idxU, u[p - 1]);
|
||||
total_S_L += u[p - 1];
|
||||
|
||||
int idxV = Arrays.binarySearch(sorted_v, v[p - 1]) + 1;
|
||||
add(bit_R_count, idxV, -1);
|
||||
add(bit_R_sum, idxV, -v[p - 1]);
|
||||
total_S_R -= v[p - 1];
|
||||
|
||||
long low = min_val - 2L * n;
|
||||
long high = max_val + 2L * n;
|
||||
long x_med = high;
|
||||
|
||||
while (low <= high) {
|
||||
long mid_val = low + ((high - low) >> 1);
|
||||
|
||||
int countL = (int) query(bit_L_count, upperBound(sorted_u, mid_val));
|
||||
int countR = (int) query(bit_R_count, upperBound(sorted_v, mid_val + 2L * p));
|
||||
|
||||
if (countL + countR >= targetCount) {
|
||||
x_med = mid_val;
|
||||
high = mid_val - 1;
|
||||
} else {
|
||||
low = mid_val + 1;
|
||||
}
|
||||
}
|
||||
|
||||
long x = Math.max(x_med, Math.max(0L, n - 2L * p + 1L));
|
||||
|
||||
int iL = upperBound(sorted_u, x);
|
||||
long cL = query(bit_L_count, iL);
|
||||
long sL = query(bit_L_sum, iL);
|
||||
long costL = (cL * x - sL) + ((total_S_L - sL) - (p - cL) * x);
|
||||
|
||||
long y = x + 2L * p;
|
||||
int iR = upperBound(sorted_v, y);
|
||||
long cR = query(bit_R_count, iR);
|
||||
long sR = query(bit_R_sum, iR);
|
||||
long costR = (cR * y - sR) + ((total_S_R - sR) - ((n - p) - cR) * y);
|
||||
|
||||
ans = Math.min(ans, costL + costR);
|
||||
}
|
||||
|
||||
System.out.println(ans);
|
||||
}
|
||||
}
|
||||
2
1/E/test1.in
Normal file
2
1/E/test1.in
Normal file
@@ -0,0 +1,2 @@
|
||||
4
|
||||
1 1 2 3
|
||||
2
1/E/test2.in
Normal file
2
1/E/test2.in
Normal file
@@ -0,0 +1,2 @@
|
||||
5
|
||||
4 5 7 2 2
|
||||
2
1/E/test3.in
Normal file
2
1/E/test3.in
Normal file
@@ -0,0 +1,2 @@
|
||||
6
|
||||
4 5 6 5 4 3
|
||||
Reference in New Issue
Block a user