src
stringlengths
95
64.6k
complexity
stringclasses
7 values
problem
stringlengths
6
50
from
stringclasses
1 value
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.Arrays; import java.util.HashMap; import java.util.Queue; import java.util.StringTokenizer; public class CF268_TwoSets { public static void main(String[] args) { MyScanner in = new MyScanner(); int N = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); int[] vals = new int[N]; HashMap<Integer, Integer> val2Ind = new HashMap<Integer, Integer>(); for (int i = 0; i < N; i++) { vals[i] = in.nextInt(); val2Ind.put(vals[i], i); } int[] setAssignment = new int[N]; int[] friendA = new int[N]; int[] friendB = new int[N]; Arrays.fill(setAssignment, -1); Arrays.fill(friendA, -1); Arrays.fill(friendB, -1); // Mark partners for (int i = 0; i < N; i++) { Integer friendAInd = val2Ind.get(a - vals[i]); if (friendAInd != null) { friendA[i] = friendAInd; } Integer friendBInd = val2Ind.get(b - vals[i]); if (friendBInd != null) { friendB[i] = friendBInd; } } // Find those with only one friend Queue<Integer> toProc = new ArrayDeque<Integer>(); for (int i = 0; i < N; i++) { int friends = 0; if (friendA[i] != -1) { friends++; } if (friendB[i] != -1) { friends++; } if (friends == 1) { toProc.add(i); } } // Process the one frienders while (!toProc.isEmpty()) { int ind = toProc.poll(); if (setAssignment[ind] != -1) { continue; } if (friendA[ind] != -1) { int other = friendA[ind]; if (setAssignment[other] == -1) { setAssignment[ind] = 0; setAssignment[other] = 0; // Check other's friend if (friendB[other] != -1) { int otherOther = friendB[other]; friendB[otherOther] = -1; toProc.add(otherOther); } } else { System.out.println("NO"); return; } } else if (friendB[ind] != -1) { int other = friendB[ind]; if (setAssignment[other] == -1) { setAssignment[ind] = 1; setAssignment[other] = 1; // Check other's friend if (friendA[other] != -1) { int otherOther = friendA[other]; friendA[otherOther] = -1; toProc.add(otherOther); } } else { System.out.println("NO"); return; } } else { System.out.println("NO"); return; } } // Process those with two friends for(int i = 0; i < N; i++) { if(setAssignment[i] != -1) { continue; } if(friendA[i] == -1 && friendB[i] == -1) { System.out.println("NO"); return; } // Only possibility should now be that both friends are possible setAssignment[i] = 0; setAssignment[friendA[i]] = 0; } // Print the result System.out.println("YES"); StringBuilder sb = new StringBuilder(); for(int i = 0; i < N; i++) { sb.append(setAssignment[i]); sb.append(" "); } sb.deleteCharAt(sb.length() - 1); System.out.println(sb); } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
linear
468_B. Two Sets
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class ProblemB { Map<Integer, List<int[]>> dest; private ProblemB() throws IOException { BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); String h = rd.readLine(); String[] q = h.split("\\s+"); int a = Integer.parseInt(q[1]); int b = Integer.parseInt(q[2]); h = rd.readLine(); q = h.split(" "); int n = q.length; int[] p = new int[n]; for(int i=0;i<n;i++) { p[i] = Integer.parseInt(q[i]); } Set<Integer> pset = new HashSet<>(); for(int x: p) { pset.add(x); } if(a == b) { boolean res = true; for(int x: p) { if(!pset.contains(a-x)) { res = false; break; } } out(res?"YES":"NO"); if(res) { StringBuilder buf = new StringBuilder(); for(int i=0;i<n;i++) { if(i > 0) { buf.append(' '); } buf.append('0'); } out(buf); } } else { dest = new HashMap<>(); boolean res = true; for(int x: p) { boolean aOk = pset.contains(a-x); boolean bOk = pset.contains(b-x); if(!aOk && !bOk) { res = false; break; } else { if(aOk) { addEdgeAndBack(x,a-x,0); } if(bOk) { addEdgeAndBack(x,b-x,1); } } } Set<Integer> aSet = new HashSet<>(); if(res) { for(int x: p) { List<int[]> e = getEdges(x); if(e.size() == 1) { int[] edge = e.get(0); if(edge[0] == x) { if(edge[1] == 0) { aSet.add(x); } } else { boolean odd = true; int curA = edge[1]; int prev = x; while(true) { int cur = edge[0]; if(curA == 0 && odd) { aSet.add(prev); aSet.add(cur); } e = getEdges(cur); if(e.size() == 1) { if(!odd && e.get(0)[0] != cur) { res = false; } break; } int other = e.get(0)[0] == prev?1:0; edge = e.get(other); if(edge[1] == curA) { res = false; break; } curA = 1-curA; prev = cur; odd = !odd; } if(!res) { break; } } } } } out(res?"YES":"NO"); if(res) { StringBuilder buf = new StringBuilder(); for(int i=0;i<n;i++) { if(i>0) { buf.append(' '); } buf.append(aSet.contains(p[i])?'0':'1'); } out(buf); } } } private void addEdgeAndBack(int from, int to, int u) { addEdge(from, to, u); addEdge(to, from, u); } private void addEdge(int from, int to, int u) { List<int[]> edges = getEdges(from); for(int[] edge: edges) { if(edge[0] == to) { return; } } edges.add(new int[] { to, u }); } private List<int[]> getEdges(int from) { List<int[]> ds = dest.get(from); if(ds == null) { ds = new ArrayList<>(); dest.put(from, ds); } return ds; } private static void out(Object x) { System.out.println(x); } public static void main(String[] args) throws IOException { new ProblemB(); } }
linear
468_B. Two Sets
CODEFORCES
import java.io.*; import java.util.*; public class Main { static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter writer; static int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } static long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } static double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } static String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static void main(String[] args) throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(System.out); pineapple(); reader.close(); writer.close(); } static void pineapple() throws NumberFormatException, IOException { int n = nextInt(); int a = nextInt(); int b = nextInt(); HashSet<Integer> al = new HashSet<Integer>(); HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>(); int[] ans = new int[n]; Arrays.fill(ans, -1); HashSet<Integer> used = new HashSet<Integer>(); int[] mas = new int[n]; for (int i = 0; i < n; i++) { int t = nextInt(); al.add(t); mas[i] = t; mp.put(t, i); } for (int st : al) { if (used.contains(st)) continue; { int pr = st; int cc = -1; HashSet<Integer> u2 = new HashSet<Integer>(); u2.add(pr); if (!u2.contains(a - pr) && al.contains(a - pr)) cc = a - pr; if (!u2.contains(a - pr) && al.contains(b - pr)) cc = b - pr; if (cc != -1) { u2.add(cc); boolean bGo = true; while (bGo) { bGo = false; int nxt = -1; if (!u2.contains(a - cc) && al.contains(a - cc)) nxt = a - cc; if (!u2.contains(b - cc) && al.contains(b - cc)) nxt = b - cc; if (nxt != -1) { bGo = true; u2.add(nxt); cc = nxt; pr = cc; } } st = cc; } } LinkedList<Integer> ll = new LinkedList<Integer>(); ll.add(st); while (!ll.isEmpty()) { int curr = ll.pollFirst(); used.add(curr); int next1 = a - curr; if (al.contains(next1)) { if (!used.contains(next1)) { ll.addLast(next1); if (ans[mp.get(curr)] == -1 && ans[mp.get(next1)] == -1) { ans[mp.get(next1)] = 0; ans[mp.get(curr)] = 0; } } } int next2 = b - curr; if (al.contains(next2)) { if (!used.contains(next2)) { ll.addLast(next2); if (ans[mp.get(curr)] == -1 && ans[mp.get(next2)] == -1) { ans[mp.get(next2)] = 1; ans[mp.get(curr)] = 1; } } } } } for (int i = 0; i < n; i++) { if (ans[i] == -1) { if (2 * mas[i] == a) { ans[i] = 0; continue; } if (2 * mas[i] == b) { ans[i] = 1; continue; } writer.println("NO"); return; } } writer.println("YES"); for (int i = 0; i < n; i++) { writer.print(ans[i] + " "); } } }
linear
468_B. Two Sets
CODEFORCES
import java.util.*; import java.io.*; public class b { public static void main(String[] args) throws IOException { input.init(System.in); PrintWriter out = new PrintWriter(System.out); int n = input.nextInt(), a = input.nextInt(), b = input.nextInt(); Num[] data = new Num[n]; for(int i = 0; i<n; i++) data[i] = new Num(input.nextInt(), i); int[] res = new int[n]; Arrays.fill(res,-1); Arrays.sort(data); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i = 0; i<n; i++) map.put(data[i].x, data[i].i); boolean good = true; for(int i = 0; i<n; i++) { if(res[data[i].i] != -1) continue; int val = data[i].x; if(!map.containsKey(a-val) && !map.containsKey(b-val)) { good = false; break; } if(!map.containsKey(a-val)) { int other = map.get(b-val); if(res[other] == 0) { good = false; break; } res[other] = res[data[i].i] = 1; } else if(!map.containsKey(b-val)) { int other = map.get(a-val); if(res[other] == 1) { good = false; break; } res[other] = res[data[i].i] = 0; } else { int cur = data[i].i; int otherB = map.get(b-val), otherA = map.get(a-val); if(b > a && res[otherB] != 0) { res[cur] = res[otherB] = 1; } else if(a>b && res[otherA] != 1) { res[cur] = res[otherA] = 0; } else if(b > a && res[otherA] != 1) { res[cur] = res[otherA] = 0; } else if(a > b && res[otherB] != 0) { res[cur] = res[otherB] = 1; } else if(b == a) { res[cur] = res[otherA] = 0; } else { good = false; break; } } } if(good) { out.println("YES"); for(int x: res) out.print(x+" "); } else out.println("NO"); out.close(); } static class Num implements Comparable<Num> { int x, i; public Num(int xx, int ii) { x = xx; i = ii; } @Override public int compareTo(Num o) { // TODO Auto-generated method stub return x - o.x; } } public static class input { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } } }
linear
468_B. Two Sets
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class Main { private PrintWriter out; private Map<Integer, Integer> map; private int arr[], ans[]; int n, a, b; class DSU { private int[] p, size; public DSU(int n) { p = new int[n]; size = new int[n]; for (int i=0; i<n; i++) { p[i] = i; size[i] = 1; } } public int find(int i) { if (p[i] == i) { return i; } return p[i] = find(p[i]); } public void union (int a, int b) { a = find(a); b = find(b); if (size[a] > size[b]) { p[b] = a; size[a] += size[b]; } else { p[a] = b; size[b] += size[a]; } } } private void solve() throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); StringTokenizer st = new StringTokenizer(in.readLine()); n = Integer.valueOf(st.nextToken()); a = Integer.valueOf(st.nextToken()); b = Integer.valueOf(st.nextToken()); map = new HashMap<Integer, Integer>(n); String line = in.readLine(); StringTokenizer st1 = new StringTokenizer(line); arr = new int[n]; ans = new int[n]; DSU dsu = new DSU(n); for (int i=0; i<n; i++) { arr[i] = Integer.valueOf(st1.nextToken()); map.put(arr[i], i); } in.close(); for (int i=0; i<n; i++) { boolean f = false; if (map.get(a - arr[i]) != null) { f = true; dsu.union(i, map.get(a - arr[i])); } if (map.get(b - arr[i]) != null) { f = true; dsu.union(i, map.get(b - arr[i])); } if (!f) { out.println("NO"); out.flush(); return; } } for (int i=0; i<n; i++) { int p = dsu.find(i); if (map.get(a - arr[i]) == null) { ans[p] = 1; } else if (map.get(b - arr[i]) == null) { ans[p] = 0; } } for (int i=0; i<n; i++) { int p = dsu.find(i); if (ans[p] == 0 && map.get(a - arr[i]) == null) { out.println("NO"); out.flush(); return; } if (ans[p] == 1 && map.get(b - arr[i]) == null) { out.println("NO"); out.flush(); return; } } out.println("YES"); for (int i=0; i<n; i++) { out.print(ans[dsu.find(i)] + " "); } out.flush(); out.close(); } public static void main(String[] args) throws Exception { new Main().solve(); } }
linear
468_B. Two Sets
CODEFORCES
import java.io.*; import java.util.*; public class Pr468B { public static void main(String[] args) throws IOException { new Pr468B().run(); } BufferedReader in; PrintWriter out; StringTokenizer st; String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out, true); solve(); out.flush(); } int[] which; boolean[] was; int[] pa; int[] pb; void dfs(int i, boolean fa) { was[i] = true; if (fa) { if (pa[i] == -1) { out.println("NO"); out.flush(); System.exit(0); } which[i] = 0; which[pa[i]] = 0; if (pb[pa[i]] != -1 && !was[pb[pa[i]]]) { dfs(pb[pa[i]], fa); } } else { if (pb[i] == -1) { out.println("NO"); out.flush(); System.exit(0); } which[i] = 1; which[pb[i]] = 1; if (pa[pb[i]] != -1 && !was[pa[pb[i]]]) { dfs(pa[pb[i]], fa); } } } void solve() throws IOException { int n = nextInt(); int a = nextInt(); int b = nextInt(); int[] p = new int[n]; HashMap<Integer, Integer> allNums = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { p[i] = nextInt(); if (p[i] >= Math.max(a, b)) { out.println("NO"); return; } allNums.put(p[i], i); } pa = new int[n]; pb = new int[n]; Arrays.fill(pa, -1); Arrays.fill(pb, -1); which = new int[n]; Arrays.fill(which, -1); for (int i = 0; i < n; i++) { if (pa[i] == -1 && allNums.containsKey(a - p[i])) { pa[i] = allNums.get(a - p[i]); pa[pa[i]] = i; } if (pb[i] == -1 && allNums.containsKey(b - p[i])) { pb[i] = allNums.get(b - p[i]); pb[pb[i]] = i; } if (pa[i] == -1 && pb[i] == -1) { out.println("NO"); return; } } was = new boolean[n]; for (int i = 0; i < n; i++) { if (!was[i] && pa[i] == -1) { dfs(i, false); } else if (!was[i] && pb[i] == -1) { dfs(i, true); } } for (int i = 0; i < n; i++) { if (!was[i]) { dfs(i, true); } } out.println("YES"); for (int i = 0; i < n; i++) { out.print(which[i] + " "); } } }
linear
468_B. Two Sets
CODEFORCES
import java.io.BufferedInputStream; import java.io.BufferedWriter; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Map.Entry; import java.util.Scanner; import java.util.TreeMap; public class Main { public static void main(String[] args) { Scanner in = new Scanner(new BufferedInputStream(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(System.out))); while (in.hasNext()) { int n = in.nextInt(), a = in.nextInt(), b = in.nextInt(), c = 0; int[] p = new int[n]; TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>(); for (int i = 0; i < n; i++) { p[i] = in.nextInt(); map.put(p[i], i); } if (a > b) { int t = b; b = a; a = t; c = 1; } boolean ok = true; int[] cls = new int[n]; while (ok && map.size() > 0) { Entry<Integer, Integer> last = map.lastEntry(); int v = last.getKey(); int idx = last.getValue(); if (map.containsKey(a - v)) { cls[idx] = 0; cls[map.get(a - v)] = 0; map.remove(v); map.remove(a -v); } else if (map.containsKey(b - v)) { cls[idx] = 1; cls[map.get(b - v)] = 1; map.remove(v); map.remove(b -v); } else ok = false; } if (!ok) System.out.println("NO"); else { System.out.println("YES"); for (int j = 0; j < cls.length; j++) { if (j != 0) System.out.print(" "); System.out.print(c ^ cls[j]); } System.out.println(); } out.flush(); } in.close(); } }
linear
468_B. Two Sets
CODEFORCES
import java.util.*; import java.io.*; import java.awt.Point; import java.math.BigDecimal; import java.math.BigInteger; import static java.lang.Math.*; // Solution is at the bottom of code public class B implements Runnable{ final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; OutputWriter out; StringTokenizer tok = new StringTokenizer(""); public static void main(String[] args){ new Thread(null, new B(), "", 128 * (1L << 20)).start(); } ///////////////////////////////////////////////////////////////////// void init() throws FileNotFoundException{ Locale.setDefault(Locale.US); if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new OutputWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new OutputWriter("output.txt"); } } //////////////////////////////////////////////////////////////// long timeBegin, timeEnd; void time(){ timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } void debug(Object... objects){ if (ONLINE_JUDGE){ for (Object o: objects){ System.err.println(o.toString()); } } } ///////////////////////////////////////////////////////////////////// public void run(){ try{ timeBegin = System.currentTimeMillis(); Locale.setDefault(Locale.US); init(); solve(); out.close(); time(); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } ///////////////////////////////////////////////////////////////////// String delim = " "; String readString() throws IOException{ while(!tok.hasMoreTokens()){ try{ tok = new StringTokenizer(in.readLine()); }catch (Exception e){ return null; } } return tok.nextToken(delim); } String readLine() throws IOException{ return in.readLine(); } ///////////////////////////////////////////////////////////////// final char NOT_A_SYMBOL = '\0'; char readChar() throws IOException{ int intValue = in.read(); if (intValue == -1){ return NOT_A_SYMBOL; } return (char) intValue; } char[] readCharArray() throws IOException{ return readLine().toCharArray(); } ///////////////////////////////////////////////////////////////// int readInt() throws IOException { return Integer.parseInt(readString()); } int[] readIntArray(int size) throws IOException { int[] array = new int[size]; for (int index = 0; index < size; ++index){ try { array[index] = readInt(); } catch (Exception e) { System.err.println(index); return null; } } return array; } int[] readSortedIntArray(int size) throws IOException { Integer[] array = new Integer[size]; for (int index = 0; index < size; ++index) { array[index] = readInt(); } Arrays.sort(array); int[] sortedArray = new int[size]; for (int index = 0; index < size; ++index) { sortedArray[index] = array[index]; } return sortedArray; } int[] readIntArrayWithDecrease(int size) throws IOException { int[] array = readIntArray(size); for (int i = 0; i < size; ++i) { array[i]--; } return array; } /////////////////////////////////////////////////////////////////// int[][] readIntMatrix(int rowsCount, int columnsCount) throws IOException { int[][] matrix = new int[rowsCount][]; for (int rowIndex = 0; rowIndex < rowsCount; ++rowIndex) { matrix[rowIndex] = readIntArray(columnsCount); } return matrix; } int[][] readIntMatrixWithDecrease(int rowsCount, int columnsCount) throws IOException { int[][] matrix = new int[rowsCount][]; for (int rowIndex = 0; rowIndex < rowsCount; ++rowIndex) { matrix[rowIndex] = readIntArrayWithDecrease(columnsCount); } return matrix; } /////////////////////////////////////////////////////////////////// long readLong() throws IOException{ return Long.parseLong(readString()); } long[] readLongArray(int size) throws IOException{ long[] array = new long[size]; for (int index = 0; index < size; ++index){ array[index] = readLong(); } return array; } //////////////////////////////////////////////////////////////////// double readDouble() throws IOException{ return Double.parseDouble(readString()); } double[] readDoubleArray(int size) throws IOException{ double[] array = new double[size]; for (int index = 0; index < size; ++index){ array[index] = readDouble(); } return array; } //////////////////////////////////////////////////////////////////// BigInteger readBigInteger() throws IOException { return new BigInteger(readString()); } BigDecimal readBigDecimal() throws IOException { return new BigDecimal(readString()); } ///////////////////////////////////////////////////////////////////// Point readPoint() throws IOException{ int x = readInt(); int y = readInt(); return new Point(x, y); } Point[] readPointArray(int size) throws IOException{ Point[] array = new Point[size]; for (int index = 0; index < size; ++index){ array[index] = readPoint(); } return array; } ///////////////////////////////////////////////////////////////////// List<Integer>[] readGraph(int vertexNumber, int edgeNumber) throws IOException{ @SuppressWarnings("unchecked") List<Integer>[] graph = new List[vertexNumber]; for (int index = 0; index < vertexNumber; ++index){ graph[index] = new ArrayList<Integer>(); } while (edgeNumber-- > 0){ int from = readInt() - 1; int to = readInt() - 1; graph[from].add(to); graph[to].add(from); } return graph; } ///////////////////////////////////////////////////////////////////// static class IntIndexPair { static Comparator<IntIndexPair> increaseComparator = new Comparator<B.IntIndexPair>() { @Override public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) { int value1 = indexPair1.value; int value2 = indexPair2.value; if (value1 != value2) return value1 - value2; int index1 = indexPair1.index; int index2 = indexPair2.index; return index1 - index2; } }; static Comparator<IntIndexPair> decreaseComparator = new Comparator<B.IntIndexPair>() { @Override public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) { int value1 = indexPair1.value; int value2 = indexPair2.value; if (value1 != value2) return -(value1 - value2); int index1 = indexPair1.index; int index2 = indexPair2.index; return index1 - index2; } }; int value, index; public IntIndexPair(int value, int index) { super(); this.value = value; this.index = index; } public int getRealIndex() { return index + 1; } } IntIndexPair[] readIntIndexArray(int size) throws IOException { IntIndexPair[] array = new IntIndexPair[size]; for (int index = 0; index < size; ++index) { array[index] = new IntIndexPair(readInt(), index); } return array; } ///////////////////////////////////////////////////////////////////// static class OutputWriter extends PrintWriter { final int DEFAULT_PRECISION = 12; protected int precision; protected String format, formatWithSpace; { precision = DEFAULT_PRECISION; format = createFormat(precision); formatWithSpace = format + " "; } public OutputWriter(OutputStream out) { super(out); } public OutputWriter(String fileName) throws FileNotFoundException { super(fileName); } public int getPrecision() { return precision; } public void setPrecision(int precision) { precision = max(0, precision); this.precision = precision; format = createFormat(precision); formatWithSpace = format + " "; } private String createFormat(int precision){ return "%." + precision + "f"; } @Override public void print(double d){ printf(format, d); } public void printWithSpace(double d){ printf(formatWithSpace, d); } public void printAll(double...d){ for (int i = 0; i < d.length - 1; ++i){ printWithSpace(d[i]); } print(d[d.length - 1]); } @Override public void println(double d){ printlnAll(d); } public void printlnAll(double... d){ printAll(d); println(); } } ///////////////////////////////////////////////////////////////////// static final int[][] steps = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; static final int[][] steps8 = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, 1}, {1, -1}, {-1, 1} }; static final boolean check(int index, int lim){ return (0 <= index && index < lim); } ///////////////////////////////////////////////////////////////////// static final boolean checkBit(int mask, int bit){ return (mask & (1 << bit)) != 0; } ///////////////////////////////////////////////////////////////////// static final long getSum(int[] array) { long sum = 0; for (int value: array) { sum += value; } return sum; } static final Point getMinMax(int[] array) { int min = array[0]; int max = array[0]; for (int index = 0, size = array.length; index < size; ++index, ++index) { int value = array[index]; if (index == size - 1) { min = min(min, value); max = max(max, value); } else { int otherValue = array[index + 1]; if (value <= otherValue) { min = min(min, value); max = max(max, otherValue); } else { min = min(min, otherValue); max = max(max, value); } } } return new Point(min, max); } ///////////////////////////////////////////////////////////////////// void solve() throws IOException { int n = readInt(); int a = readInt(); int b = readInt(); Map<Integer, Integer> numbers = new HashMap<>(); int[] p = readIntArray(n); for (int index = 0; index < n; ++index) { numbers.put(p[index], index); } Set<Integer> used = new HashSet<Integer>(); Deque<Integer> q = new ArrayDeque<Integer>(); int[] answers = new int[n]; for (int i = 0; i < n; ++i) { if (used.contains(p[i])) continue; int leftSize = 0; for (int number = p[i], cur = a, next = b; numbers.containsKey(number) && !used.contains(number); number = cur - number, cur = (a ^ b ^ cur), next = (a ^ b ^ next)) { q.addFirst(number); used.add(number); ++leftSize; } for (int number = b - p[i], cur = a, next = b; numbers.containsKey(number) && !used.contains(number); number = cur - number, cur = (a ^ b ^ cur), next = (a ^ b ^ next)) { q.addLast(number); used.add(number); } int curColor = (leftSize & 1); if ((q.size() & 1) == 1) { int first = q.peekFirst(); // 0 - a, 1 - b if (curColor == 0 && (first << 1) == b || curColor == 1 && (first << 1) == a) { q.poll(); curColor ^= 1; int firstIndex = numbers.get(first); answers[firstIndex] = curColor; } else { int last = q.peekLast(); // 0 - b, 1 - a if (curColor == 0 && (last << 1) == a || curColor == 1 && (first << 1) == b) { q.poll(); int firstIndex = numbers.get(first); answers[firstIndex] = curColor; } else { out.println("NO"); return; } } } while (q.size() > 0) { int first = q.poll(); int second = q.poll(); int firstIndex = numbers.get(first); int secondIndex = numbers.get(second); answers[firstIndex] = curColor; answers[secondIndex] = curColor; } } out.println("YES"); for (int answer : answers) { out.print(answer + " "); } out.println(); } }
linear
468_B. Two Sets
CODEFORCES
import java.util.List; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.InputMismatchException; import java.io.PrintStream; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Random; import java.io.Reader; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Nipuna Samarasekara */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { ///////////////////////////////////////////////////////////// static nn[] B; static int n,a,b; public void solve(int testNumber, FastScanner in, FastPrinter out) { n=in.nextInt();a=in.nextInt();b=in.nextInt(); int ccc=0; if (a==b)ccc=1; int[] A=in.readIntArray(n); B=new nn[n]; for (int i = 0; i < n; i++) { B[i]=new nn(A[i],i); } ArrayUtils.shuffle(B); Arrays.sort(B); int chk=1; for (int i = 0; i < n; i++) { if (B[i].assign>=0)continue; int v=B[i].val; int cc=0; int pos1=Arrays.binarySearch(B,new nn(a-v,0)); if (pos1>=0&&B[pos1].assign==-1)cc++; if (a!=b){ int pos2=Arrays.binarySearch(B,new nn(b-v,0)); if (pos2>=0&&B[pos2].assign==-1)cc++; } if (cc==0){ chk=0; break; } if (cc==1){ go(i); } } if (chk==0){ out.println("NO"); return; } int[] ans= new int[n]; for (int i = 0; i < n; i++) { ans[B[i].pos]=B[i].assign; } out.println("YES"); for (int i = 0; i < n; i++) { out.print(ans[i] + " "); } out.println(); } static void go (int i){ int v=B[i].val; int pos1=Arrays.binarySearch(B,new nn(a-v,0)); if (pos1>=0&&B[pos1].assign==-1){ B[i].assign=0; B[pos1].assign=0; int vv=B[pos1].val; int np=Arrays.binarySearch(B,new nn(b-vv,0)); if (np>=0)go(np); } if (a!=b){ int pos2=Arrays.binarySearch(B,new nn(b-v,0)); if (pos2>=0&&B[pos2].assign==-1){ B[i].assign=1; B[pos2].assign=1; int vv=B[pos2].val; int np=Arrays.binarySearch(B,new nn(a-vv,0)); if (np>=0)go(np); } } } } class nn implements Comparable<nn> { int val,pos; public String toString() { return "nn{" + "val=" + val + ", pos=" + pos + ", assign=" + assign + ", ct=" + ct + '}'; } int assign=-1; int ct=0; nn(int val, int pos) { this.val = val; this.pos = pos; } public int compareTo(nn o) { return this.val-o.val; //To change body of implemented methods use File | Settings | File Templates. } } class FastScanner extends BufferedReader { public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); // if (isEOF && ret < 0) { // throw new InputMismatchException(); // } // isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (c >= 0 && !isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } } class ArrayUtils { static final long seed = System.nanoTime(); static final Random rand = new Random(seed); public static <T> void shuffle(T[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); T t = a[i]; a[i] = a[j]; a[j] = t; } } }
linear
468_B. Two Sets
CODEFORCES
import java.util.Map; import java.io.IOException; import java.util.TreeMap; import java.util.InputMismatchException; import java.io.PrintStream; import java.io.OutputStream; import java.util.ArrayDeque; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Queue; import java.util.Collection; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author karan173 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { int val[]; int p[]; int aneigh[], bneight[], deg[]; public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.ni (); val = new int[n]; int a = in.ni (); int b = in.ni (); Map<Integer, Integer> set = new TreeMap<Integer, Integer> (); p = in.iArr (n); for (int i = 0; i < n; i++) { set.put (p[i], i); } aneigh = new int[n]; bneight = new int[n]; deg = new int[n]; for (int i = 0; i < n; i++) { aneigh[i] = val[i] = bneight[i] = -1; deg[i] = 0; } Queue<Integer> queue = new ArrayDeque<Integer> (); for (int i = 0; i < n; i++) { Integer x1 = set.get (a - p[i]); Integer x2 = set.get (b - p[i]); if (x1 != null) { aneigh[i] = x1; deg[i]++; } if (x2 != null && a != b) { bneight[i] = x2; deg[i]++; } if (deg[i] == 1) { queue.add (i); } } while (!queue.isEmpty ()) { int idx = queue.remove (); if (deg[idx] != 1) { continue; } int aa = aneigh[idx]; int bb = bneight[idx]; if (aa != -1) { val[idx] = val[aa] = 0; deg[aa]--; deg[idx]--; aneigh[aa] = -1; aneigh[idx] = -1; if (deg[aa] == 1) { int zz = bneight[aa]; bneight[zz] = -1; deg[zz]--; if(deg[zz] == 1) queue.add (zz); } } else { val[idx] = val[bb] = 1; deg[bb]--; deg[idx]--; bneight[idx] = bneight[bb] = -1; if (deg[bb] == 1) { //queue.add (bb); int zz = aneigh[bb]; aneigh[zz] = -1; deg[zz]--; if(deg[zz] == 1) queue.add (zz); } } } for (int i = 0; i < n; i++) { if (val[i] == -1 && cantBePaired(i)) { out.println ("NO"); return; } } //every person has two neighbours out.println ("YES"); for (int i = 0; i < n; i++) { out.print (val[i] + " "); } out.println (); } private boolean cantBePaired(int i) { int aa = aneigh[i]; int bb = bneight[i]; if (aa != -1 && val[aa] == -1) { return false; } if (bb != -1 && val[bb] == -1) { return false; } return true; } } class FastReader { public InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public FastReader() { } public int read() { if (numChars == -1) { throw new InputMismatchException (); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read (buf); } catch (IOException e) { throw new InputMismatchException (); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int ni() { int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-') { sgn = -1; c = read (); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } while (!isSpaceChar (c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar (c); } return isWhitespace (c); } public static boolean isWhitespace(int c) { return c==' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] iArr(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni (); } return a; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
linear
468_B. Two Sets
CODEFORCES
import java.util.ArrayList; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.io.BufferedReader; import java.util.List; import java.util.Map; import java.math.BigInteger; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; /** * Built using CHelper plug-in * Actual solution is at the top * @author shu_mj @ http://shu-mj.com */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { Scanner in; PrintWriter out; public void solve(int testNumber, Scanner in, PrintWriter out) { this.in = in; this.out = out; run(); } void run() { int n = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); int[] is = in.nextIntArray(n); Map<Integer, Integer> id = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { id.put(is[i], i); } SCC.V[] vs = new SCC.V[n * 2]; for (int i = 0; i < vs.length; i++) vs[i] = new SCC.V(); for (int i = 0; i < n; i++) { if (id.containsKey(a - is[i])) { int j = id.get(a - is[i]); vs[i].add(vs[j]); vs[j + n].add(vs[i + n]); } else { vs[i].add(vs[i + n]); } if (id.containsKey(b - is[i])) { int j = id.get(b - is[i]); vs[i + n].add(vs[j + n]); vs[j].add(vs[i]); } else { vs[i + n].add(vs[i]); } } SCC.scc(vs); for (int i = 0; i < n; i++) { if (vs[i].comp == vs[i + n].comp) { out.println("NO"); return ; } } out.println("YES"); for (int i = 0; i < n; i++) { if (vs[i].comp > vs[i + n].comp) { out.print("0 "); } else { out.print("1 "); } } out.println(); } } class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); eat(""); } private void eat(String s) { st = new StringTokenizer(s); } public String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } public boolean hasNext() { while (!st.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextIntArray(int n) { int[] is = new int[n]; for (int i = 0; i < n; i++) { is[i] = nextInt(); } return is; } } class SCC { public static int n; public static V[] us; public static int scc(V[] vs) { n = vs.length; us = new V[n]; for (V v : vs) if (!v.visit) dfs(v); for (V v : vs) v.visit = false; for (V u : us) if (!u.visit) dfsRev(u, n++); return n; } public static void dfs(V v) { v.visit = true; for (V u : v.fs) if (!u.visit) dfs(u); us[--n] = v; } public static void dfsRev(V v, int k) { v.visit = true; for (V u : v.rs) if (!u.visit) dfsRev(u, k); v.comp = k; } public static class V { public boolean visit; public int comp; public List<V> fs = new ArrayList<V>(); public List<V> rs = new ArrayList<V>(); public void add(V u) { fs.add(u); u.rs.add(this); } } }
linear
468_B. Two Sets
CODEFORCES
import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Scanner; public class TwoSets<V> { private static int n; private static int a; private static int b; private static List<Node<Integer>> nodes = new LinkedList<Node<Integer>>(); private static Map<Integer, Node<Integer>> datas = new HashMap<Integer, Node<Integer>>(); private static Node<Integer> first; private static Node<Integer> second; private static TwoSets<Integer> sets = new TwoSets<Integer>(); private static class Node<V> { V node; Node<V> parent; int rank; int color = -1; boolean inprogress; public Node() { this.parent = this; } @Override public String toString() { return String.format("{node: %s, parent: %s, rank: %s, color:%s}", node, parent.node, rank, color); } } public Node<V> makeSet(V x) { Node<V> node = new Node<V>(); node.node = x; node.parent = node; node.rank = 0; return node; } public void link(Node<V> x, Node<V> y) { if (x.rank > y.rank) { y.parent = x; } else { x.parent = y; if (x.rank == y.rank) { y.rank++; } } } public Node<V> findSet(Node<V> x) { if (x.parent != x) { x.parent = findSet(x.parent); } return x.parent; } public void union(Node<V> x, Node<V> y) { Node<V> rtX = findSet(x); Node<V> rtY = findSet(y); if (rtX.parent != rtY.parent) { link(rtX, rtY); } } public V getNode(Node<V> x) { return x.node; } private int getColor(Node<V> node) { int color; Node<V> parent = findSet(node); color = parent.color; return color; } private void setColor(Node<V> node, int color) { Node<V> parent = findSet(node); parent.color = color; } private static Node<Integer> getOrInitNode(Integer key) { Node<Integer> node = datas.get(key); if (node == null) { node = sets.makeSet(key); datas.put(key, node); } return node; } private static void initNodes(Scanner scanner) { int key; Node<Integer> node; for (int i = 0; i < n; i++) { key = scanner.nextInt(); node = getOrInitNode(key); nodes.add(node); } } private static void unionAll(Node<Integer> value) { int color = sets.getColor(value); if (color == 0) { if (first == null) { first = value; } else { sets.union(first, value); } } else if (color == 1) { if (second == null) { second = value; } else { sets.union(second, value); } } } private static int getKey(Node<Integer> value, int color) { int key = value.node; if (color == 0) { key = a - key; } else { key = b - key; } return key; } private static boolean checkOpposite(Node<Integer> value, int color) { boolean valid; if (value.inprogress) { valid = Boolean.TRUE; } else { value.inprogress = Boolean.TRUE; int opColor = 1 - color; int key = getKey(value, opColor); Node<Integer> node = datas.get(key); valid = (value.node.equals(key)) || (node == null); if (!valid) { key = getKey(node, color); Node<Integer> child = datas.get(key); valid = (child != null); if (valid) { valid = checkOpposite(child, color); if (valid) { sets.union(value, node); sets.union(value, child); value.inprogress = Boolean.FALSE; } } } value.inprogress = Boolean.FALSE; } return valid; } private static boolean checkNodes(Node<Integer> value, int color) { boolean valid; int key = getKey(value, color); int opColor = 1 - color; Node<Integer> node = datas.get(key); valid = (value.node.equals(key)) || (node != null); if (valid) { valid = checkOpposite(value, color); if (valid) { sets.union(value, node); sets.setColor(value, color); } else if (color == 0) { valid = checkNodes(value, opColor); } } else if (color == 0) { valid = checkNodes(value, opColor); } return valid; } private static void format(StringBuilder builder, int i) { if (i > 0) { builder.append(' '); } } private static String printNodes() { String text; StringBuilder builder = new StringBuilder(); Iterator<Node<Integer>> iterator = nodes.iterator(); int i = 0; Node<Integer> node; while (iterator.hasNext()) { format(builder, i); node = iterator.next(); builder.append(sets.getColor(node)); i++; } text = builder.toString().trim(); return text; } private static boolean checkNodes(int color) { boolean valid = Boolean.TRUE; for (Node<Integer> value : nodes) { if (sets.getColor(value) == -1) { valid = checkNodes(value, color); if (valid) { unionAll(value); } else { break; } } } return valid; } private static void calculate() { int color = 0; boolean valid = checkNodes(color); String message = "NO"; if (valid) { message = "YES"; String array = printNodes(); System.out.println(message); System.out.println(array); } else { System.out.println(message); } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { n = scanner.nextInt(); a = scanner.nextInt(); b = scanner.nextInt(); initNodes(scanner); calculate(); } finally { scanner.close(); } } }
linear
468_B. Two Sets
CODEFORCES
import java.io.*; import java.util.*; import java.util.List; public class Main { private static StringTokenizer st; private static BufferedReader br; public static long MOD = 1000000007; public static long tenFive = 100000; public static int INF = 100000; public static void print(Object x) { System.out.println(x + ""); } public static void printArr(long[] x) { StringBuilder s = new StringBuilder(); for (int i = 0; i < x.length; i++) { s.append(x[i] + " "); } print(s); } public static void printArr(int[] x) { StringBuilder s = new StringBuilder(); for (int i = 0; i < x.length; i++) { s.append(x[i] + " "); } print(s); } public static String join(Collection<?> x, String space) { if (x.size() == 0) return ""; StringBuilder sb = new StringBuilder(); boolean first = true; for (Object elt : x) { if (first) first = false; else sb.append(space); sb.append(elt); } return sb.toString(); } public static String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = br.readLine(); st = new StringTokenizer(line.trim()); } return st.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public static long nextLong() throws IOException { return Long.parseLong(nextToken()); } public static List<Integer> nextInts(int N) throws IOException { List<Integer> ret = new ArrayList<Integer>(); for (int i = 0; i < N; i++) { ret.add(nextInt()); } return ret; } public static void solve(int a, int b, List<Integer> orig) { boolean swap = false; if (a > b) { swap = true; int tmp = a; a = b; b = tmp; } List<Integer> nums = new ArrayList<Integer>(orig); Collections.sort(nums); Collections.reverse(nums); Set<Integer> all = new HashSet<Integer>(nums); Set<Integer> done = new HashSet<Integer>(); Set<Integer> inB = new HashSet<Integer>(); for (int x : nums) { if (done.contains(x)) continue; if (all.contains(a - x) && !done.contains(a - x)) { done.add(x); done.add(a - x); //print(x + " " + (a - x)); } else if (all.contains(b - x) && !done.contains(b - x)) { done.add(x); done.add(b - x); inB.add(x); inB.add(b - x); } else { print("NO"); return; } } print("YES"); List<Integer> out = new ArrayList<Integer>(); for (int x : orig) { if (inB.contains(x) ^ swap) out.add(1); else out.add(0); } print(join(out, " ")); } public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); int n = nextInt(); int a = nextInt(); int b = nextInt(); List<Integer> nums = nextInts(n); solve(a, b, nums); } }
linear
468_B. Two Sets
CODEFORCES
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main2 { static List<List<Integer>> getLayers(int[] numbers, int a, int b) { boolean[] used = new boolean[numbers.length]; HashSet<Integer> hs = new HashSet<Integer>(); for (int i = 0; i < numbers.length; i++) { hs.add(numbers[i]); } HashMap<Integer, Integer> numberToIndex = new HashMap<Integer, Integer>(); for (int i = 0; i < numbers.length; i++) { numberToIndex.put(numbers[i], i); } List<List<Integer>> ans = new ArrayList<List<Integer>>(); for (int i = 0; i < numbers.length; i++) { if (!used[i]) { List<Integer> ansRow = new ArrayList<Integer>(); LinkedList<Integer> current = new LinkedList<Integer>(); current.add(numbers[i]); while (!current.isEmpty()) { int c = current.removeFirst(); used[numberToIndex.get(c)] = true; boolean found = false; if (hs.contains(a - c)) { found = true; if (a - c != c) current.add(a - c); } if (hs.contains(b - c)) { found = true; if (b - c != c) current.add(b - c); } if (found || ansRow.size() > 0) ansRow.add(c); hs.remove(c); } ans.add(ansRow); } } return ans; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int[] numbers = new int[n]; for (int i = 0; i < numbers.length; i++) { numbers[i] = sc.nextInt(); } HashSet<Integer> hs = new HashSet<Integer>(); for (int i = 0; i < numbers.length; i++) { hs.add(numbers[i]); } int[] belongs = new int[n]; for (int i = 0; i < belongs.length; i++) { belongs[i] = -1; } HashMap<Integer, Integer> numberToIndex = new HashMap<Integer, Integer>(); for (int i = 0; i < numbers.length; i++) { numberToIndex.put(numbers[i], i); } boolean possible = true; List<List<Integer>> layers = getLayers(numbers, a, b); for (List<Integer> layer : layers) { if (layer.size() == 0) { System.out.println("NO"); return; } int starting = -1; for (int j = 0; j < layer.size(); j++) { int cur = layer.get(j); int nei = 0; if (hs.contains(a - cur)) { nei++; } if (hs.contains(b - cur)) { nei++; } if (nei == 1 || (a == b && nei == 2)) { starting = j; } } if (starting == -1) throw new Error(); int c = layer.get(starting); HashSet<Integer> layerset = new HashSet<Integer>(layer); while (true) { if (layerset.contains(c) && layerset.contains(a - c)) { belongs[numberToIndex.get(c)] = 0; belongs[numberToIndex.get(a - c)] = 0; layerset.remove(c); layerset.remove(a - c); c = b - (a - c); } else if (layerset.contains(c) && layerset.contains(b - c)) { belongs[numberToIndex.get(c)] = 1; belongs[numberToIndex.get(b - c)] = 1; layerset.remove(c); layerset.remove(b - c); c = a - (b - c); } else { break; } } } printResult(belongs); } static void printResult(int[] belongs) { boolean ok = true; for (int i = 0; i < belongs.length; i++) { if (belongs[i] < 0) ok = false; } if (ok) { System.out.println("YES"); StringBuffer sb = new StringBuffer(); for (int i = 0; i < belongs.length; i++) { sb.append(belongs[i]); if (i != belongs.length - 1) sb.append(" "); } System.out.println(sb.toString()); } else { System.out.println("NO"); } } }
linear
468_B. Two Sets
CODEFORCES
import java.io.*; import java.math.BigInteger; import java.util.*; public class B { void solve() throws IOException { int n=nextInt(); int a=nextInt(); int b=nextInt(); int[] p=new int[n]; for(int i=0;i<n;i++)p[i]=nextInt(); // if(n%2==1){ // out.println("NO"); // return; // } TreeSet<Integer>[] s=new TreeSet[n]; for(int i=0;i<n;i++)s[i]=new TreeSet<Integer>(); HashMap<Integer,Integer> m=new HashMap<Integer, Integer>(); for(int i=0;i<n;i++) m.put(p[i],i); for(int i=0;i<n;i++){ if(m.containsKey(a-p[i])){ s[i].add(a-p[i]); s[m.get(a-p[i])].add(p[i]); } if(m.containsKey(b-p[i])){ s[i].add(b-p[i]); s[m.get(b-p[i])].add(p[i]); } } int last=-1; LinkedList<Integer> q=new LinkedList<Integer>(); for(int i=0;i<n;i++){ if(s[i].size()==0){ out.println("NO"); return; } if(s[i].size()==1){ q.add(i); } } int[] ans=new int[n]; while(last!=n){ while(!q.isEmpty()){ int cur=q.poll(); if(s[cur].size()==0)continue; int x=p[cur]; int y=s[cur].first(); if(x==a-y){ ans[cur]=1; ans[m.get(y)]=1; } else{ ans[cur]=2; ans[m.get(y)]=2; } for(Integer u:s[m.get(y)]){ int o=m.get(u); if(o!=m.get(y)) { s[o].remove(y); if (s[o].size() == 1) q.add(o); } } for(Integer u:s[cur]){ int o=m.get(u); if(o!=m.get(y)) { s[o].remove(y); if (s[o].size() == 1) q.add(o); } } } last++; while(last!=n){ if(s[last].size()!=0&&ans[last]==0)break; last++; } if(last!=n){ q.add(last); } } for(int i=0;i<n;i++) if(ans[i]==0){ out.println("NO"); return; } out.println("YES"); for(int i=0;i<n;i++) out.print((ans[i]-1)+" "); } public static void main(String[] args) throws IOException { new B().run(); } void run() throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); // reader = new BufferedReader(new FileReader("input.txt")); tokenizer = null; out = new PrintWriter(new OutputStreamWriter(System.out)); // out = new PrintWriter(new FileWriter("output.txt")); solve(); reader.close(); out.flush(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter out; int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
linear
468_B. Two Sets
CODEFORCES
import java.util.*; import java.io.*; public class Main { BufferedReader in; StringTokenizer str = null; PrintWriter out; private String next() throws Exception{ while (str == null || !str.hasMoreElements()) str = new StringTokenizer(in.readLine()); return str.nextToken(); } private int nextInt() throws Exception{ return Integer.parseInt(next()); } private long nextLong() throws Exception{ return Long.parseLong(next()); } private double nextDouble() throws Exception{ return Double.parseDouble(next()); } Map<Integer, Integer> map; int []p, rank; public void run() throws Exception{ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); map = new HashMap<Integer, Integer>(); int n = nextInt(); int x = nextInt(), y = nextInt(); int []a = new int[n]; p = new int[n]; for(int i = 1; i < n; ++i) p[i] = i; rank = new int[n]; for(int i = 0; i < n; ++i) { a[i] = nextInt(); map.put(a[i], i); } int mask[] = new int[n]; for(int i = 0; i < n; ++i) { if (map.containsKey(x - a[i])) { union(map.get(x - a[i]), i); mask[i] |= 1; } if (map.containsKey(y - a[i])) { union(map.get(y - a[i]), i); mask[i] |= 2; } } int []b = new int[n]; Arrays.fill(b, 3); for(int i = 0; i < n; ++i) b[find(i)] &= mask[i]; for(int i = 0; i < n; ++i) { if (b[i] == 0) { out.println("NO"); out.close(); return; } } out.println("YES"); for(int i = 0; i < n; ++i) { out.print((b[find(i)] & 1) == 1 ? 0 : 1); if (i != n - 1) out.print(" "); } out.println(); out.close(); } private int find(int x) { if (x != p[x]) return p[x] = find(p[x]); return x; } private void union(int a, int b) { a = find(a); b = find(b); if (rank[a] < rank[b]) { int tmp = a; a = b; b = tmp; } p[b] = a; if (rank[a] == rank[b]) ++rank[a]; } public static void main(String[] args) throws Exception{ new Main().run(); } }
linear
468_B. Two Sets
CODEFORCES
import java.util.Map; import java.io.InputStreamReader; import java.io.IOException; import java.util.TreeMap; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Hamed Valizadeh (havaliza@gmail.com) */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInputReader in = new FastInputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { int n, a, b; Map<Integer, Integer> position; int[] p; int[] group; public void solve(int testNumber, FastInputReader in, PrintWriter out) { n = in.nextInt(); a = in.nextInt(); b = in.nextInt(); position = new TreeMap<Integer, Integer>(); p = new int[n]; group = new int[n]; for (int i = 0; i < n; i++) { p[i] = in.nextInt(); group[i] = -1; position.put(p[i], i); } for (int i = 0; i < n; i++) { if (getMate(i) != -1) continue; out.println("NO"); return; } for (int i = 0; i < n; i++) { boolean aMate = position.containsKey(a - p[i]); boolean bMate = position.containsKey(b - p[i]); if (aMate && bMate) continue; if (group[i] != -1) continue; if (!solve(i)) { out.println("NO"); return; } } for (int i = 0; i < n; i++) { if (group[i] != -1) continue; if (!solve(i)) { out.println("NO"); return; } } out.println("YES"); for (int i = 0; i < n; i++) { out.print(group[i]); out.print(" "); } out.println(); } private boolean solve(int index) { int mate = getMate(index); if (mate == -1) return false; assign(index, mate); if (getMate(index) != -1) return solve(getMate(index)); else return getMate(mate) == -1 || solve(getMate(mate)); } private void assign(int index, int mate) { int sum = p[index] + p[mate]; if (sum == a) { group[index] = group[mate] = 0; return; } if (sum == b) { group[index] = group[mate] = 1; return; } throw new RuntimeException("Wrong assignment :("); } private int getMate(int index) { int[] possibleMates = new int[] {a - p[index], b - p[index]}; for (int mate: possibleMates) { if (position.containsKey(mate)) { int mateIndex = position.get(mate); if (group[mateIndex] == -1) return mateIndex; } } return -1; } } class FastInputReader { public BufferedReader reader; public StringTokenizer tokenizer; public FastInputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
linear
468_B. Two Sets
CODEFORCES
import java.util.Map; import java.io.IOException; import java.util.InputMismatchException; import java.util.TreeMap; import java.io.PrintStream; import java.io.OutputStream; import java.util.ArrayDeque; import java.io.PrintWriter; import java.util.Deque; import java.math.BigInteger; import java.util.Queue; import java.util.Collection; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author karan173 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { int val[]; int p[]; int aneigh[], bneight[], deg[]; Deque<Integer> mycycle; boolean loops = false; public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.ni (); val = new int[n]; int a = in.ni (); int b = in.ni (); Map<Integer, Integer> set = new TreeMap<Integer, Integer> (); p = in.iArr (n); for (int i = 0; i < n; i++) { set.put (p[i], i); } aneigh = new int[n]; bneight = new int[n]; deg = new int[n]; for (int i = 0; i < n; i++) { aneigh[i] = val[i] = bneight[i] = -1; deg[i] = 0; } Queue<Integer> queue = new ArrayDeque<Integer> (); for (int i = 0; i < n; i++) { Integer x1 = set.get (a - p[i]); Integer x2 = set.get (b - p[i]); if (x1 != null) { aneigh[i] = x1; deg[i]++; } if (x2 != null && a != b) { bneight[i] = x2; deg[i]++; } if (deg[i] == 1) { queue.add (i); } } while (!queue.isEmpty ()) { int idx = queue.remove (); if (deg[idx] != 1) { continue; } int aa = aneigh[idx]; int bb = bneight[idx]; if (aa != -1) { val[idx] = val[aa] = 0; deg[aa]--; deg[idx]--; aneigh[aa] = -1; aneigh[idx] = -1; if (deg[aa] == 1) { int zz = bneight[aa]; bneight[zz] = -1; deg[zz]--; if(deg[zz] == 1) queue.add (zz); } } else { val[idx] = val[bb] = 1; deg[bb]--; deg[idx]--; bneight[idx] = bneight[bb] = -1; if (deg[bb] == 1) { //queue.add (bb); int zz = aneigh[bb]; aneigh[zz] = -1; deg[zz]--; if(deg[zz] == 1) queue.add (zz); } } } for (int i = 0; i < n; i++) { if (val[i] == -1 && cantBePaired(i)) { out.println ("NO"); return; } } //every person has two neighbours //cases => either cycles or linear path with loops end points for (int i = 0; i < n; i++) { if (val[i] == -1) { mycycle = new ArrayDeque<Integer> (); loops = false; cycle (i); if (loops || mycycle.size () % 2 == 0) { doEvenCycle (); continue; } out.println ("NO"); return; } } out.println ("YES"); for (int i = 0; i < n; i++) { out.print (val[i] + " "); } out.println (); } private boolean cantBePaired(int i) { int aa = aneigh[i]; int bb = bneight[i]; if (aa != -1 && val[aa] == -1) { return false; } if (bb != -1 && val[bb] == -1) { return false; } return true; } private void doEvenCycle() { for (int x : mycycle) { val[x] = 0; } } private void cycle(int i) { boolean aa = false; int prev = i; mycycle.addLast (i); System.out.println (i); int j = aneigh[i]; while (j != i) { if (j == prev) { loops = true; break; } mycycle.addLast (j); System.out.println (j); prev = j; j = aa ? aneigh[j] : bneight[j]; // if (j == -1) // { // System.out.println (prev + " " + aneigh[prev] + bneight[prev] + " " + deg[prev] + " " + val[prev] + " " + val[64] + " " + deg[64]); // } aa = !aa; } if (loops) { j = bneight[i]; prev = i; aa = true; while (prev != j) { mycycle.addFirst (j); prev = j; j = aa ? aneigh[j] : bneight[j]; aa = !aa; } } System.out.println ("XXX"); } } class FastReader { public InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public FastReader() { } public int read() { if (numChars == -1) { throw new InputMismatchException (); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read (buf); } catch (IOException e) { throw new InputMismatchException (); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int ni() { int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-') { sgn = -1; c = read (); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } while (!isSpaceChar (c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar (c); } return isWhitespace (c); } public static boolean isWhitespace(int c) { return c==' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] iArr(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni (); } return a; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
linear
468_B. Two Sets
CODEFORCES
import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class Main { int work(int x){ if(x%2==0)return x+1; else return x-1; } static int N = 200050; class Node implements Comparable <Node>{ int x, id; Node(int x, int id){ this.x = x; this.id = id; } public int compareTo(Node o){ return Integer.compare(x, o.x); } public String toString(){ return id + "=" + x; } } class Edge{ int from, to, nex; Edge (int from, int to, int nex){ this.from = from; this.to = to; this.nex = nex; } } Edge[] edge = new Edge[N*10]; int[] head = new int[N]; int edgenum; void addedge(int u, int v){ Edge E = new Edge(u, v, head[u]); edge[edgenum] = E; head[u] = edgenum ++; } int n; int[] p = new int[N], ans = new int[N]; int a, b, max; Map<Integer, Integer> map = new HashMap(); boolean match(int x, int y, int col){ int P = map.get(x); if(map.containsKey(y-x) == false) return false; int Q = map.get(y - x); if(ans[Q] == -1 || x * 2 == y){ ans[Q] = ans[P] = col; } else { if(match(a+b-2*y+x, y, col)) ans[Q] = ans[P] = col; else return false; } return true; } boolean solve(){ if(max >= a && max >= b)return false; for(int i = 1; i <= n; i++) if(ans[i] == -1) { if(match(p[i], a, 0)==false && match(p[i], b, 1) == false) return false; } return true; } void init(){ n = cin.nextInt(); a = cin.nextInt(); b = cin.nextInt(); max = 0; for(int i = 1; i <= n; i++){ ans[i] = -1; p[i] = cin.nextInt(); map.put(p[i], i); if(p[i] > max) max = p[i]; } } public void work(){ init(); if(solve()){ out.println("YES"); for(int i = 1; i <= n; i++)out.print(ans[i]+" "); out.println(); } else out.println("NO"); } Main() { cin = new Scanner(System.in); out = new PrintWriter(System.out); } public static void main(String[] args) { Main e = new Main(); e.work(); out.close(); } public Scanner cin; public static PrintWriter out; }
linear
468_B. Two Sets
CODEFORCES
import java.io.*; import java.util.*; public class Main { static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter writer; static int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } static long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } static double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } static String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static void main(String[] args) throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(System.out); pineapple(); reader.close(); writer.close(); } static void pineapple() throws NumberFormatException, IOException { int n = nextInt(); int a = nextInt(); int b = nextInt(); TreeSet<Integer> al = new TreeSet<Integer>(); TreeMap<Integer, Integer> mp = new TreeMap<Integer, Integer>(); int[] ans = new int[n]; Arrays.fill(ans, -1); TreeSet<Integer> used = new TreeSet<Integer>(); int[] mas = new int[n]; for (int i = 0; i < n; i++) { int t = nextInt(); al.add(t); mas[i] = t; mp.put(t, i); } for (int st : al) { if (used.contains(st)) continue; { int pr = st; int cc = -1; TreeSet<Integer> u2 = new TreeSet<Integer>(); u2.add(pr); if (al.contains(a - pr) && !u2.contains(a - pr)) cc = a - pr; else if (al.contains(b - pr) && !u2.contains(a - pr)) cc = b - pr; if (cc != -1) { u2.add(cc); boolean bGo = true; while (bGo) { bGo = false; int nxt = -1; if (al.contains(a - cc) && !u2.contains(a - cc)) nxt = a - cc; else if (al.contains(b - cc) && !u2.contains(b - cc)) nxt = b - cc; if (nxt != -1) { bGo = true; u2.add(nxt); cc = nxt; pr = cc; } } st = cc; } } int x = st; while (x != -1) { int curr = x; used.add(curr); x = -1; int next1 = a - curr; if (al.contains(next1)) { if (!used.contains(next1)) { x = next1; int ci = mp.get(curr); int ni = mp.get(next1); if (ans[ci] == -1 && ans[ni] == -1) { ans[ni] = 0; ans[ci] = 0; } } } int next2 = b - curr; if (al.contains(next2)) { if (!used.contains(next2)) { x = next2; int ci = mp.get(curr); int ni = mp.get(next2); if (ans[ci] == -1 && ans[ni] == -1) { ans[ni] = 1; ans[ci] = 1; } } } } } for (int i = 0; i < n; i++) { if (ans[i] == -1) { if (2 * mas[i] == a) { ans[i] = 0; continue; } if (2 * mas[i] == b) { ans[i] = 1; continue; } writer.println("NO"); return; } } writer.println("YES"); for (int i = 0; i < n; i++) { writer.print(ans[i] + " "); } } }
linear
468_B. Two Sets
CODEFORCES
import java.util.*; import java.util.Map.Entry; import java.io.*; public class A{ public static void main(String args[]){ FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); boolean change = false; if(a > b){ int t = a; a = b; b = t; change = true; } boolean[] inb = new boolean[n]; int[] numbers = new int[n]; TreeMap<Integer, Integer> num = new TreeMap<Integer, Integer>(); for(int i = 0; i < n; i++){ num.put(in.nextInt(), i); } boolean hasAns = true; while(!num.isEmpty()){ Entry<Integer, Integer> last = num.lastEntry(); int key = last.getKey(); if(num.containsKey(a - key)){ num.remove(key); num.remove(a - key); } else if(num.containsKey(b - key)){ inb[num.get(key)] = true; inb[num.get(b - key)] = true; num.remove(key); num.remove(b - key); } else{ hasAns = false; break; } } if(hasAns){ out.println("YES"); for(int i = 0; i < n && !change; i++){ if(inb[i]){ out.print("1"); } else{ out.print("0"); } if(i != n - 1){ out.print(" "); } } for(int i = 0; i < n && change; i++){ if(inb[i]){ out.print("0"); } else{ out.print("1"); } if(i != n - 1){ out.print(" "); } } } else{ out.println("NO"); } out.close(); } static class FastScanner{ private BufferedReader reader; private StringTokenizer tokenizer; public FastScanner(InputStream stream){ reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String nextLine(){ try{ return reader.readLine(); } catch(IOException e){ e.printStackTrace(); return null; } } public String next(){ while(tokenizer == null || !tokenizer.hasMoreTokens()){ try{ tokenizer = new StringTokenizer(reader.readLine()); } catch(IOException e){ throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt(){ return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } } }
linear
468_B. Two Sets
CODEFORCES
import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class Main { int work(int x){ if(x%2==0)return x+1; else return x-1; } static int N = 200050; class Node implements Comparable <Node>{ int x, id; Node(int x, int id){ this.x = x; this.id = id; } public int compareTo(Node o){ return Integer.compare(x, o.x); } public String toString(){ return id + "=" + x; } } class Edge{ int from, to, nex; Edge (int from, int to, int nex){ this.from = from; this.to = to; this.nex = nex; } } Edge[] edge = new Edge[N*10]; int[] head = new int[N]; int edgenum; void addedge(int u, int v){ Edge E = new Edge(u, v, head[u]); edge[edgenum] = E; head[u] = edgenum ++; } int n; int[] p = new int[N], ans = new int[N]; int a, b, max; Map<Integer, Integer> map = new HashMap(); boolean match(int x, int y, int col){ int P = map.get(x); if(map.containsKey(y-x) == false) return false; int Q = map.get(y - x); if(ans[Q] == -1 || x * 2 == y){ ans[Q] = ans[P] = col; } else { if(match(a+b-2*y+x, y, col)) ans[Q] = ans[P] = col; else return false; } return true; } boolean solve(){ if(max >= a && max >= b)return false; for(int i = 1; i <= n; i++) if(ans[i] == -1) { if(match(p[i], a, 0)==false && match(p[i], b, 1) == false) return false; } return true; } void init(){ n = cin.nextInt(); a = cin.nextInt(); b = cin.nextInt(); max = 0; for(int i = 1; i <= n; i++){ ans[i] = -1; p[i] = cin.nextInt(); map.put(p[i], i); if(p[i] > max) max = p[i]; } } public void work(){ init(); if(solve()){ out.println("YES"); for(int i = 1; i <= n; i++)out.print(ans[i]+" "); out.println(); } else out.println("NO"); } Main() { cin = new Scanner(System.in); out = new PrintWriter(System.out); } public static void main(String[] args) { Main e = new Main(); e.work(); out.close(); } public Scanner cin; public static PrintWriter out; } /* http://blog.csdn.net/keshuai19940722/article/details/39528801 */
linear
468_B. Two Sets
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.StringTokenizer; import static java.lang.Integer.*; public class BDiv1 { static int n; static int a; static int b; static HashMap<Integer,Integer> graphA=new HashMap<>(); static HashMap<Integer,Integer> graphB=new HashMap<>(); static int [] array; static int [] original; static boolean x=true; public static void main(String[] args) throws Exception{ BufferedReader buf =new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st =new StringTokenizer(buf.readLine()); n=parseInt(st.nextToken()); a=parseInt(st.nextToken()); b=parseInt(st.nextToken()); st =new StringTokenizer(buf.readLine()); array=new int[n]; original=new int [n]; for (int i=0;i<n;i++){ array[i]=parseInt(st.nextToken()); original[i]=array[i]; } Arrays.sort(array); for (int i=0;i<n;i++){ int k= Arrays.binarySearch(array,a-array[i]); if (k>=0){ graphA.put(array[i],array[k]); graphA.put(array[k],array[i]); } } for (int i=0;i<n;i++){ int k= Arrays.binarySearch(array,b-array[i]); if (k>=0){ graphB.put(array[i],array[k]); graphB.put(array[k],array[i]); } } for (int i=0;i<n;i++){ Integer j=graphA.get(array[i]); if (j!=null){ if (graphB.containsKey(array[i]) && graphB.containsKey(j)){ graphA.remove(array[i]); graphA.remove(j); } else if (graphB.containsKey(array[i]) && !graphB.containsKey(j)){ graphB.remove(graphB.get(array[i])); graphB.remove(array[i]); } else if (!graphB.containsKey(array[i]) && graphB.containsKey(j)){ graphB.remove(graphB.get(j)); graphB.remove(j); } } } int [] res=new int [n]; for (int i=0;i<n;i++){ if (graphA.containsKey(original[i]))res[i]=0; else if (graphB.containsKey(original[i])) res[i]=1; else { System.out.println("NO"); return; } } System.out.println("YES"); for (int k:res)System.out.print(k+" "); } }
linear
468_B. Two Sets
CODEFORCES
import java.io.*; import java.util.*; public class Main { // main public static void main(String [] args) throws IOException { // makes the reader and writer BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); // read in n,a,b,ints StringTokenizer st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); st = new StringTokenizer(f.readLine()); HashMap<Integer,Integer> in = new HashMap<Integer,Integer>(); int[][] locs = new int[n][2]; for (int i=0;i<n;i++) { int num = Integer.parseInt(st.nextToken()); locs[i] = new int[]{num,i}; in.put(num,i); } // use greedy boolean ok = true; int swap = 0; if (a>b) {swap = 1; int t = a; a = b; b = t; } Arrays.sort(locs,new Comparator<int[]>() { public int compare(int[] a, int[] b) { return (new Integer(a[0])).compareTo(b[0]); } }); int[] inB = new int[n]; for (int[] i: locs) { if (in.containsKey(b-i[0])) { inB[i[1]] = 1-swap; in.remove(b-i[0]); } else if (in.containsKey(a-i[0])) { inB[i[1]] = swap; in.remove(a-i[0]); } else ok = false; } // write to out StringBuffer p = new StringBuffer(); for (int i=0;i<n-1;i++) { p.append(inB[i]); p.append(" "); } p.append(inB[n-1]); if (ok) { out.println("YES"); out.println(p.toString()); } else { out.println("NO"); } // cleanup out.close(); System.exit(0); } }
linear
468_B. Two Sets
CODEFORCES
import java.io.IOException; import java.util.Arrays; import java.util.TreeMap; import java.util.InputMismatchException; import java.util.ArrayList; import java.io.OutputStream; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author George Marcus */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { ArrayList<Integer>[] G; int[] st, dr; boolean[] v; public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); int[] A = new int[N]; TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>(); for (int i = 0; i < N; i++) { A[i] = in.nextInt(); map.put(A[i], i); } G = new ArrayList[N]; for (int i = 0; i < N; i++) { G[i] = new ArrayList<Integer>(); } for (int i = 0; i < N; i++) { int val = a - A[i]; if (map.containsKey(val)) { int p = map.get(val); // if (p != i) { G[i].add(p); G[p].add(i); // } } val = b - A[i]; if (map.containsKey(val)) { int p = map.get(val); // if (p != i) { G[i].add(p); G[p].add(i); // } } } st = new int[N]; dr = new int[N]; Arrays.fill(st, -1); Arrays.fill(dr, -1); v = new boolean[N]; boolean ok = true; int match = 0; while (ok) { ok = false; Arrays.fill(v, false); for (int i = 0; i < N; i++) { if (dr[i] == -1) { if (pairup(i)) { ok = true; match++; } } } } if (match == N) { out.println("YES"); for (int i = 0; i < N; i++) { if (i > 0) { out.print(" "); } int other = dr[i]; if (A[i] == b - A[other]) { out.print(1); } else { out.print(0); } } } else { out.println("NO"); } } private boolean pairup(int node) { if (v[node]) { return false; } v[node] = true; for (int x : G[node]) { if (st[x] == -1) { st[x] = node; dr[node] = x; return true; } } for (int x : G[node]) { if (pairup(st[x])) { st[x] = node; dr[node] = x; return true; } } return false; } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(nextString()); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } }
linear
468_B. Two Sets
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class D909 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); char[] line = br.readLine().toCharArray(); int n = line.length; int l = 0; ArrayList<Node> groups = new ArrayList<>(); Node node = new Node(line[0], 1); groups.add(node); for (int i = 1; i < n; i++) { if (line[i] == groups.get(l).letter) { groups.get(l).count++; } else { node = new Node(line[i], 1); groups.add(node); l++; } } int moves = 0; ArrayList<Node> temp = new ArrayList<>(); while (groups.size() > 1) { moves++; l = groups.size(); groups.get(0).count--; groups.get(l - 1).count--; for (int i = 1; i < l - 1; i++) { groups.get(i).count -= 2; } int p; for (p = 0; p < l; p++) { if (groups.get(p).count > 0) { temp.add(groups.get(p)); break; } } int lTemp = temp.size(); for (p++; p < l; p++) { if (groups.get(p).count <= 0) { continue; } if (groups.get(p).letter == temp.get(lTemp - 1).letter) { temp.get(lTemp - 1).count += groups.get(p).count; } else { temp.add(groups.get(p)); lTemp++; } } groups.clear(); groups.addAll(temp); temp.clear(); } System.out.println(moves); } private static class Node { char letter; int count; Node(char letter, int count) { this.letter = letter; this.count = count; } } }
linear
909_D. Colorful Points
CODEFORCES
import java.util.LinkedList; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class PlayingPiano { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); List<Integer> as = new LinkedList<>(); int[] as2 = new int[n]; for (int i = 0; i < n; i++) { int a = scanner.nextInt(); as.add(a); as2[i] = a; } //System.out.println(solve(as)); System.out.println(solve2(as2)); scanner.close(); } public static String solve(List<Integer> as) { List<Integer> fingers = new LinkedList<>(); fingers.add(1); fingers.add(2); fingers.add(3); fingers.add(4); fingers.add(5); List<Integer> solution = assign(as, fingers, fingers); if (solution == null) { return "-1"; } else { StringBuilder sb = new StringBuilder(); for (int b : solution) { sb.append(b); sb.append(" "); } sb.deleteCharAt(sb.length() - 1); return sb.toString(); } } private static List<Integer> assign(List<Integer> as, List<Integer> fingers, List<Integer> allFingers) { // if fingers is empty return null if (fingers.isEmpty()) { return null; } // if as size is one then return first element in fingers if (as.size() == 1) { List<Integer> ret = new LinkedList<>(); ret.add(fingers.get(0)); return ret; } // get sublist List<Integer> subList = as.subList(1, as.size()); for (int i = 0; i < fingers.size(); i++) { // recursively call with sublist and limited list of fingers List<Integer> subFingers = new LinkedList<>(); final int j = i; if (as.get(0) < as.get(1)) { subFingers = allFingers.stream() .filter(p -> p > fingers.get(j)).collect(Collectors.toList()); } else if (as.get(0) > as.get(1)) { subFingers = allFingers.stream() .filter(p -> p < fingers.get(j)).collect(Collectors.toList()); } else { subFingers = allFingers.stream() .filter(p -> p != fingers.get(j)).collect(Collectors.toList()); } List<Integer> ret = assign(subList, subFingers, allFingers); if (ret != null) { List<Integer> solution = new LinkedList<>(); solution.add(fingers.get(i)); solution.addAll(ret); return solution; } // if return is null, then return null, else return an array } return null; } public static String solve2(int[] as) { int[] ret = new int[as.length]; if (as.length == 1) return "1"; if (as[0] < as[1]) ret[0] = 1; else if (as[0] == as[1]) ret[0] = 3; else ret[0] = 5; for (int i = 1; i < as.length - 1; i++) { if (as[i-1] < as[i] && ret[i-1] == 5) return "-1"; if (as[i-1] > as[i] && ret[i-1] == 1) return "-1"; if (as[i-1] < as[i] && as[i] < as[i+1]) { ret[i] = ret[i-1] + 1; } else if (as[i-1] == as[i] && as[i] < as[i+1]) { ret[i] = ret[i-1] == 1 ? 2 : 1; } else if (as[i-1] > as[i] && as[i] < as[i+1]) { ret[i] = 1; } else if (as[i-1] < as[i] && as[i] == as[i+1]) { ret[i] = ret[i-1] + 1; } else if (as[i-1] == as[i] && as[i] == as[i+1]) { ret[i] = ret[i-1] == 4 ? 2 : 4; } else if (as[i-1] > as[i] && as[i] == as[i+1]) { ret[i] = ret[i-1] == 2 ? 1 : 2; } else if (as[i-1] < as[i] && as[i] > as[i+1]) { ret[i] = 5; } else if (as[i-1] == as[i] && as[i] > as[i+1]) { ret[i] = ret[i-1] == 5 ? 4 : 5; } else if (as[i-1] > as[i] && as[i] > as[i+1]) { ret[i] = ret[i-1] - 1; } } if (as.length > 1) { if (as[as.length - 1] > as[as.length - 2]) { if (ret[as.length - 2] == 5) return "-1"; ret[as.length - 1] = 5; } else if (as[as.length - 1] == as[as.length - 2]) { ret[as.length - 1] = ret[as.length - 2] == 5 ? 4 : 5; } else { if (ret[as.length - 2] == 1) return "-1"; ret[as.length - 1] = 1; } } StringBuilder sb = new StringBuilder(); for (int b : ret) { sb.append(b); sb.append(" "); } sb.deleteCharAt(sb.length() - 1); return sb.toString(); } }
linear
1032_C. Playing Piano
CODEFORCES
import java.util.*; public class Main{ private static final int MAX_SIZE = 100005; public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); if(((m + 1) / 60 < a) || ((m + 1) / 60 == a && (m + 1) % 60 <= b)) { out(0, 0); System.exit(0); } for(int i = 2; i <= n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); int bb = b + 2 * m + 2; int aa = a + bb / 60; bb %= 60; if((aa < x) || (aa == x && bb <= y)) { b = b + m + 1; a = a + b / 60; b %= 60; out(a, b); System.exit(0); } a = x; b = y; } b = b + m + 1; a = a + b / 60; b = b % 60; out(a, b); } private static void out(int a, int b) { cout(a); cout(" "); cout(b); } private static void cout(Object a) { System.out.print(a); } }
linear
967_A. Mind the Gap
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class AnnoyingPresent { //UPSOLVED public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); long n = Long.parseLong(st.nextToken()) , m = Long.parseLong(st.nextToken()); long sum = 0; for(int i=0;i<m;i++){ StringTokenizer st1 = new StringTokenizer(br.readLine()); sum+= n* Long.parseLong(st1.nextToken()); Long a= Long.parseLong(st1.nextToken()); if(a < 0){ if(n % 2 == 0) sum += n*n / 4*a; else{ sum += (n/2) * (n/2+1) * a; } } else sum += (a*(n) * (n-1) / 2); } System.out.println((double)sum/n); } }
linear
1009_C. Annoying Present
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * @author Don Li */ public class SequenceTransformation { void solve() { int p = 1, n = in.nextInt(); while (n > 0) { if (n == 1) { out.print(p + " "); break; } if (n == 2) { out.print(p + " "); out.print(2 * p + " "); break; } if (n == 3) { out.print(p + " "); out.print(p + " "); out.print(3 * p + " "); break; } for (int i = 0; i < (n + 1) / 2; i++) { out.print(p + " "); } p *= 2; n /= 2; } } public static void main(String[] args) { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); new SequenceTransformation().solve(); out.close(); } static FastScanner in; static PrintWriter out; static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } }
linear
1059_C. Sequence Transformation
CODEFORCES
import java.io.*; import java.util.*; public class Main { private static void solve(InputReader in, OutputWriter out) { int n = in.nextInt(); List<List<Integer>> g = new ArrayList<>(n + 1); for (int i = 0; i < n + 1; i++) { g.add(new LinkedList<>()); } int degree1 = 0, degree2 = 0, root = 0; for (int i = 0; i < n - 1; i++) { int a = in.nextInt(); int b = in.nextInt(); g.get(a).add(b); g.get(b).add(a); if (g.get(a).size() > degree1) { if (a == root) { degree1 = g.get(a).size(); } else { degree2 = degree1; degree1 = g.get(a).size(); root = a; } } else if (g.get(a).size() > degree2) { degree2 = g.get(a).size(); } if (g.get(b).size() > degree1) { if (b == root) { degree1 = g.get(b).size(); } else { degree2 = degree1; degree1 = g.get(b).size(); root = b; } } else if (g.get(b).size() > degree2) { degree2 = g.get(b).size(); } } if (degree2 > 2) { out.print("No"); } else { out.println("Yes"); List<Integer> leaves = new LinkedList<>(); for (int i = 1; i <= n; i++) { if (i != root) { if (g.get(i).size() == 1) { leaves.add(i); } } } out.println(leaves.size()); for (int i : leaves) { out.println(root + " " + i); } } } private static void shuffleArray(int[] array) { int index; Random random = new Random(); for (int i = array.length - 1; i > 0; i--) { index = random.nextInt(i + 1); if (index != i) { array[index] ^= array[i]; array[i] ^= array[index]; array[index] ^= array[i]; } } } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); solve(in, out); in.close(); out.close(); } private static class InputReader { private BufferedReader br; private StringTokenizer st; InputReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); st = null; } String nextLine() { String line = null; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } String next() { while (st == null || !st.hasMoreTokens()) { String line = nextLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } byte nextByte() { return Byte.parseByte(next()); } short nextShort() { return Short.parseShort(next()); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } private static class OutputWriter { BufferedWriter bw; OutputWriter(OutputStream os) { bw = new BufferedWriter(new OutputStreamWriter(os)); } void print(int i) { print(Integer.toString(i)); } void println(int i) { println(Integer.toString(i)); } void print(long l) { print(Long.toString(l)); } void println(long l) { println(Long.toString(l)); } void print(double d) { print(Double.toString(d)); } void println(double d) { println(Double.toString(d)); } void print(boolean b) { print(Boolean.toString(b)); } void println(boolean b) { println(Boolean.toString(b)); } void print(char c) { try { bw.write(c); } catch (IOException e) { e.printStackTrace(); } } void println(char c) { println(Character.toString(c)); } void print(String s) { try { bw.write(s); } catch (IOException e) { e.printStackTrace(); } } void println(String s) { print(s); print('\n'); } void close() { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
linear
981_C. Useful Decomposition
CODEFORCES
import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Scanner; public class A1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); long size = scan.nextLong(); int numberOfSpecial = scan.nextInt(); long pageSize = scan.nextLong(); long[] specialItemsArray = new long[numberOfSpecial]; for (int i = 0; i < numberOfSpecial; i++) { specialItemsArray[i] = scan.nextLong(); } int totalRemoved = 0; int step = 0; long currentPageIndex = BigDecimal.valueOf(specialItemsArray[0]).divide(BigDecimal.valueOf(pageSize),2, RoundingMode.UP).setScale(0, RoundingMode.CEILING).longValue(); int specialItemArrayIndex = 1; while (specialItemArrayIndex < numberOfSpecial) { long pageIndex = BigDecimal.valueOf(specialItemsArray[specialItemArrayIndex] - totalRemoved).divide(BigDecimal.valueOf(pageSize),2,RoundingMode.UP).setScale(0, RoundingMode.CEILING).longValue(); if (currentPageIndex != pageIndex) { step++; totalRemoved = specialItemArrayIndex; currentPageIndex = BigDecimal.valueOf(specialItemsArray[specialItemArrayIndex] - totalRemoved).divide(BigDecimal.valueOf(pageSize),2,RoundingMode.UP).setScale(0, RoundingMode.CEILING).longValue(); } specialItemArrayIndex++; } System.out.println(step + 1); } }
linear
1190_A. Tokitsukaze and Discard Items
CODEFORCES
import java.util.*; import java.io.*; public class Solution{ public static long page(long p,long k){ return (p-1)/k; } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); int m = sc.nextInt(); long k = sc.nextLong(); long[] p = new long[m]; long del = 0; long nb = 1; int op = 0; for(int i=0;i<m;i++) p[i] = sc.nextLong(); for(int i=1;i<m;i++){ if(page(p[i]-del,k)!=page(p[i-1]-del,k)){ del += nb; nb = 1; op++; }else{ nb++; } } if(nb!=0) op++; System.out.println(op); } }
linear
1191_C. Tokitsukaze and Discard Items
CODEFORCES
/** * BaZ :D */ import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main { static Reader scan; static PrintWriter pw; static int n,k,left[],right[],arr[]; static long MOD = 1000000007,count[],dp[]; public static void main(String[] args) { new Thread(null,null,"BaZ",1<<25) { public void run() { try { solve(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static void solve() throws IOException { scan = new Reader(); pw = new PrintWriter(System.out,true); StringBuilder sb = new StringBuilder(); n = ni(); k = ni(); int stack[] = new int[1000001]; int top = -1; arr = new int[n]; left = new int[n]; right = new int[n]; for(int i=0;i<n;++i) { arr[i] = ni(); while(top>=0 && arr[stack[top]]<=arr[i]) top--; if(top==-1) left[i] = 0; else left[i] = stack[top]+1; stack[++top] = i; } top = -1; for(int i=n-1;i>=0;--i) { while(top>=0 && arr[stack[top]]<arr[i]) top--; if(top==-1) right[i] = n-1; else right[i] = stack[top]-1; stack[++top] = i; } //pa("left", left); //pa("right", right); dp = new long[n+1]; for(int i=0;i<=n;++i) { if(i<k) continue; dp[i] = dp[i-k+1] + (i-k+1); } count = new long[n]; long ans = 0; for(int i=0;i<n;++i) { int len = right[i]-left[i]+1; int lef = i-left[i]; int rig = right[i]-i; long count = dp[len] - dp[lef] - dp[rig]; if(count>=MOD) count%=MOD; ans += count*arr[i]; if(ans>=MOD) ans%=MOD; } pl(ans); pw.flush(); pw.close(); } static int ni() throws IOException { return scan.nextInt(); } static long nl() throws IOException { return scan.nextLong(); } static double nd() throws IOException { return scan.nextDouble(); } static void pl() { pw.println(); } static void p(Object o) { pw.print(o+" "); } static void pl(Object o) { pw.println(o); } static void psb(StringBuilder sb) { pw.print(sb); } static void pa(String arrayName, Object arr[]) { pl(arrayName+" : "); for(Object o : arr) p(o); pl(); } static void pa(String arrayName, int arr[]) { pl(arrayName+" : "); for(int o : arr) p(o); pl(); } static void pa(String arrayName, long arr[]) { pl(arrayName+" : "); for(long o : arr) p(o); pl(); } static void pa(String arrayName, double arr[]) { pl(arrayName+" : "); for(double o : arr) p(o); pl(); } static void pa(String arrayName, char arr[]) { pl(arrayName+" : "); for(char o : arr) p(o); pl(); } static void pa(String listName, List list) { pl(listName+" : "); for(Object o : list) p(o); pl(); } static void pa(String arrayName, Object[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(Object o : arr[i]) p(o); pl(); } } static void pa(String arrayName, int[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(int o : arr[i]) p(o); pl(); } } static void pa(String arrayName, long[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(long o : arr[i]) p(o); pl(); } } static void pa(String arrayName, char[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(char o : arr[i]) p(o); pl(); } } static void pa(String arrayName, double[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(double o : arr[i]) p(o); pl(); } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
linear
1037_F. Maximum Reduction
CODEFORCES
import java.util.*; import java.io.*; public class _1036_B_DiagonalWalkingV2 { public static void main(String[] args) throws IOException { int Q = readInt(); while(Q-- > 0) { long n = readLong(), m = readLong(), k = readLong(); if(Math.max(n, m) > k) println(-1); else { long ans = k; if(n%2 != k%2) ans--; if(m%2 != k%2) ans--; println(ans); } } exit(); } final private static int BUFFER_SIZE = 1 << 16; private static DataInputStream din = new DataInputStream(System.in); private static byte[] buffer = new byte[BUFFER_SIZE]; private static int bufferPointer = 0, bytesRead = 0; static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = Read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public static String read() throws IOException { byte[] ret = new byte[1024]; int idx = 0; byte c = Read(); while (c <= ' ') { c = Read(); } do { ret[idx++] = c; c = Read(); } while (c != -1 && c != ' ' && c != '\n' && c != '\r'); return new String(ret, 0, idx); } public static int readInt() throws IOException { int ret = 0; byte c = Read(); while (c <= ' ') c = Read(); boolean neg = (c == '-'); if (neg) c = Read(); do { ret = ret * 10 + c - '0'; } while ((c = Read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public static long readLong() throws IOException { long ret = 0; byte c = Read(); while (c <= ' ') c = Read(); boolean neg = (c == '-'); if (neg) c = Read(); do { ret = ret * 10 + c - '0'; } while ((c = Read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public static double readDouble() throws IOException { double ret = 0, div = 1; byte c = Read(); while (c <= ' ') c = Read(); boolean neg = (c == '-'); if (neg) c = Read(); do { ret = ret * 10 + c - '0'; } while ((c = Read()) >= '0' && c <= '9'); if (c == '.') { while ((c = Read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private static void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private static byte Read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } static void print(Object o) { pr.print(o); } static void println(Object o) { pr.println(o); } static void flush() { pr.flush(); } static void println() { pr.println(); } static void exit() throws IOException { din.close(); pr.close(); System.exit(0); } }
linear
1036_B. Diagonal Walking v.2
CODEFORCES
//package com.krakn.CF.D1159; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n, k; n = sc.nextInt(); k = sc.nextInt(); int a = (n - k) / 2; StringBuilder s = new StringBuilder(); int i; while (s.length() < n) { i = 0; while (i < a && s.length() < n) { s.append("0"); i++; } if (s.length() < n) s.append("1"); } System.out.println(s); } }
linear
1159_D. The minimal unique substring
CODEFORCES
import java.io.*; import java.util.*; public class Main { private static void solve(InputReader in, OutputWriter out) { int n = in.nextInt(); if (n < 6) { out.println(-1); } else { int m = (n - 2); for (int i = 2; i <= m; i++) { out.println("1 " + i); } out.println(m + " " + (m + 1)); out.println(m + " " + (m + 2)); } for (int i = 2; i <= n; i++) { out.println("1 " + i); } } private static void shuffleArray(int[] array) { int index; Random random = new Random(); for (int i = array.length - 1; i > 0; i--) { index = random.nextInt(i + 1); if (index != i) { array[index] ^= array[i]; array[i] ^= array[index]; array[index] ^= array[i]; } } } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); solve(in, out); in.close(); out.close(); } private static class InputReader { private BufferedReader br; private StringTokenizer st; InputReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); st = null; } String nextLine() { String line = null; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } String next() { while (st == null || !st.hasMoreTokens()) { String line = nextLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } byte nextByte() { return Byte.parseByte(next()); } short nextShort() { return Short.parseShort(next()); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } private static class OutputWriter { BufferedWriter bw; OutputWriter(OutputStream os) { bw = new BufferedWriter(new OutputStreamWriter(os)); } void print(int i) { print(Integer.toString(i)); } void println(int i) { println(Integer.toString(i)); } void print(long l) { print(Long.toString(l)); } void println(long l) { println(Long.toString(l)); } void print(double d) { print(Double.toString(d)); } void println(double d) { println(Double.toString(d)); } void print(boolean b) { print(Boolean.toString(b)); } void println(boolean b) { println(Boolean.toString(b)); } void print(char c) { try { bw.write(c); } catch (IOException e) { e.printStackTrace(); } } void println(char c) { println(Character.toString(c)); } void print(String s) { try { bw.write(s); } catch (IOException e) { e.printStackTrace(); } } void println(String s) { print(s); print('\n'); } void close() { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
linear
959_C. Mahmoud and Ehab and the wrong algorithm
CODEFORCES
import java.io.*; import java.util.*; import java.lang.*; import java.awt.*; import java.awt.geom.*; import java.math.*; import java.text.*; import java.math.BigInteger.*; import java.util.Arrays; public class CF111111 { BufferedReader in; StringTokenizer as; int nums[],nums2[]; int[] nums1[]; boolean con = true; ArrayList < Integer > ar = new ArrayList < Integer >(); ArrayList < Integer > fi = new ArrayList < Integer >(); Map<Integer,Integer > map = new HashMap<Integer, Integer>(); public static void main (String[] args) { new CF111111 (); } public int GCD(int a, int b) { if (b==0) return a; return GCD(b,a%b); } public int LIS(int arr[]) { int n = arr.length; int sun[] = new int [n]; int cur = 0; for(int x = 0;x<n;x++) { int temp = Arrays.binarySearch(sun,0,cur,arr[x]); if(temp < 0) temp = -temp -1; sun[temp] = arr[x]; if(temp == cur) cur++; } return cur; } public CF111111 () { try { in = new BufferedReader (new InputStreamReader (System.in)); int a = nextInt(); for(int xx1 = 0;xx1<a;xx1++) { int b = nextInt(); nums = new int [b]; for(int x = 0;x<b;x++) { nums[x] = nextInt(); } int max = 0; int max2 = -1; for(int x = 0;x<b;x++) { if(nums[x] >= max) { max2 = max; max = nums[x]; } else if(nums[x] >= max2) max2 = nums[x]; } System.out.println(Math.min(max2, b-1)-1); } } catch(IOException e) { } } String next () throws IOException { while (as == null || !as.hasMoreTokens ()) { as = new StringTokenizer (in.readLine ().trim ()); } return as.nextToken (); } long nextLong () throws IOException { return Long.parseLong (next ()); } int nextInt () throws IOException { return Integer.parseInt (next ()); } double nextDouble () throws IOException { return Double.parseDouble (next ()); } String nextLine () throws IOException { return in.readLine ().trim (); } }
linear
1197_A. DIY Wooden Ladder
CODEFORCES
import java.util.*; import java.io.*; import java.math.*; public class Solution{ public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringTokenizer st; for(int z=0;z<t;z++){ st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int min=1; int max=1; for(int i=0;i<n;i++){ int k = Integer.parseInt(st.nextToken()); if(max<k){ min = max; max = k; }else if(min<k){ min = k; } } int res = Math.min(n-2,min-1); System.out.println(res); } } }
linear
1197_A. DIY Wooden Ladder
CODEFORCES
//package com.krakn.CF.B1159; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n; n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int min = 1000000000, temp; for (int i = 0; i < n; i++) { temp = arr[i] / Math.max(i, n - 1 - i); if (temp < min) min = temp; // System.out.println(i + " " + temp); } System.out.println(min); } }
linear
1159_B. Expansion coefficient of the array
CODEFORCES
import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); /*int n = stdin.nextInt(); for(int i = 0; i < n; i++) { test(stdin); }*/ test(stdin); stdin.close(); } public static void test(Scanner stdin) { int n = stdin.nextInt(); int min = Integer.MAX_VALUE; for(int i = 0; i < n; i++) { int a = stdin.nextInt(); if((int)((1.0)*a/(Math.max(i, n - i - 1))) < min) { min = (int)((1.0)*a/(Math.max(i, n - i - 1))); } } System.out.println(min); } }
linear
1159_B. Expansion coefficient of the array
CODEFORCES
// practice with rainboy import java.io.*; import java.util.*; public class CF903F { static final int INF = 0x3f3f3f3f; static void fill(int[][][][] aa, int a) { for (int h0 = 0; h0 <= 4; h0++) for (int h1 = 0; h1 <= 4; h1++) for (int h2 = 0; h2 <= 4; h2++) for (int h3 = 0; h3 <= 4; h3++) aa[h0][h1][h2][h3] = a; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int a1 = Integer.parseInt(st.nextToken()); int a2 = Integer.parseInt(st.nextToken()); int a3 = Integer.parseInt(st.nextToken()); int a4 = Integer.parseInt(st.nextToken()); int[] aa = new int[10]; aa[0] = aa[1] = aa[2] = aa[3] = a1; aa[4] = aa[5] = aa[6] = a2; aa[7] = aa[8] = a3; aa[9] = a4; int[][] ww = new int[10][4]; ww[0][0] = 1; ww[1][1] = 1; ww[2][2] = 1; ww[3][3] = 1; ww[4][0] = ww[4][1] = 2; ww[5][1] = ww[5][2] = 2; ww[6][2] = ww[6][3] = 2; ww[7][0] = ww[7][1] = ww[7][2] = 3; ww[8][1] = ww[8][2] = ww[8][3] = 3; ww[9][0] = ww[9][1] = ww[9][2] = ww[9][3] = 4; char[][] cc = new char[4][n + 8]; for (int k = 0; k < 4; k++) { char[] c_ = cc[k]; br.readLine().getChars(0, n, c_, 4); c_[0] = c_[1] = c_[2] = c_[3] = c_[n + 4] = c_[n + 5] = c_[n + 6] = c_[n + 7] = '.'; } int[][][][] dp = new int[5][5][5][5]; int[][][][] dq = new int[5][5][5][5]; fill(dp, INF); dp[4][4][4][4] = 0; int[] hh = new int[4]; for (int i = 0; i < n + 4; i++) { for (int h0 = 0; h0 <= 4; h0++) for (int h1 = 0; h1 <= 4; h1++) for (int h2 = 0; h2 <= 4; h2++) for (int h3 = 0; h3 <= 4; h3++) for (int s = 0; s < 10; s++) { hh[0] = h0; hh[1] = h1; hh[2] = h2; hh[3] = h3; for (int k = 0; k < 4; k++) { int h = ww[s][k]; if (hh[k] < h) { while (h < 4 && cc[k][i + h] == '.') h++; hh[k] = h; } } int x = dp[h0][h1][h2][h3] + aa[s]; if (dp[hh[0]][hh[1]][hh[2]][hh[3]] > x) dp[hh[0]][hh[1]][hh[2]][hh[3]] = x; } fill(dq, INF); for (int h0 = 1; h0 <= 4; h0++) { hh[0] = h0 < 4 || cc[0][i + 4] == '*' ? h0 - 1 : 4; for (int h1 = 1; h1 <= 4; h1++) { hh[1] = h1 < 4 || cc[1][i + 4] == '*' ? h1 - 1 : 4; for (int h2 = 1; h2 <= 4; h2++) { hh[2] = h2 < 4 || cc[2][i + 4] == '*' ? h2 - 1 : 4; for (int h3 = 1; h3 <= 4; h3++) { hh[3] = h3 < 4 || cc[3][i + 4] == '*' ? h3 - 1 : 4; int x = dp[h0][h1][h2][h3]; if (dq[hh[0]][hh[1]][hh[2]][hh[3]] > x) dq[hh[0]][hh[1]][hh[2]][hh[3]] = x; } } } } int[][][][] tmp = dp; dp = dq; dq = tmp; } System.out.println(dp[4][4][4][4]); } }
linear
903_F. Clear The Matrix
CODEFORCES
import java.io.*; import java.util.*; public class GFG { public static void main (String[] args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int ans = 0; int t= sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ int nn = sc.nextInt(); ans+=a; if(b<c){ ans += (t-nn) * (c - b); } } System.out.println(ans); } }
linear
964_B. Messages
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Cf1017A { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int result = 1; int thomasSum = 0; StringTokenizer stk; stk = new StringTokenizer(br.readLine()); int first = Integer.parseInt(stk.nextToken()); int second = Integer.parseInt(stk.nextToken()); int third = Integer.parseInt(stk.nextToken()); int fourth = Integer.parseInt(stk.nextToken()); thomasSum = first + second + third + fourth; int tmp; for (int i = 1; i < n; i++) { stk = new StringTokenizer(br.readLine()); first = Integer.parseInt(stk.nextToken()); second = Integer.parseInt(stk.nextToken()); third = Integer.parseInt(stk.nextToken()); fourth = Integer.parseInt(stk.nextToken()); tmp = first + second + third + fourth; if (tmp > thomasSum) result++; } System.out.println(result); } }
linear
1017_A. The Rank
CODEFORCES
import java.util.*; public class helloWorld { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[] ar = new int[200]; String str = in.next(); for(int i = 0; i < str.length(); i++) ar[ str.charAt(i) ]++; int ans = 100000; for(int i = 'A'; i < 'A' + m; i++) ans = Math.min(ans, ar[i]); ans *= m; System.out.println(ans); in.close(); } }
linear
1038_A. Equality
CODEFORCES
import java.util.Scanner; public class TreasureHunt { public static String Solve() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); String kuro = sc.nextLine(), shiro = sc.nextLine(), katie = sc.nextLine(); sc.close(); String[] output = {"Kuro", "Shiro", "Katie", "Draw"}; if(n >= kuro.length()) return output[3]; int[] maxArr = new int[3]; int[][] freq = new int[3][58]; for(int i = 0; i < kuro.length(); i++) { maxArr[0] = ++freq[0][kuro.charAt(i) - 65] > maxArr[0]? freq[0][kuro.charAt(i) - 65] : maxArr[0]; maxArr[1] = ++freq[1][shiro.charAt(i) - 65] > maxArr[1]? freq[1][shiro.charAt(i) - 65] : maxArr[1]; maxArr[2] = ++freq[2][katie.charAt(i) - 65] > maxArr[2]? freq[2][katie.charAt(i) - 65] : maxArr[2]; } int winner = 0, max = 0; for(int i = 0; i < 3; i++) { if(kuro.length() - maxArr[i] >= n) maxArr[i] += n; else maxArr[i] = n == 1? kuro.length() - 1: kuro.length(); if(max < maxArr[i]) { winner = i; max = maxArr[i]; } else if(max == maxArr[i]) winner = 3; } return output[winner]; } public static void main(String[] args) { System.out.println(Solve()); } }
linear
979_B. Treasure Hunt
CODEFORCES
import java.io.*; import java.util.*; import java.lang.*; import static java.lang.Math.*; // _ h _ r _ t r _ // _ t _ t _ s t _ public class TaskA implements Runnable { long m = (int)1e9+7; PrintWriter w; InputReader c; final int MAXN = (int)1e6 + 100; public void run() { c = new InputReader(System.in); w = new PrintWriter(System.out); int n = c.nextInt(), hamming_distance = 0; char[] s = c.next().toCharArray(), t = c.next().toCharArray(); HashMap<Character, HashSet<Character>> replace = new HashMap<>(); HashMap<Character, Integer> map = new HashMap<>(); for(int i=0;i<n;++i) if(s[i] != t[i]) { HashSet<Character> temp; if(replace.containsKey(s[i])){ temp = replace.get(s[i]); temp.add(t[i]); } else { temp = new HashSet<>(); temp.add(t[i]); } map.put(s[i],i); replace.put(s[i], temp); hamming_distance++; } int l = -1, r = -1; boolean global_check = false; for(int i=0;i<n;i++) if(s[i] != t[i]) { if(replace.containsKey(t[i])) { HashSet<Character> indices = replace.get(t[i]); int ind = map.get(t[i]); l = i + 1; r = ind + 1; if (indices.contains(s[i])) { hamming_distance -= 2; global_check = true; break; } } if(global_check) break; } if(!global_check && l!=-1) hamming_distance--; else if(global_check){ for(int i=0;i<n;i++) { if(t[i] == s[l-1] && s[i] == t[l-1]){ r = i + 1; break; } } } w.println(hamming_distance); w.println(l+" "+r); w.close(); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static void sortbyColumn(int arr[][], int col){ Arrays.sort(arr, new Comparator<int[]>() { public int compare(int[] o1, int[] o2){ return(Integer.valueOf(o1[col]).compareTo(o2[col])); } }); } public static class DJSet { public int[] upper; public DJSet(int n) { upper = new int[n]; Arrays.fill(upper, -1); } public int root(int x) { return upper[x] < 0 ? x : (upper[x] = root(upper[x])); } public boolean equiv(int x, int y) { return root(x) == root(y); } public boolean union(int x, int y) { x = root(x); y = root(y); if (x != y) { if (upper[y] < upper[x]) { int d = x; x = y; y = d; } upper[x] += upper[y]; upper[y] = x; } return x == y; } } public static int[] radixSort(int[] f) { int[] to = new int[f.length]; { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]&0xffff]++] = f[i]; int[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]>>>16)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]>>>16]++] = f[i]; int[] d = f; f = to;to = d; } return f; } public void printArray(int[] a){ for(int i=0;i<a.length;i++) w.print(a[i]+" "); w.println(); } public int[] scanArrayI(int n){ int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = c.nextInt(); return a; } public long[] scanArrayL(int n){ long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = c.nextLong(); return a; } public void printArray(long[] a){ for(int i=0;i<a.length;i++) w.print(a[i]+" "); w.println(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new TaskA(),"TaskA",1<<26).start(); } }
linear
527_B. Error Correct System
CODEFORCES
import java.util.*; import java.io.*; import java.math.*; import java.util.HashMap; public class Main { static class Reader { private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;} public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];} public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;} public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();} public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;} public int i(){int c = read() ;while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }int res = 0;do{if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read() ;}while(!isSpaceChar(c));return res * sgn;} public double d() throws IOException {return Double.parseDouble(s()) ;} public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String args[]) { Reader sc=new Reader(); PrintWriter out=new PrintWriter(System.out); int n=sc.i(); String s1=sc.s(); String s2=sc.s(); int pos1=-1; int pos2=-1; int arr[][][]=new int[100][100][2]; for(int i=0;i<n;i++) { if(s1.charAt(i)!=s2.charAt(i)) { if(arr[s2.charAt(i)-97][s1.charAt(i)-97][0]==1) { pos2=i; pos1=arr[s2.charAt(i)-97][s1.charAt(i)-97][1]; break; } arr[s1.charAt(i)-97][s2.charAt(i)-97][0]=1; arr[s1.charAt(i)-97][s2.charAt(i)-97][1]=i; } } int ham=0; for(int i=0;i<n;i++) { if(s1.charAt(i)!=s2.charAt(i)) ham++; } if(pos1!=-1&&pos2!=-1) { System.out.println(ham-2); System.out.println(pos1+1+" "+(pos2+1)); System.exit(0); } int arr1[][]=new int[100][2]; int arr2[][]=new int[100][2]; for(int i=0;i<n;i++) { if(s1.charAt(i)!=s2.charAt(i)) { if(arr1[s1.charAt(i)-97][0]==1) { pos2=i; pos1=arr1[s1.charAt(i)-97][1]; break; } if(arr2[s2.charAt(i)-97][0]==1) { pos2=i; pos1=arr2[s2.charAt(i)-97][1]; break; } arr1[s2.charAt(i)-97][0]=1; arr1[s2.charAt(i)-97][1]=i; arr2[s1.charAt(i)-97][0]=1; arr2[s1.charAt(i)-97][1]=i; } } if(pos1!=-1&&pos2!=-1) { System.out.println(ham-1); System.out.println(pos1+1+" "+(pos2+1)); System.exit(0); } System.out.println(ham); System.out.println(pos1+" "+pos2); } }
linear
527_B. Error Correct System
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.InputMismatchException; public class Solution1 implements Runnable { static final long MAX = 1000000007L; static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Solution1(),"Solution1",1<<26).start(); } long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } // method to return LCM of two numbers long lcm(long a, long b) { return (a*b)/gcd(a, b); } int root(int a){ while(arr[a] != a){ arr[a] = arr[arr[a]]; a = arr[a]; } return a; } void union(int a,int b){ int xroot = root(a); int yroot = root(b); if(arr[xroot] < arr[yroot]){ arr[xroot] = yroot; }else{ arr[yroot] = xroot; } } boolean find(int a,int b){ int roota = root(a); int rootb = root(b); if(roota == rootb){ return true; }else{ return false; } } int[] arr; final int level = 20; public void run() { InputReader sc= new InputReader(System.in); PrintWriter w= new PrintWriter(System.out); int n = sc.nextInt(); char[] ch = new char[n]; char[] ch2 = new char[n]; ch = sc.next().toCharArray(); ch2 = sc.next().toCharArray(); HashSet<Integer> hset[] = new HashSet[26]; for(int i = 0;i < 26;i++){ hset[i] =new HashSet(); } int count = 0; for(int i = 0;i < ch.length;i++){ if(ch[i] != ch2[i]){ hset[ch[i]-97].add(ch2[i]-97); count++; } } boolean flag = false; int swap1 = -1; int swap2 = -1; int rem = -1; for(int i = 0;i < ch.length;i++){ if(ch[i] != ch2[i]){ if(hset[ch2[i]-97].size() != 0){ swap1 = i; flag = true; if(hset[ch2[i]-97].contains(ch[i]-97)){ rem = i; count-=2; flag = false; break; } } } } if(flag){ count--; w.println(count); for(int i = 0;i < n;i++){ if(i != swap1 && ch[i] == ch2[swap1] && ch[i] != ch2[i]){ w.println((swap1+1) + " " + (i+1)); w.close(); System.exit(0); } } }else{ if(rem == -1){ w.println(count); w.println("-1 -1"); }else{ w.println(count); for(int i = 0;i < n;i++){ if(i != rem && ch[i] == ch2[rem] && ch[rem] == ch2[i] && ch[i] != ch2[i]){ w.println((rem+1) + " " + (i+1)); w.close(); System.exit(0); } } } } w.close(); } boolean fun(long[] prefix,long mid,long temp,long[] arr){ if(temp >= prefix[(int)mid]){ return true; } return false; } static class Pair implements Comparable<Pair>{ int x; int y; Pair(){} Pair(int x,int y){ this.x = x; this.y = y; } public int compareTo(Pair p){ return Long.compare(this.x,p.x); } } }
linear
527_B. Error Correct System
CODEFORCES
import java.util.*; public class ErrorCorrectSystem { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); String a = scan.next(); String b = scan.next(); int[][] mismatch = new int[26][26]; for(int i = 0; i < 26; i++) Arrays.fill(mismatch[i], -1); int[][] pair = new int[2][26]; for(int i = 0; i < 2; i++) Arrays.fill(pair[i], -1); int hd = 0; for(int i = 0; i < n; i++) { if(a.charAt(i) != b.charAt(i)) { hd++; mismatch[a.charAt(i)-'a'][b.charAt(i)-'a'] = i; pair[0][a.charAt(i)-'a'] = i; pair[1][b.charAt(i)-'a'] = i; } } for(int i = 0; i < 26; i++) { for(int j = i+1; j < 26; j++) { if(mismatch[i][j] > -1 && mismatch[j][i] > -1) { System.out.println(hd-2); System.out.println((mismatch[i][j]+1)+" "+(mismatch[j][i]+1)); return; } } } for(int i = 0; i < n; i++) { if(a.charAt(i) != b.charAt(i)) { //try a gets b's letter if(pair[0][b.charAt(i)-'a'] > -1) { System.out.println(hd-1); System.out.println((i+1)+" "+(pair[0][b.charAt(i)-'a']+1)); return; } } } System.out.println(hd); System.out.println("-1 -1"); } }
linear
527_B. Error Correct System
CODEFORCES
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; public class C { public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); // Scanner scan = new Scanner(System.in); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(bf.readLine()); int counter = 0; for(int i=0; i<2*n/3; i++) System.out.println("0 " + i); for(int i=0; i<n-2*n/3; i++) System.out.println("3 " + (2*i+1)); } }
linear
1067_C. Knights
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates 744444444747477777774 44444447474747777777 */ /** * * @author Andy Phan */ public class b { public static void main(String[] args) { FS in = new FS(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); Integer[] arr = new Integer[n]; int numZ = 0; for(int i = 0; i < n; i++) { arr[i] = in.nextInt(); if(arr[i] == 0) numZ++; } Arrays.sort(arr); if(numZ > 1) { System.out.println("cslnb"); return; } int numDup = 0; int[] arr2 = new int[n]; for(int i = 0; i < n; i++) { arr2[i] = arr[i]; if(i != 0) { if(arr2[i] == arr2[i-1]) { arr2[i-1]--; numDup++; } } } if(numDup > 1) { System.out.println("cslnb"); return; } for(int i = 0; i < n; i++) { if(i != 0) { if(arr2[i] == arr2[i-1]) { System.out.println("cslnb"); return; } } } long num = 0; if(numDup == 1) num++; for(int i = 0; i < n; i++) { num += arr2[i]-i; } if(num%2 == 0) { System.out.println("cslnb"); } else { System.out.println("sjfnb"); } out.close(); } static class FS { BufferedReader in; StringTokenizer token; public FS(InputStream str) { in = new BufferedReader(new InputStreamReader(str)); } public String next() { if (token == null || !token.hasMoreElements()) { try { token = new StringTokenizer(in.readLine()); } catch (IOException ex) { } return next(); } return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
linear
1191_D. Tokitsukaze, CSL and Stone Game
CODEFORCES
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class temp { void solve() { FastReader sc =new FastReader(); int n = sc.nextInt(); ArrayList<String> a[] = new ArrayList[5]; for(int i=0;i<=4;i++) a[i] = new ArrayList<>(); for(int i=0;i<n;i++) { String s = sc.next(); a[s.length()].add(s); } int ans = 0; for(int i=0;i<n;i++) { String s = sc.next(); if(a[s.length()].contains(s)) a[s.length()].remove(new String(s)); } for(int i=1;i<=4;i++) ans+=a[i].size(); System.out.println(ans); } public static void main(String[] args) { new temp().solve(); } 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 (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.util.*; import java.util.Map.Entry; public class Main { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private PrintWriter pw; private long mod = 1000000000 + 7; private StringBuilder ans_sb; private int size = 1000005; private long[] fact; private void soln() { int n = nextInt(); HashMap<String, Integer>[] s1 = new HashMap[4]; for(int i=0;i<=3;i++) { s1[i] = new HashMap<>(); } int cnt = 0; for(int i=0;i<n;i++) { String s = nextLine(); if(s1[s.length()-1].containsKey(s)) { s1[s.length()-1].put(s, s1[s.length()-1].get(s)+1); }else s1[s.length()-1].put(s, 1); } for(int i=0;i<n;i++) { String s = nextLine(); if(s1[s.length()-1].containsKey(s)) { s1[s.length()-1].put(s, s1[s.length()-1].get(s)-1); if(s1[s.length()-1].get(s) == 0) s1[s.length()-1].remove(s); }else { cnt++; } } pw.println(cnt); } private class Pair implements Comparable<Pair>{ long x, y; int i; public Pair(long a,long b,int c) { x = a; y = b; i = c; } @Override public int compareTo( Pair o) { return Long.compare(this.x, o.x); } public String toString() { return ""+i; } } private void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } private long pow(long a, long b, long c) { if (b == 0) return 1; long p = pow(a, b / 2, c); p = (p * p) % c; return (b % 2 == 0) ? p : (a * p) % c; } private long gcd(long n, long l) { if (l == 0) return n; return gcd(l, n % l); } public static void main(String[] args) throws Exception { new Thread(null, new Runnable() { @Override public void run() { new Main().solve(); } }, "1", 1 << 26).start(); //new Main().solve(); } public StringBuilder solve() { InputReader(System.in); /* * try { InputReader(new FileInputStream("C:\\Users\\hardik\\Desktop\\in.txt")); * } catch(FileNotFoundException e) {} */ pw = new PrintWriter(System.out); // ans_sb = new StringBuilder(); soln(); pw.close(); // System.out.println(ans_sb); return ans_sb; } public void InputReader(InputStream stream1) { stream = stream1; } private boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } private int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } private int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private String nextToken() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } private int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } private long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private void pArray(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); return; } private void pArray(long[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); return; } private boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } private char nextChar() { int c = read(); while (isSpaceChar(c)) c = read(); char c1 = (char) c; while (!isSpaceChar(c)) c = read(); return c1; } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Map; import java.util.HashMap; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author bacali */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); ACodehorsesTShirts solver = new ACodehorsesTShirts(); solver.solve(1, in, out); out.close(); } static class ACodehorsesTShirts { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(); Map<String, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { String ch = in.next(); if (map.containsKey(ch)) { map.put(ch, map.get(ch) + 1); } else { map.put(ch, 1); } } int s = n; for (int i = 0; i < n; i++) { String ch = in.next(); if (map.containsKey(ch)) { if (map.get(ch) == 1) { map.remove(ch); } else { map.put(ch, map.get(ch) - 1); } s--; } } out.println(s); } } static class FastScanner { private BufferedReader br; private StringTokenizer st; public FastScanner(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main { static BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer tok; static boolean hasNext() { while(tok==null||!tok.hasMoreTokens()) try{ tok=new StringTokenizer(in.readLine()); } catch(Exception e){ return false; } return true; } static String next() { hasNext(); return tok.nextToken(); } static long nextLong() { return Long.parseLong(next()); } static int nextInt() { return Integer.parseInt(next()); } static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) { int n = nextInt(); int a[] = new int[9]; int b[] = new int[9]; for(int i=0;i<n;i++){ String s = next(); if(s.equals("M")){ a[0]++; }else if (s.equals("L")){ a[1]++; } else if (s.equals("XL")){ a[2]++; } else if (s.equals("XXL")){ a[3]++; } else if (s.equals("XXXL")){ a[4]++; } else if (s.equals("S")){ a[5]++; } else if (s.equals("XS")){ a[6]++; } else if (s.equals("XXS")){ a[7]++; } else if (s.equals("XXXS")){ a[8]++; } } for(int i=0;i<n;i++){ String s = next(); if(s.equals("M")){ b[0]++; }else if (s.equals("L")){ b[1]++; } else if (s.equals("XL")){ b[2]++; } else if (s.equals("XXL")){ b[3]++; } else if (s.equals("XXXL")){ b[4]++; } else if (s.equals("S")){ b[5]++; } else if (s.equals("XS")){ b[6]++; } else if (s.equals("XXS")){ b[7]++; } else if (s.equals("XXXS")){ b[8]++; } } int ans = 0; ans+=Math.abs(a[2]-b[2]); ans+=Math.abs(a[3]-b[3]); ans+=Math.abs(a[4]-b[4]); int max = Math.abs(a[0]-b[0]); max = max(max,Math.abs(a[1]-b[1])); max = max(max,Math.abs(a[5]-b[5])); ans+=max; out.print(ans); out.flush(); } public static int max(int a,int b){ return a>b?a:b; } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.*; public class hackerearth { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); HashMap<String,Integer> map=new HashMap<>(); HashMap<String,Integer> map2=new HashMap<>(); for(int i=0;i<n;i++) { String s=sc.next(); if(map.containsKey(s)) map.put(s,map.get(s)+1); else map.put(s,1); } for(int i=0;i<n;i++) { String s=sc.next(); if(map2.containsKey(s)) map2.put(s,map2.get(s)+1); else map2.put(s,1); if(map.containsKey(s)) { int feq = map.get(s); feq--; if (feq <= 0) map.remove(s); else map.put(s, feq); } } int ans=0; for(Map.Entry<String,Integer> entry:map.entrySet()) { ans+=entry.getValue(); } System.out.println(ans); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.InputMismatchException; public class E46A { public static void main(String[] args) { FastScanner in = new FastScanner(System.in); String[] sizes = {"XXXS", "XXS", "XS", "S", "M", "L", "XL", "XXL", "XXXL"}; int n = in.nextInt(); HashMap<String, Integer> a = new HashMap<>(); HashMap<String, Integer> b = new HashMap<>(); for (String s : sizes) { a.put(s, 0); b.put(s, 0); } for (int i = 0; i < n; i++) { String s = in.next(); a.put(s, a.get(s) + 1); } for (int i = 0; i < n; i++) { String s = in.next(); b.put(s, b.get(s) + 1); } for (String s : sizes) { int cut = Math.min(a.get(s), b.get(s)); a.put(s, a.get(s) - cut); b.put(s, b.get(s) - cut); } int changes = 0; for (String s : sizes) changes += a.get(s); System.out.println(changes); } /** * Source: Matt Fontaine */ public static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.*; import java.io.*; // Solution public class Main { public static void main (String[] argv) { new Main(); } boolean test = false; public Main() { FastReader in = new FastReader(new BufferedReader(new InputStreamReader(System.in))); //FastReader in = new FastReader(new BufferedReader(new FileReader("Main.in"))); int n = in.nextInt(); int nM = 0; int[] nS = new int[4]; int[] nL = new int[4]; for (int i = 0; i < n; i++) { String s = in.next(); int ns = s.length(); if (s.charAt(0) == 'M') nM++; else if (s.charAt(ns - 1) == 'S') nS[ns-1]++; else nL[ns-1]++; } int c = 0; int[] nSr = new int[4]; int[] nLr = new int[4]; int nMr = 0; for (int i = 0; i < n; i++) { String s = in.next(); int ns = s.length(); if (s.charAt(0) == 'M') { if (nM > 0) --nM; else ++nMr; }else if (s.charAt(ns - 1) == 'S') { if (nS[ns-1] > 0) --nS[ns-1]; else ++nSr[ns-1]; }else { if (nL[ns-1] > 0) --nL[ns-1]; else ++nLr[ns-1]; } } for (int i = 0; i < 4; i++) c += nS[i] + nL[i]; c += nM; System.out.println(c); } private int nBit1(int v) { int v0 = v; int c = 0; while (v != 0) { ++c; v = v & (v - 1); } return c; } private int common(int v) { int c = 0; while (v != 1) { v = (v >>> 1); ++c; } return c; } private void reverse(char[] a, int i, int j) { while (i < j) { swap(a, i++, j--); } } private void swap(char[] a, int i, int j) { char t = a[i]; a[i] = a[j]; a[j] = t; } private long gcd(long x, long y) { if (y == 0) return x; return gcd(y, x % y); } private int max(int a, int b) { return a > b ? a : b; } private int min(int a, int b) { return a > b ? b : a; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(BufferedReader in) { br = in; } String next() { while (st == null || !st.hasMoreElements()) { try { String line = br.readLine(); if (line == null || line.length() == 0) return ""; st = new StringTokenizer(line); } catch (IOException e) { return ""; //e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { return ""; //e.printStackTrace(); } return str; } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.ArrayList; import java.util.Scanner; import java.lang.StringBuilder; import java.util.Arrays; import java.util.Stack; import java.util.TreeMap; public class Test11 { public static void main(String[] Args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s0 = sc.nextLine(); int s = 0; // S int m = 0; int l = 0; int s1 = 0; int l1 = 0; int s2 = 0; int l2 = 0; int s3 = 0; int l3 = 0; for (int i = 0; i < n; i++) { s0 = sc.nextLine(); if (s0.charAt(0) == 'S') s++; if (s0.charAt(0) == 'M') m++; if (s0.charAt(0) == 'L') l++; if (s0.charAt(0) == 'X' && s0.length() == 2 && s0.charAt(s0.length() - 1) == 'S') s1++; if (s0.charAt(0) == 'X' && s0.length() == 3 && s0.charAt(s0.length() - 1) == 'S') s2++; if (s0.charAt(0) == 'X' && s0.length() == 4 && s0.charAt(s0.length() - 1) == 'S') s3++; if (s0.charAt(0) == 'X' && s0.length() == 2 && s0.charAt(s0.length() - 1) == 'L') l1++; if (s0.charAt(0) == 'X' && s0.length() == 3 && s0.charAt(s0.length() - 1) == 'L') l2++; if (s0.charAt(0) == 'X' && s0.length() == 4 && s0.charAt(s0.length() - 1) == 'L') l3++; } int rs = 0; // S int rm = 0; int rl = 0; int rs1 = 0; int rl1 = 0; int rs2 = 0; int rl2 = 0; int rs3 = 0; int rl3 = 0; for (int i = 0; i < n; i++) { s0 = sc.nextLine(); if (s0.charAt(0) == 'S') rs++; if (s0.charAt(0) == 'M') rm++; if (s0.charAt(0) == 'L') rl++; if (s0.charAt(0) == 'X' && s0.length() == 2 && s0.charAt(s0.length() - 1) == 'S') rs1++; if (s0.charAt(0) == 'X' && s0.length() == 3 && s0.charAt(s0.length() - 1) == 'S') rs2++; if (s0.charAt(0) == 'X' && s0.length() == 4 && s0.charAt(s0.length() - 1) == 'S') rs3++; if (s0.charAt(0) == 'X' && s0.length() == 2 && s0.charAt(s0.length() - 1) == 'L') rl1++; if (s0.charAt(0) == 'X' && s0.length() == 3 && s0.charAt(s0.length() - 1) == 'L') rl2++; if (s0.charAt(0) == 'X' && s0.length() == 4 && s0.charAt(s0.length() - 1) == 'L') rl3++; } int ans = Math.abs(s1 - rs1) + Math.abs(s2 - rs2) + Math.abs(s3 - rs3) + (Math.abs(s - rs) + Math.abs(l - rl) + Math.abs(m - rm))/2; System.out.println(ans); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class Edu_46A { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int nE = Integer.parseInt(reader.readLine()); int[][] cnt = new int[][] { { 0, 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }; for (int i = 0; i < nE; i++) { String nxt = reader.readLine(); if (nxt.equals("S")) { cnt[0][0]++; } if (nxt.equals("M")) { cnt[0][1]++; } if (nxt.equals("L")) { cnt[0][2]++; } if (nxt.equals("XS")) { cnt[1][0]++; } if (nxt.equals("XL")) { cnt[1][1]++; } if (nxt.equals("XXS")) { cnt[2][0]++; } if (nxt.equals("XXL")) { cnt[2][1]++; } if (nxt.equals("XXXS")) { cnt[3][0]++; } if (nxt.equals("XXXL")) { cnt[3][1]++; } } for (int i = 0; i < nE; i++) { String nxt = reader.readLine(); if (nxt.equals("S")) { cnt[0][0]--; } if (nxt.equals("M")) { cnt[0][1]--; } if (nxt.equals("L")) { cnt[0][2]--; } if (nxt.equals("XS")) { cnt[1][0]--; } if (nxt.equals("XL")) { cnt[1][1]--; } if (nxt.equals("XXS")) { cnt[2][0]--; } if (nxt.equals("XXL")) { cnt[2][1]--; } if (nxt.equals("XXXS")) { cnt[3][0]--; } if (nxt.equals("XXXL")) { cnt[3][1]--; } } int ans = 0; for (int i = 1; i <= 3; i++) { ans += Math.abs(cnt[i][0]); } int max = 0; for (int i = 0; i < 3; i++) { max = Math.max(max, Math.abs(cnt[0][i])); } ans += max; printer.println(ans); printer.close(); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashMap; public class CodehorsesT_shirts { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(reader.readLine()); HashMap<String, Integer> map = new HashMap<String, Integer>(); String input; for (int i = 0; i < n; i++) { input = reader.readLine(); if (map.containsKey(input)) { map.put(input, map.get(input) + 1); } else { map.put(input, 1); } } int change = 0; for (int i = 0; i < n; i++) { input = reader.readLine(); if (map.containsKey(input)) { map.put(input, map.get(input) - 1); } else { map.put(input, -1); } } for (int x : map.values()) { change += Math.abs(x); } System.out.println(change/2); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); String[] s = {"XXXS", "XXS", "XS", "S", "M", "L", "XL", "XXL", "XXXL"}; int[] size = new int[9]; for(int i=0; i<N; i++){ String c = br.readLine(); for(int j=0; j<9; j++){ if(s[j].equals(c)){ size[j]++; } } } for(int i=0; i<N; i++){ String c = br.readLine(); for(int j=0; j<9; j++){ if(s[j].equals(c)){ size[j]--; } } } int sum = 0; for(int i=0; i<9; i++){ if(size[i]>0) sum += size[i]; } System.out.println(sum); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.*; import java.lang.*; import java.io.*; public class CodehorsesTShirt { public static void main(String args[]) throws IOException { FastReader in = new FastReader(); OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); Task.solve(in, out); out.close(); } static class Task { public static void solve(FastReader in, PrintWriter out) { int n = in.nextInt(); HashMap<String , Integer> hm1 = new HashMap<>(); HashMap<String , Integer> hm2 = new HashMap<>(); for(int i=0;i<n;i++){ String val = in.next(); if(hm1.containsKey(val)){ hm1.put(val, hm1.get(val)+1); }else{ hm1.put(val,1); } } for(int i=0;i<n;i++){ String val = in.next(); if(hm1.containsKey(val)){ int x = hm1.get(val); if(x==1){ hm1.remove(val); }else{ hm1.put(val,hm1.get(val)-1); } }else{ if(hm2.containsKey(val)){ hm2.put(val, hm2.get(val)+1); }else{ hm2.put(val,1); } } } int ans = 0; for(Map.Entry<String , Integer> row: hm1.entrySet()){ ans += row.getValue(); } System.out.println(ans); } } 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 (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.*; import java.util.*; /* * Heart beats fast * Colors and promises * How to be brave * How can I love when I am afraid... */ //read the question correctly (is y a vowel? what are the exact constraints?) //look out for SPECIAL CASES (n=1?) and overflow (ll vs int?) //always declare multidimensional arrays as [2][n] not [n][2] //it can lead to upto 2-3x diff in runtime //declare int/long tries with 16 array size due to object overhead :D public class Main { public static void main(String[] args) throws Exception { int n=ni(); Map<String, Integer> hola=new HashMap<String,Integer>(); hola.put("S", 0); hola.put("XS", 0); hola.put("XXS", 0); hola.put("XXXS", 0); hola.put("M", 0); hola.put("L", 0); hola.put("XL", 0); hola.put("XXL", 0); hola.put("XXXL", 0); for(int i=0; i<n; i++) { String te=ns(); hola.put(te,hola.get(te)+1); } for(int i=0; i<n; i++) { String te=ns(); hola.put(te,hola.get(te)-1); } int ans=0; for(int te:hola.values()) { ans+=max(te,0); } pr(ans); System.out.print(output); } /////////////////////////////////////////// /////////////////////////////////////////// ///template from here static int pow(int a, int b) { int c=1; while(b>0) { if(b%2!=0) c*=a; a*=a; } return c; } static class pair { int a, b; pair(){} pair(int c,int d){a=c;b=d;} } static interface combiner { public long combine(long a,long b); } static final long mod=1000000007; static final double eps=1e-9; static final long inf=100000000000000000L; static Reader in=new Reader(); static StringBuilder output=new StringBuilder(); static Random rn=new Random(); static void reverse(int[]a){for(int i=0; i<a.length/2; i++){a[i]^=a[a.length-i-1];a[a.length-i-1]^=a[i];a[i]^=a[a.length-i-1];}} static void sort(int[]a) { int te; for(int i=0; i<a.length; i+=2) { te=rn.nextInt(a.length); if(i!=te) { a[i]^=a[te]; a[te]^=a[i]; a[i]^=a[te]; } } Arrays.sort(a); } static void sort(long[]a) { int te; for(int i=0; i<a.length; i+=2) { te=rn.nextInt(a.length); if(i!=te) { a[i]^=a[te]; a[te]^=a[i]; a[i]^=a[te]; } } Arrays.sort(a); } static void sort(double[]a) { int te; double te1; for(int i=0; i<a.length; i+=2) { te=rn.nextInt(a.length); if(i!=te) { te1=a[te]; a[te]=a[i]; a[i]=te1; } } Arrays.sort(a); } static void sort(int[][]a) { Arrays.sort(a, new Comparator<int[]>() { public int compare(int[]a,int[]b) { if(a[0]>b[0]) return -1; if(b[0]>a[0]) return 1; return 0; } }); } static void sort(pair[]a) { Arrays.sort(a,new Comparator<pair>() { @Override public int compare(pair a,pair b) { if(a.a>b.a) return 1; if(b.a>a.a) return -1; return 0; } }); } static int log2n(long a) { int te=0; while(a>0) { a>>=1; ++te; } return te; } static class vectorl implements Iterable<Long> { long a[]; int size; vectorl(){a=new long[10];size=0;} vectorl(int n){a=new long[n];size=0;} public void add(long b){if(++size==a.length)a=Arrays.copyOf(a, 2*size);a[size-1]=b;} public void sort(){Arrays.sort(a, 0, size);} public void sort(int l, int r){Arrays.sort(a, l, r);} @Override public Iterator<Long> iterator() { Iterator<Long> hola=new Iterator<Long>() { int cur=0; @Override public boolean hasNext() { return cur<size; } @Override public Long next() { return a[cur++]; } }; return hola; } } static class vector implements Iterable<Integer> { int a[],size; vector(){a=new int[10];size=0;} vector(int n){a=new int[n];size=0;} public void add(int b){if(++size==a.length)a=Arrays.copyOf(a, 2*size);a[size-1]=b;} public void sort(){Arrays.sort(a, 0, size);} public void sort(int l, int r){Arrays.sort(a, l, r);} @Override public Iterator<Integer> iterator() { Iterator<Integer> hola=new Iterator<Integer>() { int cur=0; @Override public boolean hasNext() { return cur<size; } @Override public Integer next() { return a[cur++]; } }; return hola; } } //output functions//////////////// static void pr(Object a){output.append(a+"\n");} static void pr(){output.append("\n");} static void p(Object a){output.append(a);} static void pra(int[]a){for(int i:a)output.append(i+" ");output.append("\n");} static void pra(long[]a){for(long i:a)output.append(i+" ");output.append("\n");} static void pra(String[]a){for(String i:a)output.append(i+" ");output.append("\n");} static void pra(double[]a){for(double i:a)output.append(i+" ");output.append("\n");} static void sop(Object a){System.out.println(a);} static void flush(){System.out.println(output);output=new StringBuilder();} ////////////////////////////////// //input functions///////////////// static int ni(){return Integer.parseInt(in.next());} static long nl(){return Long.parseLong(in.next());} static String ns(){return in.next();} static double nd(){return Double.parseDouble(in.next());} static int[] nia(int n){int a[]=new int[n];for(int i=0; i<n; i++)a[i]=ni();return a;} static int[] pnia(int n){int a[]=new int[n+1];for(int i=1; i<=n; i++)a[i]=ni();return a;} static long[] nla(int n){long a[]=new long[n];for(int i=0; i<n; i++)a[i]=nl();return a;} static String[] nsa(int n){String a[]=new String[n];for(int i=0; i<n; i++)a[i]=ns();return a;} static double[] nda(int n){double a[]=new double[n];for(int i=0; i<n; i++)a[i]=nd();return a;} ////////////////////////////////// //some utility functions static void exit(){System.out.print(output);System.exit(0);} static int min(int... a){int min=a[0];for(int i:a)min=Math.min(min, i);return min;} static int max(int... a){int max=a[0];for(int i:a)max=Math.max(max, i);return max;} static int gcd(int... a){int gcd=a[0];for(int i:a)gcd=gcd(gcd, i);return gcd;} static long min(long... a){long min=a[0];for(long i:a)min=Math.min(min, i);return min;} static long max(long... a){long max=a[0];for(long i:a)max=Math.max(max, i);return max;} static long gcd(long... a){long gcd=a[0];for(long i:a)gcd=gcd(gcd, i);return gcd;} static String pr(String a, long b){String c="";while(b>0){if(b%2==1)c=c.concat(a);a=a.concat(a);b>>=1;}return c;} static long powm(long a, long b, long m){long an=1;long c=a;while(b>0){if(b%2==1)an=(an*c)%m;c=(c*c)%m;b>>=1;}return an;} static int gcd(int a, int b){if(b==0)return a;return gcd(b, a%b);} static long gcd(long a, long b){if(b==0)return a;return gcd(b, a%b);} static class Reader { public BufferedReader reader; public StringTokenizer tokenizer; public Reader() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.*; import java.util.*; public class TaskA { public static void main(String[] args) { new TaskA(System.in, System.out); } static class Solver implements Runnable { int n; String[] last, curr; BufferedReader in; PrintWriter out; void solve() throws IOException { n = Integer.parseInt(in.readLine()); last = new String[n]; curr = new String[n]; for (int i = 0; i < n; i++) last[i] = in.readLine(); for (int i = 0; i < n; i++) curr[i] = in.readLine(); int changes = 0; String[] sizes = new String[]{"S", "M", "L", "XS", "XXS", "XXXS", "XL", "XXL", "XXXL"}; int[] old = count(last, sizes); int[] now = count(curr, sizes); for (int i= 0; i < sizes.length; i++) { changes += Math.abs(old[i] - now[i]); } out.println(changes / 2); /* if (now[0] > old[0]) { int add = now[0] - old[0]; changes += add; }*/ } int[] count(String[] s, String[] sizes) { int len = sizes.length; int[] cnt = new int[len]; for (int i = 0; i < len; i++) { for (String str : s) { if (str.equals(sizes[i])) cnt[i]++; } } return cnt; } void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } public Solver(BufferedReader in, PrintWriter out) { this.in = in; this.out = out; } @Override public void run() { try { solve(); } catch (IOException e) { e.printStackTrace(); } } } static class CMath { static long power(long number, long power) { if (number == 1 || number == 0 || power == 0) return 1; if (power == 1) return number; if (power % 2 == 0) return power(number * number, power / 2); else return power(number * number, power / 2) * number; } static long modPower(long number, long power, long mod) { if (number == 1 || number == 0 || power == 0) return 1; number = mod(number, mod); if (power == 1) return number; long square = mod(number * number, mod); if (power % 2 == 0) return modPower(square, power / 2, mod); else return mod(modPower(square, power / 2, mod) * number, mod); } static long moduloInverse(long number, long mod) { return modPower(number, mod - 2, mod); } static long mod(long number, long mod) { return number - (number / mod) * mod; } static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } static long min(long... arr) { long min = arr[0]; for (int i = 1; i < arr.length; i++) min = Math.min(min, arr[i]); return min; } static long max(long... arr) { long max = arr[0]; for (int i = 1; i < arr.length; i++) max = Math.max(max, arr[i]); return max; } static int min(int... arr) { int min = arr[0]; for (int i = 1; i < arr.length; i++) min = Math.min(min, arr[i]); return min; } static int max(int... arr) { int max = arr[0]; for (int i = 1; i < arr.length; i++) max = Math.max(max, arr[i]); return max; } } static class Utils { static boolean nextPermutation(int[] arr) { for (int a = arr.length - 2; a >= 0; --a) { if (arr[a] < arr[a + 1]) { for (int b = arr.length - 1; ; --b) { if (arr[b] > arr[a]) { int t = arr[a]; arr[a] = arr[b]; arr[b] = t; for (++a, b = arr.length - 1; a < b; ++a, --b) { t = arr[a]; arr[a] = arr[b]; arr[b] = t; } return true; } } } } return false; } } public TaskA(InputStream inputStream, OutputStream outputStream) { BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); PrintWriter out = new PrintWriter(outputStream); Thread thread = new Thread(null, new Solver(in, out), "TaskA", 1 << 29); try { thread.start(); thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } out.flush(); out.close(); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.util.ArrayList; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ACodehorsesTShirts solver = new ACodehorsesTShirts(); solver.solve(1, in, out); out.close(); } static class ACodehorsesTShirts { public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); ArrayList<String>[] arrayLists = new ArrayList[5]; ArrayList<String>[] arrayLists1 = new ArrayList[5]; for (int i = 0; i < 5; i++) { arrayLists[i] = new ArrayList<>(); arrayLists1[i] = new ArrayList<>(); } for (int i = 0; i < n; i++) { String s = in.scanString(); arrayLists[s.length()].add(s); } for (int i = 0; i < n; i++) { String s = in.scanString(); arrayLists1[s.length()].add(s); } long ans = 0; for (int i = 0; i < 5; i++) { for (int diff = 0; diff < 5; diff++) { for (int j = 0; j < arrayLists[i].size(); j++) { int min = Integer.MAX_VALUE; int index = -1; for (int k = 0; k < arrayLists1[i].size(); k++) { int tt = 0; for (int l = 0; l < i; l++) if (arrayLists[i].get(j).charAt(l) != arrayLists1[i].get(k).charAt(l)) tt++; if (tt < min) { min = tt; index = k; } } if (min == diff) { arrayLists1[i].remove(index); ans += min; } } } } out.println(ans); } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int index; private BufferedInputStream in; private int total; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (index >= total) { index = 0; try { total = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (total <= 0) return -1; } return buf[index++]; } public int scanInt() { int integer = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } } return neg * integer; } public String scanString() { int c = scan(); if (c == -1) return null; while (isWhiteSpace(c)) c = scan(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = scan(); } while (!isWhiteSpace(c)); return res.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.*; import java.io.*; import java.text.DecimalFormat; public class Main{ final long mod = (int)1e9+7, IINF = (long)1e19; final int MAX = (int)1e6+1, MX = (int)1e7+1, INF = (int)1e9, root = 3; DecimalFormat df = new DecimalFormat("0.0000000000000"); double eps = 1e-9; FastReader in; PrintWriter out; static boolean multipleTC = false, memory = false; public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start(); else new Main().run(); } void run() throws Exception{ in = new FastReader(); out = new PrintWriter(System.out); for(int i = 1, t = (multipleTC)?ni():1; i<=t; i++)solve(i); out.flush(); out.close(); } void solve(int TC)throws Exception{ int n = ni(); int[] f1 = new int[9], f2 = new int[9]; MyHashSet<String> set = new MyHashSet<>(); for(int i = 0; i< n; i++){ String s = n(); set.add(s); f1[s.length()]++; } int[] f = new int[4]; for(int i = 0; i< n; i++){ String s = n(); if(set.remove(s))continue; else f[s.length()-1]++; } int ans = 0; for(int i = 0; i< 4; i++)ans+=f[i]; pn(ans); } class MyHashSet<T>{ private int size; private HashMap<T, Integer> map; public MyHashSet(){ size = 0; map = new HashMap<>(); } public int size(){return size;} public void add(T t){ size++; map.put(t, map.getOrDefault(t, 0)+1); } public int cnt(T t){return map.getOrDefault(t,0);} public boolean remove(T t){ if(!map.containsKey(t))return false; size--; int c = map.get(t); if(c==1)map.remove(t); else map.put(t, c-1); return true; } } int[] reverse(int[] a){ int[] o = new int[a.length]; for(int i = 0; i< a.length; i++)o[i] = a[a.length-i-1]; return o; } int[] sort(int[] a){ if(a.length==1)return a; int mid = a.length/2; int[] b = sort(Arrays.copyOfRange(a,0,mid)), c = sort(Arrays.copyOfRange(a,mid,a.length)); for(int i = 0, j = 0, k = 0; i< a.length; i++){ if(j<b.length && k<c.length){ if(b[j]<c[k])a[i] = b[j++]; else a[i] = c[k++]; }else if(j<b.length)a[i] = b[j++]; else a[i] = c[k++]; } return a; } long[] sort(long[] a){ if(a.length==1)return a; int mid = a.length/2; long[] b = sort(Arrays.copyOfRange(a,0,mid)), c = sort(Arrays.copyOfRange(a,mid,a.length)); for(int i = 0, j = 0, k = 0; i< a.length; i++){ if(j<b.length && k<c.length){ if(b[j]<c[k])a[i] = b[j++]; else a[i] = c[k++]; }else if(j<b.length)a[i] = b[j++]; else a[i] = c[k++]; } return a; } long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bitcount(long n){return (n==0)?0:(1+bitcount(n&(n-1)));} void p(Object o){out.print(o);} void pn(Object o){out.println(o);} void pni(Object o){out.println(o);out.flush();} String n(){return in.next();} String nln(){return in.nextLine();} int ni(){return Integer.parseInt(in.next());} long nl(){return Long.parseLong(in.next());} double nd(){return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } String nextLine(){ String str = ""; try{ str = br.readLine(); }catch (IOException e){ e.printStackTrace(); } return str; } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Map; import java.util.HashMap; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author programajor */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); Map<String, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { String x = in.next(); map.put(x, map.getOrDefault(x, 0) + 1); } int ans = 0; for (int i = 0; i < n; i++) { String x = in.next(); if (map.containsKey(x)) { map.put(x, map.get(x) - 1); if (map.get(x) == 0) { map.remove(x); } } else { ans++; } } out.print(ans); } } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main { static MyScanner scan; static PrintWriter pw; public static void main(String[] args) { new Thread(null,null,"_",1<<25) { public void run() { try { solve(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static void solve() throws IOException { scan = new MyScanner(); pw = new PrintWriter(System.out, true); StringBuilder sb = new StringBuilder(); Map<String,Integer> map = new HashMap<>(); map.put("M",0); map.put("L",1); map.put("S",2); map.put("XL",3); map.put("XS",4); map.put("XXL",5); map.put("XXS",6); map.put("XXXL",7); map.put("XXXS",8); int freqa[] = new int[9]; int freqb[] = new int[9]; int n = ni(); for(int i=0;i<n;++i) ++freqa[map.get(ne())]; for(int i=0;i<n;++i) ++freqb[map.get(ne())]; for(int i=0;i<9;++i) { int xx = min(freqa[i],freqb[i]); freqa[i]-=xx; freqb[i]-=xx; } long res = 0; for(int i=0;i<9;++i) res+=freqa[i]+freqb[i]; pl(res/2); pw.flush(); pw.close(); } static long MMI(long A,long MOD) { return modpow(A,MOD-2,MOD); } static long modpow(long x,long y,long MOD) { if(y==0) return 1; if((y&1)==0) return modpow((x*x)%MOD,y>>1,MOD); else return (x*modpow(x,y-1,MOD))%MOD; } static class Pair implements Comparable<Pair> { int x,y; Pair(int x,int y) { this.x=x; this.y=y; } public int compareTo(Pair other) { if(this.x!=other.x) return this.x-other.x; return this.y-other.y; } public String toString() { return "{"+x+","+y+"}"; } } static int ni() throws IOException { return scan.nextInt(); } static long nl() throws IOException { return scan.nextLong(); } static double nd() throws IOException { return scan.nextDouble(); } static String ne() throws IOException { return scan.next(); } static String nel() throws IOException { return scan.nextLine(); } static void pl() { pw.println(); } static void p(Object o) { pw.print(o+" "); } static void pl(Object o) { pw.println(o); } static void psb(StringBuilder sb) { pw.print(sb); } static class MyScanner { BufferedReader br; StringTokenizer st; MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextLine()throws IOException { return br.readLine(); } String next() throws IOException { if(st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.*; public class q1 { public static void main(String[] args) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int[] arr1=new int[9]; int[] arr2=new int[9]; String ss; s.nextLine(); for(int i=0;i<n;i++) { ss=s.nextLine(); if(ss.equals("M")) arr1[0]++; else if(ss.equals("S")) arr1[1]++; else if(ss.equals("L")) arr1[2]++; else if(ss.equals("XS")) arr1[3]++; else if(ss.equals("XL")) arr1[4]++; else if(ss.equals("XXS")) arr1[5]++; else if(ss.equals("XXL")) arr1[6]++; else if(ss.equals("XXXS")) arr1[7]++; else if(ss.equals("XXXL")) arr1[8]++; } for(int i=0;i<n;i++) { ss=s.nextLine(); if(ss.equals("M")) arr2[0]++; else if(ss.equals("S")) arr2[1]++; else if(ss.equals("L")) arr2[2]++; else if(ss.equals("XS")) arr2[3]++; else if(ss.equals("XL")) arr2[4]++; else if(ss.equals("XXS")) arr2[5]++; else if(ss.equals("XXL")) arr2[6]++; else if(ss.equals("XXXS")) arr2[7]++; else if(ss.equals("XXXL")) arr2[8]++; } int min; for(int i=0;i<9;i++) { if(arr1[i]<arr2[i]) min=arr1[i]; else min=arr2[i]; arr1[i]-=min; arr2[i]-=min; } int sum=0; for(int i=0;i<9;i++) { sum+=arr1[i]; } System.out.println(sum); //System.out.println(Arrays.toString(arr2)); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.*; import java.io.*; public class A{ static int N, M, K; static String s; static StringTokenizer st; static int[] d; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int[][] d = new int[5][3]; int[][] d2 = new int[5][3]; int N = Integer.parseInt(br.readLine()); for (int i = 0; i < N; i++) { String r = br.readLine(); int len = r.length(); int fin = 0; if(r.charAt(r.length()-1) == 'S') fin = 0; if(r.charAt(r.length()-1) == 'M') fin = 1; if(r.charAt(r.length()-1) == 'L') fin = 2; d[len][fin]++; } for (int i = 0; i < N; i++) { String r = br.readLine(); int len = r.length(); int fin = 0; if(r.charAt(r.length()-1) == 'S') fin = 0; if(r.charAt(r.length()-1) == 'M') fin = 1; if(r.charAt(r.length()-1) == 'L') fin = 2; d2[len][fin]++; } int ans = 0; for (int i = 0; i < d.length; i++) { int sum = 0; int sum2 = 0; for (int j = 0; j < d[0].length; j++) { sum += d[i][j]; sum2 += d2[i][j]; ans += Math.max(0, d2[i][j] - d[i][j]); } } System.out.println(ans); out.close(); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.*; import java.util.*; public class A { public static void main(String[] args)throws Throwable { MyScanner sc=new MyScanner(); PrintWriter pw=new PrintWriter(System.out); int n=sc.nextInt(); String [] s={"M","L","S","XL","XS","XXL","XXS","XXXL","XXXS"}; int [] cnt=new int [9]; for(int i=0;i<n;i++){ String t=sc.next(); for(int j=0;j<9;j++) if(t.equals(s[j])) cnt[j]++; } for(int i=0;i<n;i++){ String t=sc.next(); for(int j=0;j<9;j++) if(t.equals(s[j])) cnt[j]--; } for(int i=0;i<9;i++) cnt[i]=Math.abs(cnt[i]); int ans=0; for(int i=0;i<9;i++) ans+=cnt[i]; pw.println(ans/2); pw.flush(); pw.close(); } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() {while (st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();}} return st.nextToken();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(next());} String nextLine(){String str = ""; try {str = br.readLine();} catch (IOException e) {e.printStackTrace();} return str;} } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Main implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main(),"Main",1<<26).start(); } public void run() { InputReader sc = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n=sc.nextInt(); String a[]=new String[n]; String b[]=new String[n]; int freaa[][]=new int[5][4]; for (int i = 0; i <n ; i++) { a[i]=sc.next(); int ll=a[i].length(); for (int j = 0; j <a[i].length() ; j++) { if(a[i].charAt(j)=='X') freaa[ll][0]++; if(a[i].charAt(j)=='L') freaa[ll][1]++; if(a[i].charAt(j)=='M') freaa[ll][2]++; if(a[i].charAt(j)=='S') freaa[ll][3]++; } } int frebb[][]=new int[5][4]; for (int i = 0; i <n ; i++) { b[i]=sc.next(); int ll=b[i].length(); for (int j = 0; j <b[i].length() ; j++) { if(b[i].charAt(j)=='X') frebb[ll][0]++; if(b[i].charAt(j)=='L') frebb[ll][1]++; if(b[i].charAt(j)=='M') frebb[ll][2]++; if(b[i].charAt(j)=='S') frebb[ll][3]++; } } int ans=0; for (int i = 1; i <5 ; i++) { for (int j = 0; j <4 ; j++) { if(freaa[i][j]<frebb[i][j]){ ans+=frebb[i][j]-freaa[i][j]; } } } out.println(ans); out.close(); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class JavaApplication2 { public static void main(String[] args) throws IOException { BufferedReader sc= new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(sc.readLine().split(" ")[0]); ArrayList<String> tshr = new ArrayList<>(n); for (int i = 0; i < n; i++) { tshr.add(sc.readLine()); } for (int i = 0; i < n; i++) { tshr.remove(sc.readLine()); } System.out.println(tshr.size()); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.*; import java.io.*; public class _1000_A { public static void main(String[] args) throws IOException { HashMap<String, Integer> map1 = new HashMap<>(), map2 = new HashMap<>(); int N = readInt(); for(int i = 1; i<=N; i++) { String s = read(); if(!map1.containsKey(s)) map1.put(s, 1); else map1.put(s, map1.get(s)+1); } int tot = 0; for(int i = 1; i<=N; i++) { String s = read(); if(!map2.containsKey(s)) map2.put(s, 1); else map2.put(s, map2.get(s)+1); } for(String s : map2.keySet()) { tot += Math.max(0, map2.get(s) - (map1.containsKey(s) ? map1.get(s) : 0)); } println(tot); exit(); } final private static int BUFFER_SIZE = 1 << 16; private static DataInputStream din = new DataInputStream(System.in); private static byte[] buffer = new byte[BUFFER_SIZE]; private static int bufferPointer = 0, bytesRead = 0; static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = Read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public static String read() throws IOException { byte[] ret = new byte[1024]; int idx = 0; byte c = Read(); while (c <= ' ') { c = Read(); } do { ret[idx++] = c; c = Read(); } while (c != -1 && c != ' ' && c != '\n' && c != '\r'); return new String(ret, 0, idx); } public static int readInt() throws IOException { int ret = 0; byte c = Read(); while (c <= ' ') c = Read(); boolean neg = (c == '-'); if (neg) c = Read(); do { ret = ret * 10 + c - '0'; } while ((c = Read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public static long readLong() throws IOException { long ret = 0; byte c = Read(); while (c <= ' ') c = Read(); boolean neg = (c == '-'); if (neg) c = Read(); do { ret = ret * 10 + c - '0'; } while ((c = Read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public static double readDouble() throws IOException { double ret = 0, div = 1; byte c = Read(); while (c <= ' ') c = Read(); boolean neg = (c == '-'); if (neg) c = Read(); do { ret = ret * 10 + c - '0'; } while ((c = Read()) >= '0' && c <= '9'); if (c == '.') { while ((c = Read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private static void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private static byte Read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } static void print(Object o) { pr.print(o); } static void println(Object o) { pr.println(o); } static void flush() { pr.flush(); } static void println() { pr.println(); } static void exit() throws IOException { din.close(); pr.close(); System.exit(0); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.*; import java.util.*; public class n2{ public static void main(String[] args) throws Exception{ BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(in.readLine()); int[] S=new int[4]; int[] L=new int[4]; int m=0; for(int i=0;i<n;i++){ String s=in.readLine(); if(s.charAt(s.length()-1)=='L'){ L[s.length()-1]++; } if(s.charAt(s.length()-1)=='S'){ S[s.length()-1]++; } if(s.charAt(s.length()-1)=='M'){ m++; } } for(int i=0;i<n;i++){ String s=in.readLine(); if(s.charAt(s.length()-1)=='L'){ L[s.length()-1]--; } if(s.charAt(s.length()-1)=='S'){ S[s.length()-1]--; } if(s.charAt(s.length()-1)=='M'){ m--; } } int count=0; for(int i=0;i<=3;i++){ if(S[i]>0){ count+=S[i]; } if(L[i]>0){ count+=L[i]; } } if(m>0){ count+=m; } System.out.println(count); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.util.Arrays; import java.util.Comparator; import java.util.TreeSet; public class Main { public static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); public static int read() throws IOException { tokenizer.nextToken(); return (int) tokenizer.nval; } public static void main(String[] args) throws IOException { Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); ArrayList<String> list1=new ArrayList<String>(); ArrayList<String> list2=new ArrayList<String>(); for (int i=0; i<n; i++){ String s=scanner.next(); list1.add(s); } for (int i=0; i<n; i++){ String s=scanner.next(); list2.add(s); } for (int i=0; i<list1.size(); i++){ for (int j=0; j<list2.size(); j++){ if (list1.get(i).equals(list2.get(j))){ list1.remove(i); list2.remove(j); i--; break; } } } System.out.println(list1.size()); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class A { static StringTokenizer st; static BufferedReader br; static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = nextInt(); String[]a = new String[n], b = new String[n]; Map<String, Integer> map = new HashMap<String, Integer>(); for (int i = 0; i < n; i++) { a[i] = next(); if (!map.containsKey(a[i])) map.put(a[i], 0); map.put(a[i], map.get(a[i])+1); } int ans = 0; for (int i = 0; i < n; i++) { b[i] = next(); if (!map.containsKey(b[i])) ans++; else { map.put(b[i], map.get(b[i])-1); if (map.get(b[i])==0) map.remove(b[i]); } } System.out.println(ans); pw.close(); } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } private static String next() throws IOException { while (st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
/* Keep solving problems. */ import java.util.*; import java.io.*; public class CFA { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; private static final long MOD = 1000 * 1000 * 1000 + 7; private static final int[] dx = {0, -1, 0, 1}; private static final int[] dy = {1, 0, -1, 0}; private static final String yes = "Yes"; private static final String no = "No"; void solve() throws IOException { int n = nextInt(); String[] arr1 = new String[n]; String[] arr2 = new String[n]; for (int i = 0; i < n; i++) { arr1[i] = nextString(); } for (int i = 0; i < n; i++) { arr2[i] = nextString(); } Map<String, Integer> m1 = getMap(arr1); Map<String, Integer> m2 = getMap(arr2); int res = 0; for (Map.Entry<String, Integer> entry : m2.entrySet()) { String key = entry.getKey(); int val = entry.getValue(); if (m1.containsKey(key)) { int v2 = m1.get(key); if (val > v2) { res += val - v2; } } else { res += val; } } for (Map.Entry<String, Integer> entry : m1.entrySet()) { String key = entry.getKey(); int val = entry.getValue(); if (m2.containsKey(key)) { int v2 = m2.get(key); if (val > v2) { res += val - v2; } } else { res += val; } } outln(res / 2); } Map<String, Integer> getMap(String[] arr) { Map<String, Integer> res = new HashMap<>(); for (String str : arr) { res.put(str, res.getOrDefault(str, 0) + 1); } return res; } void shuffle(int[] a) { int n = a.length; for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } long gcd(long a, long b) { while(a != 0 && b != 0) { long c = b; b = a % b; a = c; } return a + b; } private void outln(Object o) { out.println(o); } private void out(Object o) { out.print(o); } private void formatPrint(double val) { outln(String.format("%.9f%n", val)); } public CFA() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new CFA(); } public long[] nextLongArr(int n) throws IOException{ long[] res = new long[n]; for(int i = 0; i < n; i++) res[i] = nextLong(); return res; } public int[] nextIntArr(int n) throws IOException { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } public String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.math.*; import java.util.*; import java.io.*; public class Test5 { public static void main(String[] z){ StreamTokenizer st = new StreamTokenizer(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); Scanner s = new Scanner(System.in); int a = s.nextInt(), o=0; String i = ""; ArrayList<String> l1 = new ArrayList<>(), l2 = new ArrayList<>(); for(int q=0; q<a; q++){ l1.add(s.next()); } for(int q=0; q<a; q++){ i = s.next(); if(l1.contains(i)) l1.remove(i); else l2.add(i); } Collections.sort(l1); Collections.sort(l2); for(int q=0; q<l1.size(); q++){ if(l1.get(q).charAt(l1.get(q).length()-1)!=l2.get(q).charAt(l2.get(q).length()-1)) o++; } System.out.println(o); pw.flush(); } static class PyraSort { private static int heapSize; public static void sort(int[] a) { buildHeap(a); while (heapSize > 1) { swap(a, 0, heapSize - 1); heapSize--; heapify(a, 0); } } private static void buildHeap(int[] a) { heapSize = a.length; for (int i = a.length / 2; i >= 0; i--) { heapify(a, i); } } private static void heapify(int[] a, int i) { int l = 2 * i + 2; int r = 2 * i + 1; int largest = i; if (l < heapSize && a[i] < a[l]) { largest = l; } if (r < heapSize && a[largest] < a[r]) { largest = r; } if (i != largest) { swap(a, i, largest); heapify(a, largest); } } private static void swap(int[] a, int i, int j) { a[i] ^= a[j] ^= a[i]; a[j] ^= a[i]; } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.*; public class A { public static void main(String[] args) { JS in = new JS(); int n = in.nextInt(); int m1 = 0; int s1 = 0; int l1 = 0; int ss1 = 0; int sss1 = 0; int ssss1 = 0; int ll1 = 0; int lll1 = 0; int llll1 = 0; int m2 = 0; int s2 = 0; int l2 = 0; int ss2 = 0; int sss2 = 0; int ssss2 = 0; int ll2 = 0; int lll2 = 0; int llll2 = 0; for(int i = 0; i < n; i++) { String s = in.next(); if(s.equals("S")) s1++; else if(s.equals("M"))m1++; else if(s.equals("L"))l1++; else if(s.equals("XS")) ss1++; else if(s.equals("XXS")) sss1++; else if(s.equals("XXXS")) ssss1++; else if(s.equals("XL")) ll1++; else if(s.equals("XXL")) lll1++; else if(s.equals("XXXL")) llll1++; } for(int i = 0; i < n; i++) { String s = in.next(); if(s.equals("S")) s2++; else if(s.equals("M"))m2++; else if(s.equals("L"))l2++; else if(s.equals("XS")) ss2++; else if(s.equals("XXS")) sss2++; else if(s.equals("XXXS")) ssss2++; else if(s.equals("XL")) ll2++; else if(s.equals("XXL")) lll2++; else if(s.equals("XXXL")) llll2++; } int res = 0; int res1 = 0; res1 += Math.abs(m2-m1); res1 += Math.abs(s2-s1); res1 += Math.abs(l2-l1); res += res1/2; res1 = 0; res1 += Math.abs(ss2-ss1); res1 += Math.abs(ll2-ll1); res += res1/2; res1 = 0; res1 += Math.abs(sss2-sss1); res1 += Math.abs(lll2-lll1); res += res1/2; res1 = 0; res1 += Math.abs(ssss2-ssss1); res1 += Math.abs(llll2-llll1); res += res1/2; res1 = 0; System.out.println(res); } static class JS{ public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/num; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c!='\n') { res.append(c); c=nextChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=nextChar(); if(c==NC)return false; else if(c>32)return true; } } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
//package que_a; import java.io.*; import java.util.*; import java.math.*; public class utkarsh { InputStream is; PrintWriter out; long mod = (long) (1e9 + 7); boolean SHOW_TIME; void solve() { //Enter code here utkarsh //SHOW_TIME = true; int n = ni(); HashMap <String, Integer> mp = new HashMap<>(); mp.put("M", 0); mp.put("S", 1); mp.put("XS", 2); mp.put("XXS", 3); mp.put("XXXS", 4); mp.put("L", 5); mp.put("XL", 6); mp.put("XXL", 7); mp.put("XXXL", 8); int a[] = new int[10]; for(int i = 0; i < n; i++) { int j = mp.get(ns()); a[j]++; } for(int i = 0; i < n; i++) { int j = mp.get(ns()); a[j]--; } int ans = 0; for(int i = 0; i < 10; i++) { if(a[i] > 0) ans += a[i]; } out.println(ans); } //---------- I/O Template ---------- public static void main(String[] args) { new utkarsh().run(); } void run() { is = System.in; out = new PrintWriter(System.out); long start = System.currentTimeMillis(); solve(); long end = System.currentTimeMillis(); if(SHOW_TIME) out.println("\n" + (end - start) + " ms"); out.flush(); } byte input[] = new byte[1024]; int len = 0, ptr = 0; int readByte() { if(ptr >= len) { ptr = 0; try { len = is.read(input); } catch(IOException e) { throw new InputMismatchException(); } if(len <= 0) { return -1; } } return input[ptr++]; } boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); } int skip() { int b = readByte(); while(b != -1 && isSpaceChar(b)) { b = readByte(); } return b; } char nc() { return (char)skip(); } String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } int ni() { int n = 0, b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } if(b == '-') { minus = true; b = readByte(); } if(b == -1) { return -1; } //no input while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n; } long nl() { long n = 0L; int b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } if(b == '-') { minus = true; b = readByte(); } while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n; } double nd() { return Double.parseDouble(ns()); } float nf() { return Float.parseFloat(ns()); } int[] na(int n) { int a[] = new int[n]; for(int i = 0; i < n; i++) { a[i] = ni(); } return a; } char[] ns(int n) { char c[] = new char[n]; int i, b = skip(); for(i = 0; i < n; i++) { if(isSpaceChar(b)) { break; } c[i] = (char)b; b = readByte(); } return i == n ? c : Arrays.copyOf(c,i); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; import java.util.Collections; import java.util.HashSet; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); ArrayList<String> s1=new ArrayList<String> (); ArrayList<String> s2=new ArrayList<String> (); ArrayList<String> s3=new ArrayList<String> (); int i; for(i=0;i<n;i++) s1.add(sc.next()); for(i=0;i<n;i++) s2.add(sc.next()); s3.addAll(s2); for(i=0;i<n;i++) { if(s2.contains(s1.get(i))) s3.remove(s1.get(i)); } System.out.println(s3.size()); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.BufferedReader; // import java.io.FileInputStream; // import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintWriter; import java.util.*; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; import static java.util.Arrays.copyOf; import static java.util.Arrays.fill; import static java.util.Arrays.sort; // import static java.util.Collections.sort; import static java.util.Comparator.comparingInt; public class Main { FastScanner in; PrintWriter out; private void solve() throws IOException { solveA(); // solveB(); // solveC(); // solveD(); // solveE(); // solveF(); } private void solveA() throws IOException { int n = in.nextInt(); TreeMap<String, Integer> map = new TreeMap<>(); for (int i = 0; i < n; i++) { String s = in.next(); map.put(s, map.getOrDefault(s, 0) + 1); } for (int i = 0; i < n; i++) { String s = in.next(); map.put(s, map.getOrDefault(s, 0) - 1); } long ans = 0; for (String i : map.keySet()) ans += abs(map.get(i)); out.println(ans / 2); } private void solveB() throws IOException { int n = in.nextInt(); int time = (int) 1e9, ans = -1; for (int i = 0; i < n; i++) { int a = in.nextInt() - i; if ((a + (n - 1)) / n < time) { time = (a + (n - 1)) / n; ans = i; } } out.println(ans + 1); } class PairC { int i, j; PairC(int i, int j) { this.i = i; this.j = j; } public String toString() { return "(" + i + ", " + j + ")"; } } private void solveC() throws IOException { int n = in.nextInt(), k = in.nextInt(); int[][] a = new int[4][n]; for (int i = 0; i < 4; i++) for (int j = 0; j < n; j++) a[i][j] = in.nextInt() - 1; int[] empty = new int[4]; PairC[] from = new PairC[k]; for (int i = 1; i < 3; i++) for (int j = 0; j < n; j++) if (a[i][j] != -1) from[a[i][j]] = new PairC(i, j); else empty[i]++; PairC[] to = new PairC[k]; for (int i = 0; i < 4; i += 3) for (int j = 0; j < n; j++) if (a[i][j] != -1) to[a[i][j]] = new PairC(i, j); out.println(Arrays.toString(from)); out.println(Arrays.toString(to)); ArrayList<int[]> ans = new ArrayList<>(); int cnt = 0; for (int i = 0; i < k; i++) { if (abs(from[i].i - to[i].i) == 1) { if (from[i].j == to[i].j) { ans.add(new int[]{i, to[i].i, to[i].j}); a[from[i].i][from[i].j] = 0; empty[from[i].i]++; cnt++; } } else if (from[i].j == to[i].j && a[(from[i].i + to[i].i) / 2][to[i].j] == 0) { ans.add(new int[]{i, (from[i].i + to[i].i) / 2, to[i].j}); ans.add(new int[]{i, to[i].i, to[i].j}); a[from[i].i][from[i].j] = 0; empty[from[i].i]++; cnt++; } } for (int i = 1; i < 3; i++) { if (empty[i] > 0) { for (int j = 0; j < k; j++) { if (from[j].i == i && true) { } } } } while (true) { boolean flag = false; for (int i = 0; i < k; i++) { if (abs(from[i].i - to[i].i) == 1 && abs(from[i].j - to[i].j) <= empty[i]) { } } if (!flag) break; } if (cnt == k) { out.println(ans.size()); for (int[] i : ans) { for (int j : i) out.print(j + 1 + " "); out.println(); } } else out.println(-1); } private void solveD() throws IOException { int n = in.nextInt(); int[] a = new int[n * 2]; for (int i = 0; i < n * 2; i++) a[i] = in.nextInt(); long ans = 0; for (int i = 0; i < 2 * n; i += 2) { int j = i + 1; while (a[i] != a[j]) j++; ans += j - i - 1; while (j > i + 1) a[j] = a[--j]; } out.println(ans); } class PairE implements Comparable<PairE> { long x, y; int id; boolean b; PairE(long x, long y, int id) { this.x = x; this.y = y; this.id = id; b = false; } @Override public int compareTo(PairE o) { return x != o.x ? Long.compare(x, o.x) : Long.compare(y, o.y); } } private void solveE() throws IOException { int n = in.nextInt(); PairE[] p = new PairE[n]; for (int i = 0; i < n; i++) p[i] = new PairE(in.nextLong(), in.nextLong(), i); shuffle(p); sort(p); long X = 0, Y = 0; long max = 225 * (long) 1e10; for (int i = 0; i < n; i++) { if ((X + p[i].x) * (X + p[i].x) + (Y + p[i].y) * (Y + p[i].y) < (X - p[i].x) * (X - p[i].x) + (Y - p[i].y) * (Y - p[i].y)) { p[i].b = true; X += p[i].x; Y += p[i].y; } else { p[i].b = false; X -= p[i].x; Y -= p[i].y; } } sort(p, comparingInt(o -> o.id)); for (int i = 0; i < n; i++) { out.print(p[i].b ? 1 : -1); if (i + 1 < n) out.print(" "); } out.println(); } void shuffle(PairE[] a) { PairE b; Random r = new Random(); for (int i = a.length - 1, j; i > 0; i--) { j = r.nextInt(i + 1); b = a[j]; a[j] = a[i]; a[i] = b; } } private void solveF() throws IOException { } class FastScanner { StringTokenizer st; BufferedReader br; FastScanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } boolean hasNext() throws IOException { return br.ready() || (st != null && st.hasMoreTokens()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next().replace(',', '.')); } String nextLine() throws IOException { return br.readLine(); } boolean hasNextLine() throws IOException { return br.ready(); } } private void run() throws IOException { in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in")); out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out")); solve(); out.flush(); out.close(); } public static void main(String[] args) throws IOException { new Main().run(); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; public class A { public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); // Scanner scan = new Scanner(System.in); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(bf.readLine()); ArrayList<String> s1 = new ArrayList<String>(); ArrayList<String> s2 = new ArrayList<String>(); for(int i=0; i<n; i++) s1.add(bf.readLine()); for(int i=0; i<n; i++) s2.add(bf.readLine()); Map<String, Integer> mp1 = new HashMap<String, Integer>(); Map<String, Integer> mp2 = new HashMap<String, Integer>(); for(String s : s1) mp1.put(s, 0); for(String s : s1) mp1.put(s, mp1.get(s)+1); for(String s : s2) mp2.put(s, 0); for(String s : s2) mp2.put(s, mp2.get(s)+1); for(String s : mp1.keySet()) { while(mp1.get(s) > 0) { if(mp2.containsKey(s)) { if(mp2.get(s) > 0) { mp1.put(s, mp1.get(s)-1); mp2.put(s, mp2.get(s)-1); } else break; } else break; } } for(String s : mp2.keySet()) { while(mp2.get(s) > 0) { if(mp1.containsKey(s)) { if(mp1.get(s) > 0) { mp2.put(s, mp2.get(s)-1); mp1.put(s, mp1.get(s)-1); } else break; } else break; } } long sum = 0; for(String s : mp1.keySet()) sum += mp1.get(s); out.println(sum); //out.println(mp1.keySet().size()); // out.close(); // StringTokenizer st = new StringTokenizer(bf.readLine()); // int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken()); // int n = Integer.parseInt(st.nextToken()); // int n = scan.nextInt(); out.close(); System.exit(0); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.OutputStream; import java.io.Writer; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author palayutm */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); String[] a = new String[n]; String[] b = new String[n]; for (int i = 0; i < n; i++) { a[i] = in.next(); } for (int i = 0; i < n; i++) { b[i] = in.next(); } int ans = 0; for (int i = 1; i < 5; i++) { int a1 = 0, b1 = 0, c1 = 0; for (int j = 0; j < n; j++) { if (a[j].length() == i) { if (a[j].charAt(i - 1) == 'M') { a1++; } else if (a[j].charAt(i - 1) == 'S') { b1++; } else { c1++; } } } for (int j = 0; j < n; j++) { if (b[j].length() == i) { if (b[j].charAt(i - 1) == 'M') { a1--; } else if (b[j].charAt(i - 1) == 'S') { b1--; } else { c1--; } } } if (a1 > 0) ans += a1; if (b1 > 0) ans += b1; if (c1 > 0) ans += c1; } out.println(ans); } } static class InputReader { private InputStream stream; private byte[] inbuf = new byte[1024]; private int lenbuf = 0; private int ptrbuf = 0; public InputReader(InputStream stream) { this.stream = stream; } private int readByte() { if (lenbuf == -1) throw new UnknownError(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = stream.read(inbuf); } catch (IOException e) { throw new UnknownError(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } static class OutputWriter extends PrintWriter { public OutputWriter(OutputStream out) { super(out); } public OutputWriter(Writer out) { super(out); } public void close() { super.close(); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Rishabhdeep Singh */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] cnt = new int[10]; Arrays.fill(cnt, 0); List<String> a = new ArrayList<>(); for (int i = 0; i < n; i++) { a.add(in.readLine()); } List<String> b = new ArrayList<>(); for (int i = 0; i < n; i++) { String temp = in.readLine(); if (a.contains(temp)) { a.remove(temp); } else b.add(temp); } int[] cnta = new int[10]; for (int i = 0; i < a.size(); i++) { cnta[a.get(i).length()]++; } int[] cntb = new int[10]; Arrays.fill(cnta, 0); Arrays.fill(cntb, 0); for (int i = 0; i < b.size(); i++) { cntb[a.get(i).length()]++; } int ans = 0; for (int i = 0; i < 10; i++) { ans += Math.abs(cnta[i] - cntb[i]); } out.println(ans); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(int i) { writer.println(i); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
//package jsr.codeforces; import java.util.HashMap; import java.util.Scanner; public class AMatchLists { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); HashMap<String, Integer> map = new HashMap<>(); for(int i=0; i<N; i++){ String str = in.next(); if(map.get(str)==null){ map.put(str, 0); } map.put(str, map.get(str)+1); } HashMap<String, Integer> map2 = new HashMap<>(); for(int i=0; i<N; i++){ String str = in.next(); if(map.get(str)!=null){ if(map.get(str)==1) map.remove(str); else map.put(str, map.get(str)-1); } else{ if(map2.get(str)==null){ map2.put(str, 0); } map2.put(str, map2.get(str)+1); } } int[] count= {0}; map2.forEach((key, value)->{ count[0] += value; }); System.out.println(count[0]); //M, XS, XXS, XXXS, L, XL, XXl, XXXL } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.math.*; import java.util.*; import java.io.*; public class Main { void solve(){ int n=ni(); int c1[]=new int[9]; int c2[]=new int[9]; for(int i=0;i<n;i++){ String s=ns(); if(s.equals("M")) c1[0]++; else if(s.equals("S")) c1[1]++; else if(s.equals("L")) c1[2]++; else if(s.equals("XS")) c1[3]++; else if(s.equals("XL")) c1[4]++; else if(s.equals("XXS")) c1[5]++; else if(s.equals("XXL")) c1[6]++; else if(s.equals("XXXS")) c1[7]++; else if(s.equals("XXXL")) c1[8]++; } for(int i=0;i<n;i++){ String s=ns(); if(s.equals("M")) c2[0]++; else if(s.equals("S")) c2[1]++; else if(s.equals("L")) c2[2]++; else if(s.equals("XS")) c2[3]++; else if(s.equals("XL")) c2[4]++; else if(s.equals("XXS")) c2[5]++; else if(s.equals("XXL")) c2[6]++; else if(s.equals("XXXS")) c2[7]++; else if(s.equals("XXXL")) c2[8]++; } int ans=0; for(int i=0;i<9;i++){ if(c2[i]<c1[i]) ans+=c1[i]-c2[i]; } pw.println(ans); } long M=(long)1e9+7; InputStream is; PrintWriter pw; String INPUT = ""; void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); pw = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); pw.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.lang.Character.*; import static java.lang.Double.*; public class A { Scanner scan = new Scanner(System.in); void solve() { int n = scan.nextInt(); String[]A = new String[n]; String[]B = new String[n]; int res =0; for(int i=0;i<n;i++)A[i]=scan.next(); for(int i=0;i<n;i++)B[i]=scan.next(); for(int i=0;i<A.length;i++) { boolean fnd = false; for(int j=0;j<B.length;j++) { if(A[i].equals(B[j])) { fnd = true; B[j]=""; break; } } if(!fnd)res++; } System.out.println(res); } public static void main(String[] args) { new A().solve(); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
// //package ; import java.io.*; import java.util.*; public class A { public static int sol(String n,String p) { int sol=0; for(int i=0;i<n.length();i++) { if(n.charAt(i)!=p.charAt(i)) sol++; } return sol; } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(); PrintWriter pw = new PrintWriter(System.out); int n=sc.nextInt(); ArrayList<String>p=new ArrayList<>(); ArrayList<String>ne=new ArrayList<>(); for(int i=0;i<n;i++) p.add(sc.nextLine()); for(int i=0;i<n;i++) { String t=sc.nextLine(); if(p.contains(t)) p.remove(t); else ne.add(t); } Collections.sort(p); Collections.sort(ne); int ans=0; for(int i=0;i<ne.size();i++) { ans+=sol(ne.get(i),p.get(i)); } System.out.println(ans); pw.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } boolean hasnext() throws IOException{ return br.ready(); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
/* * UMANG PANCHAL * DAIICT */ import java.util.*; import java.io.*; import java.math.*; import java.util.Comparator; public class Main { private static final Comparator<? super Integer> Comparator = null; static LinkedList<Integer> adj[]; static ArrayList<Integer> adj1[]; static int[] color,visited1; static boolean b[],visited[],possible; static int level[]; static Map<Integer,HashSet<Integer>> s; static int totalnodes,colored; static int count[]; static long sum[]; static int nodes; static long ans=0; static long[] as=new long[10001]; static long c1=0,c2=0; static int[] a,d,k; static int max=100000000; static long MOD = 1000000007,sm=0,m=Long.MIN_VALUE; static boolean[] prime=new boolean[1000005]; static int[] levl; static int[] eat; static int price[]; static int res[],par[],co[]; static int result=0; static int[] root,size,du,dv; static long p=Long.MAX_VALUE; static int start,end,r=0; static boolean[] vis1,vis2; static int to; static HashMap<Pair,Integer> hs; static boolean ns; static Node head; static String st,t; static long n; // --------------------My Code Starts Here---------------------- public static void main(String[] args) throws IOException { in=new InputReader(System.in); w=new PrintWriter(System.out); int n=ni(); HashMap<String,Integer> hm=new HashMap<String,Integer>(); for(int i=0;i<n;i++) { String s=ns(); if(hm.containsKey(s)) hm.put(s,hm.get(s)+1); else hm.put(s,1); } int ans=0; for(int i=0;i<n;i++) { String s=ns(); if(hm.containsKey(s)) { if(hm.get(s)==1) hm.remove(s); else hm.put(s,hm.get(s)-1); } else { ans++; /* if(s.length()==1) { if(s.equals("M")) { if(hm.containsKey("S")) { if(hm.get("S")==1) hm.remove("S"); else hm.put(s,hm.get("S")-1); } else if(hm.containsKey("L")) { if(hm.get("L")==1) hm.remove("L"); else hm.put(s,hm.get("L")-1); } } else if() }*/ } } w.print(ans); w.close(); } // --------------------My Code Ends Here------------------------ public static void dfs(int n,boolean[] vis,int cl,int[] c) { vis[n]=true; if(c[n]!=cl) ns=false; for(int i:adj[n]) { if(!vis[i]) { dfs(i,vis,cl,c); } } } static long pow(long a, long b, long c) { if (b == 0) return 1; long p = pow(a, b / 2, c); p = (p * p) % c; return (b % 2 == 0) ? p : (a * p) % c; } public static long kadane(long[] a,int n) { long max_here=0,max_so_far=-Long.MAX_VALUE; for(int i=0;i<n;i++) { if(max_here<0) max_here=0; max_here+=a[i]; if(max_here>max_so_far) max_so_far=max_here; } return max_so_far; } public static class SegmentTreeRMQ { int st[]; //array to store segment tree // A utility function to get minimum of two numbers int minVal(int x, int y) { return (x < y) ? y : x; } // A utility function to get the middle index from corner // indexes. int getMid(int s, int e) { return s + (e - s) / 2; } /* A recursive function to get the minimum value in a given range of array indexes. The following are parameters for this function. st --> Pointer to segment tree index --> Index of current node in the segment tree. Initially 0 is passed as root is always at index 0 ss & se --> Starting and ending indexes of the segment represented by current node, i.e., st[index] qs & qe --> Starting and ending indexes of query range */ int RMQUtil(int ss, int se, int qs, int qe, int index) { // If segment of this node is a part of given range, then // return the min of the segment if (qs <= ss && qe >= se) return st[index]; // If segment of this node is outside the given range if (se < qs || ss > qe) return Integer.MIN_VALUE; // If a part of this segment overlaps with the given range int mid = getMid(ss, se); return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1), RMQUtil(mid + 1, se, qs, qe, 2 * index + 2)); } // Return minimum of elements in range from index qs (quey // start) to qe (query end). It mainly uses RMQUtil() int RMQ(int n, int qs, int qe) { // Check for erroneous input values if (qs < 0 || qe > n-1 || qs > qe) { System.out.println("Invalid Input"); return 1; } return RMQUtil(0, n - 1, qs, qe, 0); } // A recursive function that constructs Segment Tree for // array[ss..se]. si is index of current node in segment tree st int constructSTUtil(int arr[], int ss, int se, int si) { // If there is one element in array, store it in current // node of segment tree and return if (ss == se) { st[si] = arr[ss]; return arr[ss]; } // If there are more than one elements, then recur for left and // right subtrees and store the minimum of two values in this node int mid = getMid(ss, se); st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1), constructSTUtil(arr, mid + 1, se, si * 2 + 2)); return st[si]; } /* Function to construct segment tree from given array. This function allocates memory for segment tree and calls constructSTUtil() to fill the allocated memory */ void constructST(int arr[], int n) { // Allocate memory for segment tree //Height of segment tree int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); //Maximum size of segment tree int max_size = 2 * (int) Math.pow(2, x) - 1; st = new int[max_size]; // allocate memory // Fill the allocated memory st constructSTUtil(arr, 0, n - 1, 0); } // Driver program to test above functions } public static int fact(int n) { HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); boolean ans=true; while (n%2==0) { if(hm.containsKey(2)) { hm.put(2,hm.get(2)+1); ans=false; } else hm.put(2,1); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n while (n%i == 0) { if(hm.containsKey(i)) {hm.put(i,hm.get(i)+1); ans=false; } else hm.put(i,1); n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n > 2) if(hm.containsKey(n)) {hm.put(n,hm.get(n)+1); ans=false; } else hm.put(n,1); if(ans) return hm.size(); else return -1; } public static int binary_search(long[] a,long k,int l,int r) { while(l<=r) { int mid=(l+r)/2; if(a[mid]>=k) { r=mid-1; } else l=mid+1; } return l; } static class Pair implements Comparable<Pair> { Long x; Long y; Pair(long x,long y) { this.x=x; this.y=y; } public int compareTo(Pair p) { return Long.compare(this.y,p.y); } } public static void removeDuplicates() { Node cur=head; while(cur!=null) { // w.println(1); int k=cur.data; Node p=cur.next; Node pre=cur; while(p!=null) { // w.println(2); if(p.data==k) { pre.next=p.next; p=pre.next; } else { p=p.next; pre=pre.next; } } cur=cur.next; } } public static void insert_front(int x) { Node f=new Node(x); f.next=head; head=f; } public static void insert_mid(Node x,int d) { if(x==null) { w.println("Nothing can be shown"); return; } Node nex=x.next; Node ne=new Node(d); x.next=ne; ne.next=nex; } public static void insert_end(int x) { Node f=new Node(x); Node temp=head; while(temp.next!=null) temp=temp.next; temp.next=f; } public static class Node { int data; Node next; Node(int d) { this.data=d; this.next=null; } } /* * PriorityQueue<Integer> pq = new PriorityQueue<Integer>(new Comparator<Integer>() { public int compare(Integer o1, Integer o2) { return Intege r.compare(o2,o1); } }); * * */ public static void shuffle(long[] a,int n) { Random r=new Random(); for(int i=n-1;i>0;i--) { int j=r.nextInt(i); long t=a[j]; a[j]=a[i]; a[i]=t; } } public static void bfs1(int u) { Queue<Integer> q=new LinkedList(); q.add(u); visited[u]=true; while(!q.isEmpty()) { //w.print(1); int p=q.poll(); for(int i=0;i<adj[p].size();i++) { if(!visited[adj[p].get(i)]) { q.add(adj[p].get(i)); visited[adj[p].get(i)]=true; } levl[adj[p].get(i)]=levl[p]+1; } } } public static void bfs2(int u) { Queue<Integer> q=new LinkedList(); q.add(u); vis2[u]=true; while(!q.isEmpty()) { int p=q.poll(); for(int i=0;i<adj[p].size();i++) { if(!vis2[adj[p].get(i)]) { dv[adj[p].get(i)]=dv[p]+1; q.add(adj[p].get(i)); vis2[adj[p].get(i)]=true; } } } } public static void buildgraph(int n) { adj=new LinkedList[n+1]; visited=new boolean[n]; level=new int[n]; par=new int[n]; for(int i=0;i<=n;i++) { adj[i]=new LinkedList<Integer>(); } } /*public static long kruskal(Pair[] p) { long ans=0; int w=0,x=0,y=0; for(int i=0;i<p.length;i++) { w=p[i].w; x=p[i].x; y=p[i].y; if(root(x)!=root(y)) { ans+=w; union(x,y); } } return ans; }*/ static class npair implements Comparable<npair> { int a,b; npair(int a,int b) { this.a=a; this.b=b; //this.index=index; } public int compareTo(npair o) { // TODO Auto-generated method stub return Integer.compare(this.a,o.a); } } public static int root(int i) { while(root[i]!=i) { root[i]=root[root[i]]; i=root[i]; } return i; } public static void init(int n) { root=new int[n+1]; for(int i=1;i<=n;i++) root[i]=i; } public static void union(int a,int b) { int root_a=root(a); int root_b=root(b); root[root_a]=root_b; // size[root_b]+=size[root_a]; } public static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n%2 == 0 || n%3 == 0) return false; for (long i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } public static String ns() { return in.nextLine(); } public static int ni() { return in.nextInt(); } public static long nl() { return in.nextLong(); } public static int[] na(int n) { int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=ni(); return a; } public static long[] nla(int n) { long[] a=new long[n]; for(int i=0;i<n;i++) a[i]=nl(); return a; } public static void sieve() { int n=prime.length; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*2; i <n; i += p) prime[i] = false; } } } public static String rev(String s) { StringBuilder sb=new StringBuilder(s); sb.reverse(); return sb.toString(); } static long lcm(long a, long b) { return a * (b / gcd(a, b)); } static long gcd(long a, long b) { while (b > 0) { long temp = b; b = a % b; // % is remainder a = temp; } return a; } static InputReader in; static PrintWriter w; static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.HashMap; import java.util.InputMismatchException; import java.io.IOException; import java.util.Map; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author prakharjain */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); String[] a = new String[n]; Map<String, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { a[i] = in.next(); map.merge(a[i], 1, Integer::sum); } String[] b = new String[n]; for (int i = 0; i < n; i++) { b[i] = in.next(); if (map.containsKey(b[i])) { map.put(b[i], map.get(b[i]) - 1); if (map.get(b[i]) <= 0) map.remove(b[i]); } } int ans = 0; for (String s : map.keySet()) { ans += map.get(s); } out.println(ans); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(int i) { writer.println(i); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author prakhar897 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); String arr1[] = new String[n]; String arr2[] = new String[n]; int i, j, count = 0; for (i = 0; i < n; i++) { arr1[i] = in.nextString(); } for (i = 0; i < n; i++) { arr2[i] = in.nextString(); for (j = 0; j < n; j++) { if (arr2[i].equals(arr1[j])) { arr1[j] = ""; count++; break; } } } out.println(n - count); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(int i) { writer.println(i); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.*; import java.io.*; public class cfedu46a{ public static void main(String [] args) throws IOException{ InputReader in = new InputReader("cfedu46a.in"); int [] arr = new int[9]; int [] arr2 = new int[9]; int [] size = {4, 3, 2, 1, 1, 1, 2, 3, 4}; int n = in.nextInt(); for(int i = 0; i < n; i++){ String s = in.next(); switch(s.length()){ case 1: if(s.charAt(0) == 'S') arr[3]++; if(s.charAt(0) == 'M') arr[4]++; if(s.charAt(0) == 'L') arr[5]++; break; default: if(s.charAt(s.length() - 1) == 'S'){ arr[3 - (s.length() - 1)]++; } if(s.charAt(s.length() - 1) == 'L'){ arr[5 + (s.length() - 1)]++; } } } for(int i = 0; i < n; i++){ String s = in.next(); switch(s.length()){ case 1: if(s.charAt(0) == 'S') arr2[3]++; if(s.charAt(0) == 'M') arr2[4]++; if(s.charAt(0) == 'L') arr2[5]++; break; default: if(s.charAt(s.length() - 1) == 'S'){ arr2[3 - (s.length() - 1)]++; } if(s.charAt(s.length() - 1) == 'L'){ arr2[5 + (s.length() - 1)]++; } } } int cnt = 0; for(int i = 0; i < 9; i++){ if(arr[i] == arr2[i]) continue; else{ cnt += (arr2[i] - arr[i] > 0? arr2[i] - arr[i]: 0); } } System.out.println(cnt); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(String s) { try{ reader = new BufferedReader(new FileReader(s), 32768); } catch (Exception e){ reader = new BufferedReader(new InputStreamReader(System.in), 32768); } tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.HashMap; import java.util.InputMismatchException; import java.io.IOException; import java.util.Map; import java.util.Map.Entry; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); HashMap<String, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { String next = in.next(); map.put(next, map.getOrDefault(next, 0) + 1); } int ct = 0; for (int i = 0; i < n; i++) { String next = in.next(); if (map.containsKey(next)) { map.put(next, map.get(next) - 1); if (map.get(next) <= 0) { map.remove(next); } } } for (Map.Entry<String, Integer> entry : map.entrySet()) { ct += entry.getValue(); } out.println(ct); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(int i) { writer.println(i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public int nextInt() { return readInt(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.*; import java.io.*; public class CodehorsesTshirts { /************************ SOLUTION STARTS HERE ************************/ private static void solve() { int n = nextInt(); HashMap<String, Integer> a = new HashMap<>(); HashMap<String, Integer> b = new HashMap<>(); for(int i = 0; i < n; i++) a.merge(nextLine(), 1, Integer::sum); for(int i = 0; i < n; i++) b.merge(nextLine(), 1, Integer::sum); b.forEach((k , v) -> { if(a.containsKey(k)) a.put(k, a.get(k) - Math.min(v , a.get(k))); }); int cost = a.values().stream().reduce(0, Integer::sum); println(cost); } /************************ SOLUTION ENDS HERE ************************/ /************************ TEMPLATE STARTS HERE **********************/ public static void main(String[] args) throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false); st = null; solve(); reader.close(); writer.close(); } static BufferedReader reader; static PrintWriter writer; static StringTokenizer st; static String next() {while(st == null || !st.hasMoreTokens()){try{String line = reader.readLine();if(line == null){return null;} st = new StringTokenizer(line);}catch (Exception e){throw new RuntimeException();}}return st.nextToken();} static String nextLine() {String s=null;try{s=reader.readLine();}catch(IOException e){e.printStackTrace();}return s;} static int nextInt() {return Integer.parseInt(next());} static long nextLong() {return Long.parseLong(next());} static double nextDouble(){return Double.parseDouble(next());} static char nextChar() {return next().charAt(0);} static int[] nextIntArray(int n) {int[] a= new int[n]; int i=0;while(i<n){a[i++]=nextInt();} return a;} static long[] nextLongArray(int n) {long[]a= new long[n]; int i=0;while(i<n){a[i++]=nextLong();} return a;} static int[] nextIntArrayOneBased(int n) {int[] a= new int[n+1]; int i=1;while(i<=n){a[i++]=nextInt();} return a;} static long[] nextLongArrayOneBased(int n){long[]a= new long[n+1];int i=1;while(i<=n){a[i++]=nextLong();}return a;} static void print(Object o) { writer.print(o); } static void println(Object o){ writer.println(o);} /************************ TEMPLATE ENDS HERE ************************/ }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; public class A implements Runnable { public void run() { long startTime = System.nanoTime(); int n = nextInt(); String[] all = new String[9]; all[0] = "M"; for (int i = 0; i < 4; i++) { String s = ""; for (int j = 0; j < i; j++) { s += "X"; } all[2 * i + 1] = s + "S"; all[2 * i + 2] = s + "L"; } Map<String, Integer> map1 = new HashMap<>(); Map<String, Integer> map2 = new HashMap<>(); for (String s : all) { map1.put(s, 0); map2.put(s, 0); } for (int i = 0; i < n; i++) { String s = nextToken(); map1.put(s, map1.get(s) + 1); } for (int i = 0; i < n; i++) { String s = nextToken(); map2.put(s, map2.get(s) + 1); } int res = 0; for (String s : all) { int a = map1.get(s); int b = map2.get(s); if (a > b) { res += a - b; } } println(res); if (fileIOMode) { System.out.println((System.nanoTime() - startTime) / 1e9); } out.close(); } //----------------------------------------------------------------------------------- private static boolean fileIOMode; private static BufferedReader in; private static PrintWriter out; private static StringTokenizer tokenizer; public static void main(String[] args) throws Exception { fileIOMode = args.length > 0 && args[0].equals("!"); if (fileIOMode) { in = new BufferedReader(new FileReader("a.in")); out = new PrintWriter("a.out"); } else { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } tokenizer = new StringTokenizer(""); new Thread(new A()).start(); } private static String nextLine() { try { return in.readLine(); } catch (IOException e) { return null; } } private static String nextToken() { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(nextLine()); } return tokenizer.nextToken(); } private static int nextInt() { return Integer.parseInt(nextToken()); } private static long nextLong() { return Long.parseLong(nextToken()); } private static double nextDouble() { return Double.parseDouble(nextToken()); } private static BigInteger nextBigInteger() { return new BigInteger(nextToken()); } private static void print(Object o) { if (fileIOMode) { System.out.print(o); } out.print(o); } private static void println(Object o) { if (fileIOMode) { System.out.println(o); } out.println(o); } private static void printf(String s, Object... o) { if (fileIOMode) { System.out.printf(s, o); } out.printf(s, o); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES
import java.util.*; import java.io.*; public final class CF { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList<String> a = new ArrayList<>(); for(int i = 0; i<n; i++) a.add(sc.next()); int count = 0; for(int i = 0; i<n; i++) { String b = sc.next(); int idx = a.indexOf(b); if(idx!=-1) a.remove(idx); else count++; } System.out.println(count); } }
linear
1000_A. Codehorses T-shirts
CODEFORCES