src
stringlengths
95
64.6k
complexity
stringclasses
7 values
problem
stringlengths
6
50
from
stringclasses
1 value
import java.awt.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.util.List; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(), m = scanner.nextInt(); int[] vertical = new int[n]; for (int i = 0; i < n; i++) { vertical[i] = scanner.nextInt(); } Arrays.sort(vertical); ArrayList<Integer> horisontal = new ArrayList<>(); int amount = 0; for (int i = 0; i < m; i++) { int x1 = scanner.nextInt(), x2 = scanner.nextInt(), y = scanner.nextInt(); if (x1 == 1) { amount++; horisontal.add(x2); } } Collections.sort(horisontal); if (amount == 0) { System.out.println(0); return; } int minVal = amount, horSize = horisontal.size(), verLen = vertical.length; int h = 0, v = 0; for (; v < verLen && h < horSize; ) { while (h < horSize && horisontal.get(h) < vertical[v]){ h++; amount--; } minVal = Math.min(minVal, amount + v); while (h < horSize && v < verLen && horisontal.get(h) >= vertical[v]){ minVal = Math.min(minVal, amount + v); v++; } } if(horisontal.get(horSize - 1) < 1E9){ minVal = Math.min(minVal, v); } System.out.println(minVal); } }
nlogn
1075_C. The Tower is Going Home
CODEFORCES
import java.io.*; import java.util.*; public class A4 { public BufferedReader input; public PrintWriter output; public StringTokenizer stoken = new StringTokenizer(""); public static void main(String[] args) throws IOException { new A4(); } A4() throws IOException { input = new BufferedReader(new InputStreamReader(System.in)); output = new PrintWriter(System.out); run(); input.close(); output.close(); } private void run() throws IOException { int n = Math.toIntExact(nextLong()); int m = Math.toIntExact(nextLong()); int[] coor = new int[n + 1]; int[] ss = new int[n + 1]; for (int i = 0; i < n; i++) { coor[i] = Math.toIntExact(nextLong()); } coor[n] = 1000000000; Arrays.sort(coor); for (int i = 0; i < m; i++) { long x1 = nextLong(); long x2 = nextLong(); nextLong(); if (x1 == 1 && x2 >= coor[0]) { int l = 0; int r = n + 1; while (r - l > 1) { int mi = (r + l) / 2; if (coor[mi] > x2) { r = mi; } else { l = mi; } } ss[l]++; } } long[] ans = new long[n + 1]; ans[n] = ss[n] + n; long min = ans[n]; for (int i = n - 1; i > -1; i--) { ans[i] = ans[i + 1] - 1 + ss[i]; if (ans[i] < min) { min = ans[i]; } } System.out.println(min); } private Long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextString()); } private String nextString() throws IOException { while (!stoken.hasMoreTokens()) { String st = input.readLine(); stoken = new StringTokenizer(st); } return stoken.nextToken(); } }
nlogn
1075_C. The Tower is Going Home
CODEFORCES
import java.util.*; import java.io.*; import java.math.*; public class loser { static class InputReader { public BufferedReader br; public StringTokenizer token; public InputReader(InputStream stream) { br=new BufferedReader(new InputStreamReader(stream),32768); token=null; } public String next() { while(token==null || !token.hasMoreTokens()) { try { token=new StringTokenizer(br.readLine()); } catch(IOException e) { throw new RuntimeException(e); } } return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } static class card{ int l; int r; public card(int ch,int i) { this.l=ch; this.r=i; } } static class sort implements Comparator<card> { public int compare(card o1,card o2) { if(o1.l!=o2.l) return (int)(o1.l-o2.l); else return (int)(o1.r-o2.r); } } static void shuffle(long a[]) { List<Long> l=new ArrayList<>(); for(int i=0;i<a.length;i++) l.add(a[i]); Collections.shuffle(l); for(int i=0;i<a.length;i++) a[i]=l.get(i); } /*static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } static int ans1=Integer.MAX_VALUE,ans2=Integer.MAX_VALUE,ans3=Integer.MAX_VALUE,ans4=Integer.MAX_VALUE; static boolean v[]=new boolean[101]; static void dfs(Integer so,Set<Integer> s[]){ if(!v[so.intValue()]) { v[so]=true; for(Integer h:s[so.intValue()]) { if(!v[h.intValue()]) dfs(h,s); } } } static class Print{ public PrintWriter out; Print(OutputStream o) { out=new PrintWriter(o); } } static int CeilIndex(int A[], int l, int r, int key) { while (r - l > 1) { int m = l + (r - l) / 2; if (A[m] >= key) r = m; else l = m; } return r; } static int LongestIncreasingSubsequenceLength(int A[], int size) { // Add boundary case, when array size is one int[] tailTable = new int[size]; int len; // always points empty slot tailTable[0] = A[0]; len = 1; for (int i = 1; i < size; i++) { if (A[i] < tailTable[0]) // new smallest value tailTable[0] = A[i]; else if (A[i] > tailTable[len - 1]) // A[i] wants to extend largest subsequence tailTable[len++] = A[i]; else // A[i] wants to be current end candidate of an existing // subsequence. It will replace ceil value in tailTable tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]; } return len; }*/ /*static int binary(int n) { int s=1; while(n>0) { s=s<<1; n--; } return s-1; } static StringBuilder bin(int i,int n) { StringBuilder s=new StringBuilder(); while(i>0) { s.append(i%2); i=i/2; } while(s.length()!=n) { s.append(0); } return s.reverse(); }*/ static boolean valid(int i,int j,int n,int m) { if(i<n && i>=0 && j<m && j>=0) return true; else return false; } public static void main(String[] args) { InputReader sc=new InputReader(System.in); int n=sc.nextInt(); int s=sc.nextInt(); card c[]=new card[n]; for(int i=0;i<n;i++) { int x=sc.nextInt(); int y=sc.nextInt(); c[i]=new card(x,y); } Arrays.sort(c,new sort()); int time=0; for(int i=n-1;i>=0;i--) { time+=s-c[i].l; if((c[i].r-time)>0) time+=c[i].r-time; s=c[i].l; } if(c[0].l!=0) time+=c[0].l; System.out.println(time); } }
nlogn
608_A. Saitama Destroys Hotel
CODEFORCES
import javafx.util.Pair; import java.io.*; import java.util.*; public class Beacon8 { public static void main(String[] args) throws IOException { // int[] arr = {1, 3, 7, 18}; // int bIndex = Arrays.binarySearch(arr, 4); // System.out.println(bIndex); Scanner scan = new Scanner(System.in); int n = scan.nextInt(); Map<Integer, Integer> beacons = new TreeMap<>(); for (int i = 0; i < n; i++) { int index = scan.nextInt(); int power = scan.nextInt(); beacons.put(index, power); } int[] indicesArr = new int[n]; int arrInd = 0; for (int index : beacons.keySet()) { indicesArr[arrInd] = index; arrInd++; } // Integer[] indicesArr = ((Integer[])beacons.keySet().toArray()); int[] nDestroys = new int[n]; for (int i = 0; i < n; i++) { int bIndex = Arrays.binarySearch(indicesArr, indicesArr[i] - beacons.get(indicesArr[i])); if (bIndex < 0) bIndex = -(bIndex + 1); nDestroys[i] = i - bIndex; } int[] totalBeacons = new int[n]; int maxBeacons = 1; totalBeacons[0] = 1; for (int i = 1; i < n; i++) { if (nDestroys[i] == 0) totalBeacons[i] = totalBeacons[i - 1] + 1; else { if ((i - nDestroys[i] - 1) >= 0) totalBeacons[i] = totalBeacons[i - nDestroys[i] - 1] + 1; else totalBeacons[i] = 1; } // totalBeacons[i] = totalBeacons[i - nDestroys[i]] + 1; //totalBeacons[i] = i - nDestroys[i] + totalBeacons[i - nDestroys[i]] + 1; if(totalBeacons[i] > maxBeacons) maxBeacons = totalBeacons[i]; } // System.out.println("\ntotalBeacons array"); // for (int i = 0; i < n; i++) { // System.out.print(totalBeacons[i] + " "); // } // if (maxBeacons == -1) // System.out.println(n); System.out.println(n - maxBeacons); } }
nlogn
608_C. Chain Reaction
CODEFORCES
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.StringTokenizer; public class ChainReaction implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); private class Beacon implements Comparable<Beacon> { private int position, range, score; private Beacon(int position, int range) { this.position = position; this.range = range; } public void setScore(int score) { this.score = score; } @Override public int compareTo(Beacon o) { return Integer.compare(this.position, o.position); } } public void solve() { int n = in.ni(); if (n == 1) { out.println(0); return; } beacons = new ArrayList<>(); for (int i = 0; i < n; i++) { beacons.add(new Beacon(in.ni(), in.ni())); } beacons.sort(Comparator.naturalOrder()); for (int i = 1; i < n; i++) { int left = 0, right = i - 1, position = beacons.get(i).position, range = beacons.get(i).range; int leftmost = i; while (left <= right) { int mid = left + (right - left) / 2; if (position - range <= beacons.get(mid).position) { leftmost = Math.min(leftmost, mid); right = mid - 1; } else { left = mid + 1; } } beacons.get(i).setScore(i - leftmost); } dp = new Integer[n]; int ans = Integer.MAX_VALUE; for (int i = n - 1; i >= 0; i--) { ans = Math.min(n - 1 - i + recurse(i), ans); } out.println(ans); } private List<Beacon> beacons; private Integer[] dp; private int recurse(int idx) { if (idx <= 0) return 0; if (dp[idx] != null) return dp[idx]; int destroyed = beacons.get(idx).score; int ans = destroyed + recurse(idx - destroyed - 1); return dp[idx] = ans; } @Override public void close() throws IOException { in.close(); out.close(); } static class InputReader { public BufferedReader reader; public 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 ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (ChainReaction instance = new ChainReaction()) { instance.solve(); } } }
nlogn
608_C. Chain Reaction
CODEFORCES
import java.util.*; import java.io.*; import static java.lang.System.in; public class Main { public static void main(String[] args)throws IOException{ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] point = new int[n][]; for(int i=0;i<n;i++) point[i] = new int[]{sc.nextInt(),sc.nextInt()}; Arrays.sort(point,(a,b)->((a[0]-a[1])-(b[0]-b[1]))); TreeMap<Integer,Integer> tm = new TreeMap<>(); int ans = 0; for(int i=n-1;i>=0;i--){ int x = point[i][0], w = point[i][1]; Map.Entry<Integer,Integer> cur = tm.ceilingEntry(x+w); int curRes; if(cur==null) curRes = 1; else curRes = cur.getValue()+1; ans = Math.max(ans,curRes); Map.Entry<Integer,Integer> upper = tm.ceilingEntry(x-w); if(upper==null||upper.getValue()<curRes) tm.put(x-w,curRes); //Integer key = tm. } System.out.println(ans); } }
nlogn
528_B. Clique Problem
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; 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; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); TaskD.Pair[] p = new TaskD.Pair[n]; for (int i = 0; i < n; ++i) { p[i] = new TaskD.Pair(in.nextLong(), in.nextLong()); } Arrays.sort(p); int last = 0; int ans = 1; for (int i = 1; i < n; ++i) { if (p[i].x - p[i].w >= p[last].x + p[last].w) { last = i; ++ans; } } out.println(ans); } static class Pair implements Comparable<TaskD.Pair> { long x; long w; public Pair(long x, long w) { this.x = x; this.w = w; } public int compareTo(TaskD.Pair o) { return Long.compare(x + w, o.x + o.w); } public String toString() { return x + " " + w; } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
nlogn
528_B. Clique Problem
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.InputMismatchException; import java.io.OutputStreamWriter; import java.util.NoSuchElementException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Iterator; import java.io.BufferedWriter; import java.io.IOException; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author aryssoncf */ 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); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, InputReader in, OutputWriter out) { try { int n = in.readInt(); int[] x = new int[n], w = new int[n]; in.readIntArrays(x, w); int[] begin = new int[n], end = new int[n]; Arrays.setAll(begin, i -> x[i] - w[i]); Arrays.setAll(end, i -> x[i] + w[i]); int m = ArrayUtils.compress(begin, end).length; int[] dp = new int[m + 1], order = ArrayUtils.order(end); int idx = 0; for (int i = 0; i < m; i++) { if (i > 0) { dp[i] = dp[i - 1]; } while (idx < n && end[order[idx]] == i) { dp[i] = Math.max(dp[i], dp[begin[order[idx]]] + 1); idx++; } } int res = dp[m - 1]; out.printLine(res); } catch (Exception e) { e.printStackTrace(); } } } static class Sorter { private static final int INSERTION_THRESHOLD = 16; private Sorter() { } public static void sort(IntList list, IntComparator comparator) { quickSort(list, 0, list.size() - 1, (Integer.bitCount(Integer.highestOneBit(list.size()) - 1) * 5) >> 1, comparator); } private static void quickSort(IntList list, int from, int to, int remaining, IntComparator comparator) { if (to - from < INSERTION_THRESHOLD) { insertionSort(list, from, to, comparator); return; } if (remaining == 0) { heapSort(list, from, to, comparator); return; } remaining--; int pivotIndex = (from + to) >> 1; int pivot = list.get(pivotIndex); list.swap(pivotIndex, to); int storeIndex = from; int equalIndex = to; for (int i = from; i < equalIndex; i++) { int value = comparator.compare(list.get(i), pivot); if (value < 0) { list.swap(storeIndex++, i); } else if (value == 0) { list.swap(--equalIndex, i--); } } quickSort(list, from, storeIndex - 1, remaining, comparator); for (int i = equalIndex; i <= to; i++) { list.swap(storeIndex++, i); } quickSort(list, storeIndex, to, remaining, comparator); } private static void heapSort(IntList list, int from, int to, IntComparator comparator) { for (int i = (to + from - 1) >> 1; i >= from; i--) { siftDown(list, i, to, comparator, from); } for (int i = to; i > from; i--) { list.swap(from, i); siftDown(list, from, i - 1, comparator, from); } } private static void siftDown(IntList list, int start, int end, IntComparator comparator, int delta) { int value = list.get(start); while (true) { int child = ((start - delta) << 1) + 1 + delta; if (child > end) { return; } int childValue = list.get(child); if (child + 1 <= end) { int otherValue = list.get(child + 1); if (comparator.compare(otherValue, childValue) > 0) { child++; childValue = otherValue; } } if (comparator.compare(value, childValue) >= 0) { return; } list.swap(start, child); start = child; } } private static void insertionSort(IntList list, int from, int to, IntComparator comparator) { for (int i = from + 1; i <= to; i++) { int value = list.get(i); for (int j = i - 1; j >= from; j--) { if (comparator.compare(list.get(j), value) <= 0) { break; } list.swap(j, j + 1); } } } } static interface IntList extends IntReversableCollection { public abstract int get(int index); public abstract void set(int index, int value); public abstract void addAt(int index, int value); public abstract void removeAt(int index); default public void swap(int first, int second) { if (first == second) { return; } int temp = get(first); set(first, get(second)); set(second, temp); } default public IntIterator intIterator() { return new IntIterator() { private int at; private boolean removed; public int value() { if (removed) { throw new IllegalStateException(); } return get(at); } public boolean advance() { at++; removed = false; return isValid(); } public boolean isValid() { return !removed && at < size(); } public void remove() { removeAt(at); at--; removed = true; } }; } default public void add(int value) { addAt(size(), value); } default public IntList sort(IntComparator comparator) { Sorter.sort(this, comparator); return this; } default IntList unique() { int last = Integer.MIN_VALUE; IntList result = new IntArrayList(); int size = size(); for (int i = 0; i < size; i++) { int current = get(i); if (current != last) { result.add(current); last = current; } } return result; } default public IntList subList(final int from, final int to) { return new IntList() { private final int shift; private final int size; { if (from < 0 || from > to || to > IntList.this.size()) { throw new IndexOutOfBoundsException("from = " + from + ", to = " + to + ", size = " + size()); } shift = from; size = to - from; } public int size() { return size; } public int get(int at) { if (at < 0 || at >= size) { throw new IndexOutOfBoundsException("at = " + at + ", size = " + size()); } return IntList.this.get(at + shift); } public void addAt(int index, int value) { throw new UnsupportedOperationException(); } public void removeAt(int index) { throw new UnsupportedOperationException(); } public void set(int at, int value) { if (at < 0 || at >= size) { throw new IndexOutOfBoundsException("at = " + at + ", size = " + size()); } IntList.this.set(at + shift, value); } public IntList compute() { return new IntArrayList(this); } }; } } static interface IntComparator { IntComparator DEFAULT = Integer::compare; int compare(int first, int second); } static class Range { public static IntList range(int from, int to) { int[] result = new int[Math.abs(from - to)]; int current = from; if (from <= to) { for (int i = 0; i < result.length; i++) { result[i] = current++; } } else { for (int i = 0; i < result.length; i++) { result[i] = current--; } } return new IntArray(result); } } static interface IntReversableCollection extends IntCollection { } 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 printLine(int i) { writer.println(i); } } static interface IntStream extends Iterable<Integer>, Comparable<IntStream> { IntIterator intIterator(); default Iterator<Integer> iterator() { return new Iterator<Integer>() { private IntIterator it = intIterator(); public boolean hasNext() { return it.isValid(); } public Integer next() { int result = it.value(); it.advance(); return result; } }; } default int compareTo(IntStream c) { IntIterator it = intIterator(); IntIterator jt = c.intIterator(); while (it.isValid() && jt.isValid()) { int i = it.value(); int j = jt.value(); if (i < j) { return -1; } else if (i > j) { return 1; } it.advance(); jt.advance(); } if (it.isValid()) { return 1; } if (jt.isValid()) { return -1; } return 0; } } 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 void readIntArrays(int[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) { arrays[j][i] = readInt(); } } } 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 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 { boolean isSpaceChar(int ch); } } static interface IntCollection extends IntStream { public int size(); default public void add(int value) { throw new UnsupportedOperationException(); } default public int[] toArray() { int size = size(); int[] array = new int[size]; int i = 0; for (IntIterator it = intIterator(); it.isValid(); it.advance()) { array[i++] = it.value(); } return array; } default public IntCollection addAll(IntStream values) { for (IntIterator it = values.intIterator(); it.isValid(); it.advance()) { add(it.value()); } return this; } } static class IntArray extends IntAbstractStream implements IntList { private int[] data; public IntArray(int[] arr) { data = arr; } public int size() { return data.length; } public int get(int at) { return data[at]; } public void addAt(int index, int value) { throw new UnsupportedOperationException(); } public void removeAt(int index) { throw new UnsupportedOperationException(); } public void set(int index, int value) { data[index] = value; } } static class ArrayUtils { public static int[] range(int from, int to) { return Range.range(from, to).toArray(); } public static int[] createOrder(int size) { return range(0, size); } public static int[] sort(int[] array, IntComparator comparator) { return sort(array, 0, array.length, comparator); } public static int[] sort(int[] array, int from, int to, IntComparator comparator) { if (from == 0 && to == array.length) { new IntArray(array).sort(comparator); } else { new IntArray(array).subList(from, to).sort(comparator); } return array; } public static int[] order(final int[] array) { return sort(createOrder(array.length), (first, second) -> Integer.compare(array[first], array[second])); } public static int[] unique(int[] array) { return new IntArray(array).unique().toArray(); } public static int[] compress(int[]... arrays) { int totalLength = 0; for (int[] array : arrays) { totalLength += array.length; } int[] all = new int[totalLength]; int delta = 0; for (int[] array : arrays) { System.arraycopy(array, 0, all, delta, array.length); delta += array.length; } sort(all, IntComparator.DEFAULT); all = unique(all); for (int[] array : arrays) { for (int i = 0; i < array.length; i++) { array[i] = Arrays.binarySearch(all, array[i]); } } return all; } } static interface IntIterator { public int value() throws NoSuchElementException; public boolean advance(); public boolean isValid(); } static class IntArrayList extends IntAbstractStream implements IntList { private int size; private int[] data; public IntArrayList() { this(3); } public IntArrayList(int capacity) { data = new int[capacity]; } public IntArrayList(IntCollection c) { this(c.size()); addAll(c); } public IntArrayList(IntStream c) { this(); if (c instanceof IntCollection) { ensureCapacity(((IntCollection) c).size()); } addAll(c); } public IntArrayList(IntArrayList c) { size = c.size(); data = c.data.clone(); } public IntArrayList(int[] arr) { size = arr.length; data = arr.clone(); } public int size() { return size; } public int get(int at) { if (at >= size) { throw new IndexOutOfBoundsException("at = " + at + ", size = " + size); } return data[at]; } private void ensureCapacity(int capacity) { if (data.length >= capacity) { return; } capacity = Math.max(2 * data.length, capacity); data = Arrays.copyOf(data, capacity); } public void addAt(int index, int value) { ensureCapacity(size + 1); if (index > size || index < 0) { throw new IndexOutOfBoundsException("at = " + index + ", size = " + size); } if (index != size) { System.arraycopy(data, index, data, index + 1, size - index); } data[index] = value; size++; } public void removeAt(int index) { if (index >= size || index < 0) { throw new IndexOutOfBoundsException("at = " + index + ", size = " + size); } if (index != size - 1) { System.arraycopy(data, index + 1, data, index, size - index - 1); } size--; } public void set(int index, int value) { if (index >= size) { throw new IndexOutOfBoundsException("at = " + index + ", size = " + size); } data[index] = value; } public int[] toArray() { return Arrays.copyOf(data, size); } } static abstract class IntAbstractStream implements IntStream { public String toString() { StringBuilder builder = new StringBuilder(); boolean first = true; for (IntIterator it = intIterator(); it.isValid(); it.advance()) { if (first) { first = false; } else { builder.append(' '); } builder.append(it.value()); } return builder.toString(); } public boolean equals(Object o) { if (!(o instanceof IntStream)) { return false; } IntStream c = (IntStream) o; IntIterator it = intIterator(); IntIterator jt = c.intIterator(); while (it.isValid() && jt.isValid()) { if (it.value() != jt.value()) { return false; } it.advance(); jt.advance(); } return !it.isValid() && !jt.isValid(); } public int hashCode() { int result = 0; for (IntIterator it = intIterator(); it.isValid(); it.advance()) { result *= 31; result += it.value(); } return result; } } }
nlogn
528_B. Clique Problem
CODEFORCES
import java.util.*; public class A{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); TreeSet<Integer> v = new TreeSet<Integer>(); for(int i=0;i<n;i++) v.add(sc.nextInt()); Iterator<Integer> it = v.iterator(); it.next(); it.remove(); System.out.println(v.isEmpty() ? "NO" : v.iterator().next()); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.Scanner; public class Seq2 { static void metod() { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); int min = a[0]; for (int i = 1; i < a.length; i++) { if (a[i] < min) min = a[i]; } int min2 = min; boolean t = false; for (int i = 0; i < a.length; i++) { if (a[i] != min) { if (!t) { min2 = a[i]; t = true; } else { if (min2 > a[i]) min2 = a[i]; } } } System.out.println((min == min2) ? "NO" : min2); } public static void main(String[] args) { Seq2.metod(); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main { private static Parser in; private static PrintWriter out; public static void main(String[] args) { in = new Parser(System.in); out = new PrintWriter(System.out); int n= in.nextInt(); int min = 101; boolean b = false; int pmin = 101; int t= 0 ; for(int i=0; i<n; i++){ t = in.nextInt(); if (t<min){if(min != pmin)b=true; if(b) pmin = min; min = t; continue;} if (t>min && t<pmin){b=true; pmin = t; continue;} if (t>min && !b){b=true; pmin = t; continue;} //if (b){if(t<pmin){pmin = t; continue;}} //out.print(min); //out.print(pmin); } if (b) System.out.println(pmin); else System.out.println("NO"); //out.flush(); } } class Parser { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Parser(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextString(int size) { byte[] ch = new byte[size]; int point = 0; try { byte c = read(); while (c == ' ' || c == '\n' || c=='\r') c = read(); while (c != ' ' && c != '\n' && c!='\r') { ch[point++] = c; c = read(); } } catch (Exception e) {} return new String(ch,0,point); } public int nextInt() { int ret = 0; boolean neg; try { byte c = read(); while (c <= ' ') c = read(); neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; } catch (Exception e) {} return ret; } public long nextLong() { long ret = 0; boolean neg; try { byte c = read(); while (c <= ' ') c = read(); neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; } catch (Exception e) {} return ret; } private void fillBuffer() { try { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); } catch (Exception e) {} if (bytesRead == -1) buffer[0] = -1; } private byte read() { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; public class A { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] a = new int[n]; for(int i=0;i < n;i++) a[i] = scan.nextInt(); Arrays.sort(a); int min = a[0]; for(int i=1;i < n;i++) { if(a[i] > min) { System.out.println(a[i]); return; } } System.out.println("NO"); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeSet; public class A { public static void main(String[] args) throws IOException { new A().run(); } BufferedReader br; StringTokenizer st = new StringTokenizer(""); private void run() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); int n = nextInt(); Set<Integer> set = new TreeSet<Integer>(); for (int i = 0; i < n; i++) { set.add(nextInt()); } set.remove(set.iterator().next()); PrintWriter pw = new PrintWriter(System.out); if (set.isEmpty()) { pw.println("NO"); } else { pw.println(set.iterator().next()); } pw.close(); } int nextInt() throws IOException { while (!st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return Integer.parseInt(st.nextToken()); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; import java.io.*; public class Main{ BufferedReader in; StringTokenizer str = null; PrintWriter out; private String next() throws Exception{ if (str == null || !str.hasMoreTokens()) 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()); } public void run() throws Exception{ in = new BufferedReader(new InputStreamReader(System.in));//new FileReader(); out = new PrintWriter(System.out);//new File(); int n = nextInt(); HashSet<Integer> hs = new HashSet<Integer>(); for(int i=0;i<n;i++) hs.add(nextInt()); if (hs.size() == 1){ out.println("NO"); out.close(); return; } int a[] = new int[hs.size()]; int yk = 0; for(int i:hs) a[yk++] = i; Arrays.sort(a); out.println(a[1]); out.close(); } public static void main(String args[]) throws Exception{ new Main().run(); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); TreeSet<Integer> set = new TreeSet<Integer>(); for(int i=0;i<n;i++){ set.add(sc.nextInt()); } if(set.size() >= 2) System.out.println(set.toArray()[1]); else System.out.println("NO"); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.InputMismatchException; /** * @author Egor Kulikov ([email protected]) * Created on 14.03.2010 */ public class TaskA implements Runnable { private InputReader in; private PrintWriter out; public static void main(String[] args) { // new Thread(new Template()).start(); new TaskA().run(); } public TaskA() { // String id = getClass().getName().toLowerCase(); // try { // System.setIn(new FileInputStream(id + ".in")); // System.setOut(new PrintStream(new FileOutputStream(id + ".out"))); // System.setIn(new FileInputStream("input.txt")); // System.setOut(new PrintStream(new FileOutputStream("output.txt"))); // } catch (FileNotFoundException e) { // throw new RuntimeException(); // } in = new InputReader(System.in); out = new PrintWriter(System.out); } public void run() { // int numTests = in.readInt(); // for (int testNumber = 0; testNumber < numTests; testNumber++) { // out.print("Case " + (testNumber + 1) + ": "); // } int n = in.readInt(); int min = 101; int[] a = new int[n]; for (int i = 0; i < n; i++) min = Math.min(min, a[i] = in.readInt()); int ans = 101; for (int i = 0; i < n; i++) { if (a[i] > min) ans = Math.min(ans, a[i]); } if (ans == 101) out.println("NO"); else out.println(ans); out.close(); } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1000]; private int curChar, numChars; public InputReader(InputStream stream) { this.stream = stream; } 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++]; } 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 long readLong() { 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 String readString() { 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; } private String readLine0() { StringBuffer buf = new StringBuffer(); int c = read(); while (c != '\n' && c != -1) { buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) return readLine(); else return readLine0(); } public BigInteger readBigInteger() { try { return new BigInteger(readString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double readDouble() { 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 < '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 < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class VtoraiaStat implements Runnable { boolean isLocalMode = false; private void doJob() throws Exception { int n = nextInt(); int[] r = new int[n]; for(int i =0;i<n;i++){ r[i]=nextInt(); } Arrays.sort(r); int m = r[0]; for(int i=0;i<n;i++){ if(r[i]!=m){ writer.write(""+r[i]); return; } } writer.write("NO"); } public static void main(String[] args) { new VtoraiaStat().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(getReader()); tokenizer = null; writer = new PrintWriter(System.out); //do job doJob(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } 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(); } public Reader getReader() throws FileNotFoundException { if (isLocalMode) { return new FileReader("input.txt"); } else { return new InputStreamReader(System.in); } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.*; public class Main { public static void main(String[] args) throws Exception { // TODO code application logic here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int iTotTerm, i, j, iSml = 0, iPos = 0; iTotTerm = Integer.parseInt(br.readLine()); String seq[]; seq = br.readLine().split(" "); int iSeq[] = new int[iTotTerm]; for (i = 0; i < iTotTerm; i++) { iSeq[i] = Integer.parseInt(seq[i]); } for (i = 0; i < iTotTerm; i++) { iSml = iSeq[i]; iPos = i; for (j = i; j < iTotTerm; j++) { if (iSeq[j] < iSml) { iSml = iSeq[j]; iPos = j; } } iSeq[iPos] = iSeq[i]; iSeq[i] = iSml; if (i != 0 && iSeq[i - 1] != iSeq[i]) { break; } } if (iSml != iSeq[0]) { System.out.print(iSml); } else { System.out.print("NO"); } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.*; import java.util.*; public class Main{ public static void main(String []args){ Scanner cin = new Scanner( System.in ); int n = cin.nextInt(); int [] num = new int [ n ]; for (int i=0; i<n; i++) num[i] = cin.nextInt(); Arrays.sort( num ); int i = 0; while ( i < n ){ if ( num[i] != num[0] ) break; i++; } if ( i == n ) System.out.println("NO"); else System.out.println(num[i]); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; import java.lang.*; import java.io.*; public class Blah { public static void main(String args[]) { Scanner c = new Scanner(System.in); String number = c.nextLine(); int i = Integer.parseInt(number); if (i == 1) { System.out.println("NO"); return; } String line = c.nextLine(); String[] arr = line.split(" "); int[] array = new int[i]; for (int j = 0; j < i; j++) { array[j] = Integer.parseInt(arr[j]); } int min = array[0]; int second = 0; boolean thing = false; for (int j = 0; j < i; j++) { if (!thing && array[j] > min) { second = array[j]; thing = true; } if (array[j] < min) { second = min; min = array[j]; thing = true; } else if (thing && array[j] > min && array[j] < second) second = array[j]; } if (!thing) System.out.println("NO"); else System.out.println(second); return; } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.Scanner; public class prob1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); if(n == 1) { // throw new RuntimeException(); int m = input.nextInt(); System.out.println("NO"); // System.out.println(input.next()); return; } int[] num = new int[n]; boolean flag = false; for(int i = 0; i < n; i++) { num[i] = input.nextInt(); if(num[i] != num[0]) flag = true; } if(!flag) { System.out.println("NO"); return; } int min = Integer.MAX_VALUE; for(int i = 0; i < n; i++) if(num[i] < min) min = num[i]; int min2 = Integer.MAX_VALUE; for(int i = 0; i < n; i++) if(num[i] <= min2 && num[i] > min) min2 = num[i]; System.out.println(min2); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
//package codeforces.br22; import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * User: Kandy * Date: 29.06.2010 * Time: 21:07:24 */ public class ProblemA { public void solve() { boolean oj = true; try { Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("A.in"); Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("A.out"); BufferedReader br = new BufferedReader(reader); StreamTokenizer st = new StreamTokenizer(reader); PrintWriter out = new PrintWriter(writer); MyTokenizer tok = new MyTokenizer(br.readLine()); int n = (int)tok.getNum(); tok =new MyTokenizer(br.readLine()); List<Integer> a = new ArrayList<Integer>(); for(int i=0;i<n;i++) { int r = (int)tok.getNum(); if (!a.contains(r)) { a.add(r); } } Collections.sort(a); if (a.size() < 2) { out.printf("NO"); } else { out.printf("%d", a.get(1)); } br.close(); out.close(); reader.close(); writer.close(); } catch (Exception ex) { ex.printStackTrace(); } finally { } } public static void main(String[] args) { ProblemA f = new ProblemA(); f.solve(); } private class MyTokenizer { private String s; private int cur; public MyTokenizer(String s) { this.s = s; cur = 0; } public void skip() { while (cur < s.length() && (s.charAt(cur) == ' ' || s.charAt(cur) == '\n')) { cur++; } } public double getNum() { skip(); String snum = ""; while (cur < s.length() && (s.charAt(cur) >= '0' && s.charAt(cur) <= '9' || s.charAt(cur) == '.' || s.charAt(cur) == '-')) { snum += s.charAt(cur); cur++; } return Double.valueOf(snum); } public String getString() { skip(); String s2 = ""; while (cur < s.length() && (((s.charAt(cur) >= 'a' && s.charAt(cur) <= 'z')) || ((s.charAt(cur) >= 'A' && s.charAt(cur) <= 'Z')))) { s2 += s.charAt(cur); cur++; } return s2; } public char getCurrentChar() throws Exception { if (cur < s.length()) return s.charAt(cur); else throw new Exception("Current character out of string length"); } public void moveNextChar() { if (cur < s.length()) cur++; } public boolean isFinished() { return cur >= s.length(); } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; public class Main2 { public static void main(String[] args) throws Exception { new Main2().run(); } public void solve() throws Exception { n = nextInt(); int a[]= new int[n], pos = 1; for(int i=0; i<n; i++) a[i] = nextInt(); Arrays.sort(a); if(n == 1){ out.println("NO"); return; } boolean has = false; for(; pos<n; pos++){ if(a[pos] != a[0]){ has = true; break; } } if(!has){ out.println("NO"); } else{ out.println(a[pos]); } } public int n, m; public void run() throws Exception { inter = new StreamTokenizer(new BufferedReader(new InputStreamReader( System.in))); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); out.flush(); } public BufferedReader in; public StreamTokenizer inter; public PrintWriter out; public int nextInt() throws Exception { inter.nextToken(); return (int) inter.nval; } public String nextLine() throws Exception{ return in.readLine(); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE; for (int i = 0, x; i < N; ++i) { x = sc.nextInt(); if (x < first) { second = first; first = x; } else if (x > first && x < second) { second = x; } } if (second == Integer.MAX_VALUE) System.out.println("NO"); else System.out.println(second); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Scanner; public class A { /** * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); HashSet<Integer> set = new HashSet<Integer>(); for ( int i = 0 ; i < n ; ++i ) { set.add(sc.nextInt()); } // for i ArrayList<Integer> list = new ArrayList<Integer>(set); Collections.sort(list); if(list.size() > 1) System.out.println(list.get(1)); else System.out.println("NO"); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; public class A { /** * @param args */ public static void main(String[] args) { Scanner s = new Scanner(new InputStreamReader(System.in)); int n = s.nextInt(); int [] ar = new int[n]; for (int i = 0; i < n ; i++) { ar[i] = s.nextInt(); } if(ar.length == 1){ System.out.println("NO"); }else{ Arrays.sort(ar); int num = ar[0]; boolean flag = false; for (int i = 1; i < ar.length; i++) { if(ar[i]!= num){ System.out.println(ar[i]); flag = true; break; } } if(!flag) System.out.println("NO"); } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); int nums[] = new int[n]; StringTokenizer tokenizer = new StringTokenizer(in.readLine(), " "); for (int i = 0; i < n; i++) { nums[i] = Integer.parseInt(tokenizer.nextToken()); } Arrays.sort(nums); int min = nums[0]; int so = -200; for (int i = 1; i < n; i++) { if(nums[i] != min) { so = nums[i]; break; } } if(so != -200) System.out.println(so); else System.out.println("NO"); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.*; import java.util.*; public class Main implements Runnable { public void _main() throws IOException { int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); Arrays.sort(a); for (int i = 1; i < n; i++) if (a[i] != a[0]) { out.print(a[i]); return; } out.print("NO"); } private BufferedReader in; private PrintWriter out; private StringTokenizer st; private String next() throws IOException { while (st == null || !st.hasMoreTokens()) { String rl = in.readLine(); if (rl == null) return null; st = new StringTokenizer(rl); } return st.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(next()); } private long nextLong() throws IOException { return Long.parseLong(next()); } private double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) { new Thread(new Main()).start(); } public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); _main(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(202); } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import static java.util.Arrays.sort; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.SortedSet; import java.util.StringTokenizer; import java.util.TreeSet; public class SecondOrderStatistics implements Runnable { public static void main(String[] args) throws Exception { new SecondOrderStatistics().run(); } private void solve() throws Exception { int n = nextInt(); SortedSet<Integer> sset = new TreeSet<Integer>(); for (int i = 0; i < n; i++) { int a = nextInt(); sset.add(a); } if (sset.size() < 2) out.println("NO"); else { Integer v[] = (Integer[]) sset.toArray(new Integer[sset.size()]); sort(v); out.println(v[1]); } } // -------------- Input/Output routines below ---------------// private BufferedReader in; PrintWriter out; StringTokenizer tokenizer; public void run() { // String problem = this.getClass().getName(); try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedOutputStream(System.out)); solve(); out.flush(); in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); // System.exit(1); } } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.Arrays; import java.util.Date; import java.util.Scanner; public class Main { /** * @param args */ public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = s.nextInt(); Arrays.sort(a); int min = a[0]; if (a[0] == a[n-1]){ System.out.println("NO"); }else{ for(int i=1;;i++){ if (a[i] > min) { System.out.println(a[i]); break; } } } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Solution { BufferedReader in; PrintWriter out; StringTokenizer st; void solve() throws IOException { int n = ni(); int[] v = new int[n]; for (int i = 0; i < n; ++i) v[i] = ni(); Arrays.sort(v); int mn = v[0]; for (int i = 1; i < n; ++i) if (v[i] > mn) { out.println(v[i]); return; } out.println("NO"); } public Solution() throws IOException { Locale.setDefault(Locale.US); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } String ns() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } int ni() throws IOException { return Integer.valueOf(ns()); } long nl() throws IOException { return Long.valueOf(ns()); } double nd() throws IOException { return Double.valueOf(ns()); } public static void main(String[] args) throws IOException { new Solution(); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; public class A22 { static StreamTokenizer in; static PrintWriter out; static int nextInt() throws IOException { in.nextToken(); return (int)in.nval; } static String nextString() throws IOException { in.nextToken(); return in.sval; } public static void main(String[] args) throws IOException { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); Arrays.sort(a); int f = a[0], q = 1; while (q < n && a[q] == f) q++; out.println(q < n ? a[q] : "NO"); out.flush(); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
/** * * @author ishani */ import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); TreeSet<Integer> set = new TreeSet<Integer>(); for(int i=0;i<N;i++){ int a = sc.nextInt(); set.add(a); } if(set.size()==1)System.out.println("NO"); else{ set.remove(set.first()); System.out.println(set.first()); } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class CF22_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int num=sc.nextInt(); if(num!=1) { ArrayList<Integer>data=new ArrayList<Integer>(); for (int i=0;i<num;i++){ data.add(sc.nextInt()); } Collections.sort(data); int ind=1; while( data.get(ind-1)==data.get(ind) ) { ind++; if(ind ==data.size()) break; } if(data.size()>ind) System.out.println(data.get(ind)); else System.out.println("NO"); } else System.out.println("NO"); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; public class Main { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int size = keyboard.nextInt(); int[] arr = new int[size]; int i = 0; while( size != 0 ) { arr[i] = keyboard.nextInt(); size--; i++; } //System.out.print("size " + arr.length ); Arrays.sort(arr); int index = 0; boolean val = false; int ans = 0; for ( i = 0; i< arr.length-1 ; i++ ) { //System.out.print(" arr[i] " + arr[i] + " arr[i+1] " + arr[i+1] + "\n" ); if( arr[i] != arr[i+1] ) { val = true; //System.out.print("Changed val to true"); index = i+1; System.out.println(arr[index]); return; } } if (size == 1 || ( val == false)) { System.out.println("NO"); } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.*; import java.util.*; public class A { void run()throws IOException{ Scanner sc = new Scanner(new InputStreamReader(System.in)); int n = sc.nextInt(); int i; int[] ar = new int[n]; for(i=0; i<n; i++){ ar[i] = sc.nextInt(); } Arrays.sort(ar); int min = ar[0]; for(i=1;i<n;i++){ if(ar[i]>min) break; } if(i<n) System.out.println(ar[i]); else System.out.println("NO"); } public static void main(String[] args)throws IOException { new A().run(); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; public class Beta22PA { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int minimum = 200, second = 200; for(int i=0; i<n; i++) { int temp = scan.nextInt(); if(temp<minimum) { second = minimum; minimum = temp; } else if(temp>minimum&&temp<second) { second = temp; } } if(second>100) { System.out.println("NO"); } else { System.out.println(second); } } }
nlogn
22_A. Second Order Statistics
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.Collections; import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeSet; public class order { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(System.out))); int n=Integer.parseInt(in.readLine()); Set<Integer> set = new TreeSet<Integer>(); StringTokenizer st= new StringTokenizer(in.readLine()); int a; List<Integer> list =new LinkedList<Integer>(); while(st.hasMoreTokens()){ a= Integer.parseInt(st.nextToken()); if(!set.contains(a)){ list.add(a); set.add(a); } } if(list.size()==1){ out.println("NO"); }else{ Collections.sort(list); out.println(list.get(1)); } out.close(); System.exit(0); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; import java.math.*; /** * * @author Izhari Ishak Aksa */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Hashtable<Integer, Integer> hi = new Hashtable<Integer, Integer>(); for (int i = 0; i < n; i++) { int m = sc.nextInt(); hi.put(m, 1); } Set<Integer> set = hi.keySet(); Integer[] key = set.toArray(new Integer[set.size()]); Arrays.sort(key); try { System.out.println(key[1]); } catch (Exception e) { System.out.println("NO"); } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.StreamTokenizer; import java.lang.reflect.Array; import java.util.Arrays; public class a { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //BufferedReader input = new BufferedReader(new FileReader("input.txt")); //BufferedWriter output = new BufferedWriter(new FileWriter("output.txt")); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); StreamTokenizer in = new StreamTokenizer(input); in.nextToken(); int n = (int)in.nval; int[] mas = new int[n]; for (int i = 0; i < n; i++) { in.nextToken(); mas[i] = (int)in.nval; } Arrays.sort(mas); int min = mas[0]; int i = 1; while ((i < n)&&(min == mas[i])) { i++; } if (i < n) { output.write(Integer.toString(mas[i])); } else { output.write("NO"); } input.close(); output.close(); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; public class Test { public static void main(String[] args) { Scanner t = new Scanner(System.in); int n=t.nextInt(); int a[]= new int[n]; for(int i=0; i<n; i++) a[i]=t.nextInt(); Arrays.sort(a); int r=a[0]; for(int i=1; i<n; i++) if(a[i]!=r) { System.out.println(a[i]); System.exit(0); } System.out.println("NO"); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ //package a_vtoray_poriadkovay_statistika; import java.util.Arrays; import java.util.Scanner; /** * * @author kal1sha */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic hereSc int n, i; boolean status = false; int answer; Scanner in = new Scanner(System.in); n = in.nextInt(); int a[] = new int[n]; for (i = 0; i < n; i++) { a[i] = in.nextInt(); } Arrays.sort(a); answer = a[0]; for (i = 1; i < n; i++) { if (a[i] != answer) { answer = a[i]; status = true; i = n + 1; } } if (status) { System.out.println(answer); } else { System.out.println("NO"); } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class A { public static void main(String[] args) { new A().run(); } private void run() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); Arrays.sort(a); int i = 0; while (i < n && a[i] == a[0]) i++; if (i < n) System.out.println(a[i]); else System.out.println("NO"); sc.close(); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
//package timus; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Reader; import java.io.StreamTokenizer; import java.io.Writer; import java.util.Arrays; public class Abra { public static void main(String[] args) throws IOException { new Abra().run(); } StreamTokenizer in; PrintWriter out; boolean oj; void init() throws IOException { oj = System.getProperty("ONLINE_JUDGE") != null; Reader reader = oj ? new InputStreamReader(System.in) : new FileReader( "input.txt"); Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("output.txt"); // Writer writer = new OutputStreamWriter(System.out); in = new StreamTokenizer(new BufferedReader(reader)); out = new PrintWriter(writer); } void run() throws IOException { long beginTime = System.currentTimeMillis(); init(); solve(); out.flush(); } void printMem() { if (!oj) { System.out.println("Memory used = " + (Runtime.getRuntime().totalMemory() - Runtime .getRuntime().freeMemory())); } } int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } long nextLong() throws IOException { in.nextToken(); return (long) in.nval; } String nextString() throws IOException { in.nextToken(); return in.sval; } double nextDouble() throws IOException { in.nextToken(); return in.nval; } long deg(long x, long y) { long a = x; for (long i = 2; i <= y; i++) { a *= x; } return a; } long fact(long x) { long a = 1; for (long i = 2; i <= x; i++) { a *= i; } return a; } long digitSum(String x) { long a = 0; for (int i = 0; i < x.length(); i++) { a += x.codePointAt(i) - 48; } return a; } long digitSum(long x) { long a = 0; while (x > 0) { a += x % 10; x /= 10; } return a; } long digitMul(long x) { long a = 1; while (x > 0) { a *= x % 10; x /= 10; } return a; } double pif(double ax, double ay, double bx, double by) { return Math.sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by)); } double getPosPart(double x) { if (x <= 0) return 0; else return x; } double max(double x, double y) { if (x > y) return x; else return y; } long gcd(long a, long b) { if (a < b) { long c = b; b = a; a = c; } while (a % b != 0) { a = a % b; if (a < b) { long c = b; b = a; a = c; } } return b; } int gcd(int a, int b) { if (a < b) { int c = b; b = a; a = c; } while (a % b != 0) { a = a % b; if (a < b) { int c = b; b = a; a = c; } } return b; } long lcm(long a, long b) throws IOException { return a * b / gcd(a, b); } int lcm(int a, int b) throws IOException { return a * b / gcd(a, b); } int countOccurences(String x, String y) { int a = 0, i = 0; while (true) { i = y.indexOf(x); if (i == -1) break; a++; y = y.substring(i + 1); } return a; } int[] primes; int findPrimes(int x) { boolean[] forErato = new boolean[x]; primes = new int[x]; int l = 0, j = 0; for (int i = 2; i < x; i++) { if (forErato[i]) continue; l++; primes[l] = i; j = i * 2; while (j < x) { forErato[j] = true; j += i; } } return l; } int rev(int x) { int a = 0; while (x > 0) { a = a * 10 + x % 10; x /= 10; } return a; } class myDate { int d, m, y; public myDate(int da, int ma, int ya) { d = da; m = ma; y = ya; } int[] ml = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; void inc() { if ((d == 31) && (m == 12)) { y++; d = 1; m = 1; } else { if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) { ml[1] = 29; } if (d == ml[m - 1]) { m++; d = 1; } else d++; } } } int partition(int n, int l, int m) {// n - sum, l - length, m - every part // <= m if (n < l) return 0; if (n < l + 2) return 1; if (l == 1) return 1; int c = 0; for (int i = Math.min(n - l + 1, m); i >= (n + l - 1) / l; i--) { c += partition(n - i, l - 1, i); } return c; } void solve() throws IOException { int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } Arrays.sort(a); int min = a[0]; for (int i = 1; i < n; i++) { if (a[i] != min) { out.print(a[i]); return; } } out.print("NO"); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE; for (int i = 0, x; i < N; ++i) { x = sc.nextInt(); if (x < first) { second = first; first = x; } else if (x > first && x < second) { second = x; } } if (second == Integer.MAX_VALUE) System.out.println("NO"); else System.out.println(second); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; import java.io.*; public class Main { public static void main (String [] args) throws IOException { BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int [] nums = new int[n]; args = br.readLine().split(" "); for (int i = 0; i < n; i++) { nums[i] = Integer.parseInt(args[i]); } Arrays.sort(nums); int min = nums[0]; for (int i = 1; i < n; i++) { if (nums[i]>min) { System.out.println(nums[i]); return; } } System.out.println("NO"); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; public class CDF22_A { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] A = new int[N]; for (int i=0; i<N; i++) A[i] = sc.nextInt(); Arrays.sort(A); int i = 0; while (i < A.length && A[i] == A[0]) i++; System.out.println(i == A.length ? "NO" : A[i]); sc.close(); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; import java.io.*; public class A { public static void main(String[] args) { Scanner s = new Scanner(new InputStreamReader(System.in)); int n = s.nextInt(); if (n == 1) { System.out.println("NO"); System.exit(0); } int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = s.nextInt(); } Arrays.sort(nums); int x = 1; while (x < n && nums[x] == nums[x - 1]) x++; if (x == n) { System.out.println("NO"); System.exit(0); } else { System.out.println(nums[x]); System.exit(0); } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws Exception { //System.setIn(new FileInputStream("1")); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } private static void solve() throws Exception { int n = nextInt(); boolean[] use = new boolean[500]; ArrayList<Integer> a = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { int v = nextInt(); if (!use[250 + v]) { use[250 + v] = true; a.add(v); } } Collections.sort(a); if (a.size() < 2) { out.println("NO"); } else { out.println(a.get(1)); } } static BufferedReader in; static PrintWriter out; static StringTokenizer st; static String nextString() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(nextString()); } static double nextDouble() throws IOException { return Double.parseDouble(nextString()); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; public class Main { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int size = Integer.parseInt(keyboard.nextLine()); int[] arr = new int[size]; int i = 0; while( size != 0 ) { arr[i] = keyboard.nextInt(); size--; i++; } if( arr.length == 1 ) { System.out.println("NO"); } else { Arrays.sort(arr); boolean val = false; int ans = 0; for ( i = 0; i< arr.length-1 ; i++ ) { if( arr[i] != arr[i+1] ) { val = true; ans = arr[i+1]; System.out.println(ans); i = arr.length; } else if( i == arr.length-2 ) //val == false { System.out.println("NO"); } } } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.*; import java.util.*; public class A implements Runnable { public static void main(String[] args) { new A().run(); } class FastScanner { BufferedReader br; StringTokenizer st; boolean eof; String buf; public FastScanner(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileReader(fileName)); nextToken(); } public FastScanner(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); nextToken(); } String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; break; } } String ret = buf; buf = eof ? "-1" : st.nextToken(); return ret; } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } void close() { try { br.close(); } catch (Exception e) { } } boolean isEOF() { return eof; } } FastScanner sc; PrintWriter out; public void run() { Locale.setDefault(Locale.US); try { sc = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); sc.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } } int nextInt() { return sc.nextInt(); } String nextToken() { return sc.nextToken(); } long nextLong() { return sc.nextLong(); } double nextDouble() { return sc.nextDouble(); } void solve() { int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } Arrays.sort(a); int i = 1; while (i < n && a[i] == a[i - 1]) { ++i; } if (i >= n) { out.println("NO"); } else { out.println(a[i]); } } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; /* Template for TopCoder and ACMonline. */ public class A { private static StreamTokenizer in = null; private static PrintWriter out = null; static int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } static long nextLong() throws IOException { in.nextToken(); return (long) in.nval; } static double nextDouble() throws IOException { in.nextToken(); return (double) in.nval; } static String nextString() throws IOException { in.nextToken(); return in.sval; } public static void main(String[] args) throws IOException { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); // Here is the solution: new A().solve(); out.flush(); } public void solve() throws IOException { int n = nextInt(); int min = Integer.MAX_VALUE; int res = Integer.MAX_VALUE; for (int i=0; i<n; ++i) { int d = nextInt(); if (d<min) { res = min; min = d; } else if (d>min && d<res) res = d; } if (res == Integer.MAX_VALUE) out.println("NO"); else out.println(res); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.*; import java.util.*; public class Main implements Runnable { public void solution() throws IOException { int n = in.nextInt(); int[] a = new int[n]; int min = Integer.MAX_VALUE; for (int i = 0; i < n; ++i) { a[i] = in.nextInt(); if (a[i] < min) { min = a[i]; } } int res = Integer.MAX_VALUE; for (int i = 0; i < n; ++i) { if (a[i] != min && a[i] < res) { res = a[i]; } } if (res == Integer.MAX_VALUE) { out.println("NO"); } else { out.println(res); } } public void run() { try { solution(); in.reader.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } } private class Scanner { private BufferedReader reader; private StringTokenizer tokenizer; public Scanner(Reader reader) { this.reader = new BufferedReader(reader); this.tokenizer = new StringTokenizer(""); } public boolean hasNext() throws IOException { while (!tokenizer.hasMoreTokens()) { String next = reader.readLine(); if (next == null) { return false; } tokenizer = new StringTokenizer(next); } return true; } public String next() throws IOException { hasNext(); return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public String nextLine() throws IOException { tokenizer = new StringTokenizer(""); return reader.readLine(); } } public static void main(String[] args) throws IOException { new Thread(null, new Main(), "", 1 << 28).start(); } PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); Scanner in = new Scanner(new InputStreamReader(System.in)); }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.ArrayDeque; import java.util.Arrays; public class Main { private static StreamTokenizer in; private static PrintWriter out; private static int nextInt() throws Exception { in.nextToken(); return (int)in.nval; } private static String nextString() throws Exception { in.nextToken(); return in.sval; } public static void main(String[] args) throws Exception { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); int n = nextInt(); int[] a = new int[n]; for (int i=0; i<n; i++) a[i] = nextInt(); Arrays.sort(a); int u = a[0]; for (int i=0; i<n; i++) if (a[i]>u) { out.println(a[i]); out.flush(); return; } out.println("NO"); out.flush(); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
//package round22; import java.io.PrintWriter; import java.io.StringReader; import java.util.Arrays; import java.util.Scanner; public class A { private Scanner in; private PrintWriter out; // private String INPUT = "5 1 2 3 1 1"; private String INPUT = ""; public void solve() { int n = ni(); int[] a = new int[n]; for(int i = 0;i < n;i++){ a[i] = ni(); } Arrays.sort(a); for(int i = 1;i < n;i++){ if(a[i] > a[i - 1]){ out.println(a[i]); return; } } out.println("NO"); } public void run() throws Exception { in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(new StringReader(INPUT)); out = new PrintWriter(System.out); solve(); out.flush(); } public static void main(String[] args) throws Exception { new A().run(); } private int ni() { return Integer.parseInt(in.next()); } private static void tr(Object... o) { System.out.println(o.length == 1 ? o[0] : Arrays.toString(o)); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; import java.lang.*; import java.math.*; public class Main{ Scanner sc=new Scanner(System.in); void run(){ int n = sc.nextInt(); int x[] = new int [n]; for (int i=0;i<n;i++) x[i] = sc.nextInt(); java.util.Arrays.sort(x); int i = 0; for(i=0;i<n-1;i++) { if (x[i] != x[i+1]) { System.out.println( x[i+1] ); return; } } System.out.println("NO"); return; } public static void main(String[] args){ new Main().run(); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); TreeSet<Integer> set = new TreeSet<Integer>(); for(int i=0;i<n;i++){ set.add(sc.nextInt()); } if(set.size() >= 2) System.out.println(set.toArray()[1]); else System.out.println("NO"); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class One { InputStreamReader inp = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(inp); boolean test = false; // String[] inData = { "4", "A", "B", "C", "D", "A-B 1:1", "A-C 2:2", // "A-D 1:0", "B-C 1:0", "B-D 0:3", "C-D 0:3" }; String[] inData = { "4","1 1 2" }; static int id = -1; public String readLine() throws IOException { id++; if (test) return inData[id]; else return in.readLine(); } public void solve() throws Exception { readLine(); String readLine = readLine(); String[] split = readLine.split(" "); List<Integer> ints = new ArrayList<Integer>(); for (int i = 0; i < split.length; i++) { ints.add(Integer.valueOf(split[i])); } Collections.sort(ints); Integer object = ints.get(0); for (int i = 0; i < split.length; i++) { if(ints.get(i).compareTo(object) > 0){ System.out.println(ints.get(i)); return; } } System.out.println("NO"); } public static void main(String[] args) throws Exception { new One().solve(); } }
nlogn
22_A. Second Order Statistics
CODEFORCES
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class Main { // static int INF=1<<28; //int x,y; static int sum=0; //static ArrayList<Integer> lis = new ArrayList<Integer>(); public static void main(String[] args)throws Exception{ // Scanner sc =new Scanner(new File("input.txt")); Scanner sc =new Scanner(System.in); // File file = new File("prime2.txt"); // PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file))); // sc.useDelimiter("(\\s)+|[,]"); // ArrayList<Integer> lis = new ArrayList<Integer>(); //ArrayList<String> lis = new ArrayList<String>(); //System.out.println(); // for(int i=0;i<;i++) while(sc.hasNext()){ int n=ni(sc),x[]=new int[n+1]; for(int i=1;i<=n;i++)x[i]=ni(sc); sort(x); if(x[n]==1){x[n]=2;for(int i=1;i<=n;i++)System.out.print(x[i]+" ");} else{x[0]=1; for(int i=0;i<n;i++)System.out.print(x[i]+" "); } } } static void db(Object... os){ System.err.println(Arrays.deepToString(os)); } static int ni(Scanner in){ return in.nextInt(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; public class TaskA { void Run() throws IOException { int n=ReadInt(); int[] arr=new int[n]; for(int i=0;i<n;++i) arr[i]=ReadInt(); Arrays.sort(arr); boolean one=true; for(int x : arr) if(x!=1) { one=false; break; } if(one) { for(int i=1;i<n;++i) output.print("1 "); output.print("2"); return; } int prev=1; for(int x : arr) if(x==prev) { output.print(prev); output.print(" "); } else { output.print(prev); output.print(" "); prev=x; } } public static void main(String[] args) throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; Reader reader; reader=oj ? new InputStreamReader(System.in) : new FileReader("input.txt"); input=new BufferedReader(reader); Writer writer=new OutputStreamWriter(System.out); writer=new BufferedWriter(writer); output=new PrintWriter(writer); new TaskA().Run(); output.close(); } static int ReadInt() throws IOException { return Integer.parseInt(ReadString()); } static long ReadLong() throws IOException { return Long.parseLong(ReadString()); } static String ReadString() throws IOException { while(tokenizer==null || !tokenizer.hasMoreTokens()) tokenizer=new StringTokenizer(input.readLine()); return tokenizer.nextToken(); } static StringTokenizer tokenizer; static BufferedReader input; static PrintWriter output; }
nlogn
135_A. Replacement
CODEFORCES
import javax.sound.sampled.Line; import java.awt.Point; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.io.*; import java.math.BigInteger; import static java.math.BigInteger.*; import java.util.*; public class A{ void solve()throws Exception { int n=nextInt(); int[]a=new int[n]; for(int i=0;i<n;i++) a[i]=nextInt(); Arrays.sort(a); int[]res=new int[n]; for(int i=0;i<n;i++) { if(i==0) res[i]=1; else res[i]=a[i-1]; } if(a[n-1]==1) res[n-1]=2; for(int i=0;i<n;i++) { if(i==n-1) writer.println(res[i]); else writer.print(res[i]+" "); } } //////////// BufferedReader reader; PrintWriter writer; StringTokenizer stk; void run()throws Exception { reader=new BufferedReader(new InputStreamReader(System.in)); stk=null; writer=new PrintWriter(new PrintWriter(System.out)); solve(); reader.close(); writer.close(); } int nextInt()throws Exception { return Integer.parseInt(nextToken()); } long nextLong()throws Exception { return Long.parseLong(nextToken()); } double nextDouble()throws Exception { return Double.parseDouble(nextToken()); } String nextString()throws Exception { return nextToken(); } String nextLine()throws Exception { return reader.readLine(); } String nextToken()throws Exception { if(stk==null || !stk.hasMoreTokens()) { stk=new StringTokenizer(nextLine()); return nextToken(); } return stk.nextToken(); } public static void main(String[]args) throws Exception { new A().run(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; //created at 6:06 PM 12/9/11 by Abrackadabra public class A { int IOMode = 0; //0 - consoleIO, 1 - <taskName>.in/out, 2 - input.txt/output.txt, 3 - test case generator String taskName = ""; void solve() throws IOException { int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); Arrays.sort(a); int z = 0; for (int i = 0; i < n; i++) { if (a[i] != 1) z++; } if (z == 0 && n > 1) a[n - 2] = 2; if (n == 1 && a[0] == 1) { out.println(2); return; } out.print("1 "); for (int i = 0; i < n - 1; i++) out.print(a[i] + " "); } public static void main(String[] args) throws IOException { if (args.length > 0 && args[0].equals("Abra")) debugMode = true; new A().run(); } long startTime = System.nanoTime(), tempTime = startTime, finishTime = startTime; long startMem = Runtime.getRuntime().totalMemory(), finishMem = startMem; void run() throws IOException { init(); if (debugMode) { con.println("Start"); con.println("Console output:"); } solve(); finishTime = System.nanoTime(); finishMem = Runtime.getRuntime().totalMemory(); out.flush(); if (debugMode) { int maxSymbols = 1000, c = 0; BufferedReader tbr = new BufferedReader(new FileReader("input.txt")); char[] a = new char[maxSymbols]; tbr.read(a); if (a[0] != 0) { con.println(); con.println("File input:"); con.print(a); } boolean left = true; for (int i = 0; i < maxSymbols; i++) left = left && a[i] != 0; if (left) con.println("..."); else con.println(); tbr = new BufferedReader(new FileReader("output.txt")); a = new char[maxSymbols]; tbr.read(a); if (a[0] != 0) { con.println(); con.println("File output:"); con.print(a); } left = true; for (int i = 0; i < maxSymbols; i++) left = left && a[i] != 0; if (left) con.println("..."); else con.println(); con.println("Time passed: " + (finishTime - startTime) / 1000000000.0 + " sec"); con.println("Memory used: " + (finishMem - startMem) + " bytes"); con.println("Total memory: " + Runtime.getRuntime().totalMemory() + " bytes"); } } boolean tick(double x) { if (System.nanoTime() - tempTime > x) { tempTime = System.nanoTime(); con.println("Tick at " + (tempTime - startTime) / 1000000000 + " sec"); con.print(" "); return true; } return false; } void printTime() { con.println((System.nanoTime() - tempTime) + " nanos passed"); tempTime = System.nanoTime(); } static boolean debugMode = false; PrintStream con = System.out; void init() throws IOException { if (debugMode && IOMode != 3) { br = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter(new FileWriter("output.txt")); } else switch (IOMode) { case 0: br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); break; case 1: br = new BufferedReader(new FileReader(taskName + ".in")); out = new PrintWriter(new FileWriter(taskName + ".out")); break; case 2: br = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter(new FileWriter("output.txt")); break; case 3: out = new PrintWriter(new FileWriter("input.txt")); break; } } BufferedReader br; PrintWriter out; StringTokenizer in; boolean hasMoreTokens() throws IOException { while (in == null || !in.hasMoreTokens()) { String line = br.readLine(); if (line == null) return false; in = new StringTokenizer(line); } return true; } String nextString() throws IOException { return hasMoreTokens() ? in.nextToken() : null; } int nextInt() throws IOException { return Integer.parseInt(nextString()); } long nextLong() throws IOException { return Long.parseLong(nextString()); } double nextDouble() throws IOException { return Double.parseDouble(nextString()); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.*; import java.util.*; public class c { public static void main(String[] args) throws IOException { BufferedReader r = new BufferedReader(new InputStreamReader(System.in), 1); String s = r.readLine(); int n = Integer.parseInt(s); String s2 = r.readLine(); StringTokenizer st = new StringTokenizer(s2," "); int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = Integer.parseInt(st.nextToken()); Arrays.sort(a); if (a[n - 1] == 1) a[n - 1] = 2; else {a[n - 1] = 1; Arrays.sort(a);} for (int i = 0; i < n; i++) System.out.println(a[i]); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.*; import java.util.*; public class A { public A () throws IOException { String input = r.readLine(); int N = Integer.parseInt(input); int [] A = new int [N]; input = r.readLine(); String [] S = input.split(" "); for (int i = 0; i < N; ++i) A[i] = Integer.parseInt(S[i]); solve(N, A); } public void solve (int N, int [] A) { t = millis(); Arrays.sort(A); if (A[N-1] > 1) A[N-1] = 1; else A[N-1] = 2; Arrays.sort(A); System.out.print(A[0]); for (int i = 1; i < N; ++i) System.out.print(" " + A[i]); System.out.println(); } //////////////////////////////////////////////////////////////////////////////////// static BufferedReader r; static long t; static void print2 (Object o) { System.out.println(o); } static void print (Object o) { print2(o); //print2((millis() - t) / 1000.0); System.exit(0); } static void run () throws IOException { r = new BufferedReader(new InputStreamReader(System.in)); new A(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } }
nlogn
135_A. Replacement
CODEFORCES
import static java.lang.Math.*; import static java.lang.System.currentTimeMillis; import static java.lang.System.exit; import static java.lang.System.arraycopy; import static java.util.Arrays.sort; import static java.util.Arrays.binarySearch; import static java.util.Arrays.fill; import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { try { if (new File("input.txt").exists()) System.setIn(new FileInputStream("input.txt")); } catch (SecurityException e) { } new Main().run(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); private void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = nextInt(); int m[] = new int[n]; for(int i = 0; i < n; i++) m[i] = nextInt(); sort(m); m[n - 1] = m[n - 1] != 1 ? 1 : 2; sort(m); for(int i = 0; i < n; i++){ if(i != 0) out.print(" "); out.print(m[i]); } out.println(); in.close(); out.close(); } void chk(boolean b) { if (b) return; System.out.println(new Error().getStackTrace()[1]); exit(999); } void deb(String fmt, Object... args) { System.out.printf(Locale.US, fmt + "%n", args); } String nextToken() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } 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 nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!st.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; st = new StringTokenizer(s); } return false; } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class A135 { public static void main(String[] args) throws Exception { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(r.readLine()); int[] ar = new int[n]; StringTokenizer st = new StringTokenizer(r.readLine()); for (int x = 0; x < n; x++) { ar[x] = Integer.parseInt(st.nextToken()); } Arrays.sort(ar); if (n == 1) { System.out.println(ar[0]==1?"2":"1"); return; } if (ar[n - 1] == 1) { ar[n - 2] = 2; } System.out.print("1"); for (int x = 0; x < n - 1; x++) { System.out.print(" " + ar[x]); } System.out.println(); } }
nlogn
135_A. Replacement
CODEFORCES
/** * Created by IntelliJ IDEA. * User: mac * Date: 11-12-9 * Time: 下午10:48 * To change this template use File | Settings | File Templates. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class A { BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } void solve() throws IOException { int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = nextInt(); } Arrays.sort(a); if (a[a.length - 1] == 1) { for (int i = 0; i < a.length - 1; i++) { writer.print(1 + " "); } writer.println(2 + ""); return; } for (int i = 0; i < a.length; i++) { if (i == 0) writer.print(1 + " "); else writer.print(a[i - 1] + " "); } writer.println(); } static public void main(String[] args) { new A().run(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class A implements Runnable{ final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE")!=null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException{ if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException{ while(!tok.hasMoreTokens()){ tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException{ return Integer.parseInt(readString()); } long readLong() throws IOException{ return Long.parseLong(readString()); } double readDouble() throws IOException{ return Double.parseDouble(readString()); } public static void main(String[] args){ new Thread(null, new A(), "", 256 * (1L << 20)).start(); } public void run(){ try{ long t1 = System.currentTimeMillis(); init(); solve(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = "+(t2-t1)); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } void solve() throws IOException{ int n = readInt(); int[] a = new int[n]; for (int i = 0; i < n; i++){ a[i] = readInt(); } Arrays.sort(a); a[n-1] = a[n-1] == 1? 2:1; Arrays.sort(a); for (int i = 0; i < n; i++){ out.print(a[i] + " "); } } }
nlogn
135_A. Replacement
CODEFORCES
import static java.util.Arrays.deepToString; import java.io.*; import java.math.*; import java.util.*; public class A { static int [] solve(int [] a) { int n = a.length; Arrays.sort(a); a[n - 1] = (a[n - 1] > 1 ? 1 : 2); int [] b = Arrays.copyOf(a, n); Arrays.sort(b); return b; } public static void main(String[] args) throws Exception { reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(System.out); setTime(); int n = nextInt(); int [] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } int [] b = solve(a); for (int v: b) { writer.print(v + " "); } printTime(); printMemory(); writer.close(); } static BufferedReader reader; static PrintWriter writer; static StringTokenizer tok = new StringTokenizer(""); static long systemTime; static void debug(Object... o) { System.err.println(deepToString(o)); } static void setTime() { systemTime = System.currentTimeMillis(); } static void printTime() { System.err.println("Time consumed: " + (System.currentTimeMillis() - systemTime)); } static void printMemory() { System.err.println("Memory consumed: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + "kb"); } static String next() { while (!tok.hasMoreTokens()) { String w = null; try { w = reader.readLine(); } catch (Exception e) { e.printStackTrace(); } if (w == null) return null; tok = new StringTokenizer(w); } return tok.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static long nextLong() { return Long.parseLong(next()); } static double nextDouble() { return Double.parseDouble(next()); } static BigInteger nextBigInteger() { return new BigInteger(next()); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { int n = nextInt(); int[] mas = new int[n]; for(int i = 0; i<n; i++) { mas[i] = nextInt(); } Arrays.sort(mas); if(mas[n-1] == 1) { for(int i = 0; i<n-1; i++) { out.print(1 + " "); } out.println(2); out.flush(); exit(); } out.print("1 "); for(int i = 0; i<n-1; i++) { out.print(mas[i] + " "); } out.println(); out.flush(); } ///////////////////////////////////////////////////////////////// // IO ///////////////////////////////////////////////////////////////// private static StreamTokenizer in; private static PrintWriter out; private static BufferedReader inB; private static boolean FILE=false; private static int nextInt() throws Exception{ in.nextToken(); return (int)in.nval; } private static String nextString() throws Exception{ in.nextToken(); return in.sval; } static{ try { out = new PrintWriter(FILE ? (new FileOutputStream("output.txt")) : System.out); inB = new BufferedReader(new InputStreamReader(FILE ? new FileInputStream("input.txt") : System.in)); } catch(Exception e) {e.printStackTrace();} in = new StreamTokenizer(inB); } ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// // pre - written ///////////////////////////////////////////////////////////////// private static void println(Object o) throws Exception { out.println(o); out.flush(); } private static void exit(Object o) throws Exception { println(o); exit(); } private static void exit() { System.exit(0); } private static final int INF = Integer.MAX_VALUE; private static final int MINF = Integer.MIN_VALUE; ////////////////////////////////////////////////////////////////// }
nlogn
135_A. Replacement
CODEFORCES
import java.io.* ; import java.util.*; import static java.lang.Math.* ; import static java.util.Arrays.* ; public class A { public static void main(String[] args) throws IOException { new A().solveProblem(); out.close(); } static Scanner in = new Scanner(new InputStreamReader(System.in)); //static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static PrintStream out = new PrintStream(new BufferedOutputStream(System.out)); public void solveProblem() { int n = in.nextInt() ; E[] get = new E[n] ; for( int i = 0 ; i < n ; i++ ){ get[i] = new E(i,in.nextInt()) ; } sort(get) ; if( get[n-1].g == 1){ get[n-1].g = 2 ; }else{ get[n-1].g = 1 ; } sort(get) ; for( int i = 0 ; i < n - 1 ; i++ ) out.print(get[i].g +" " ) ; out.println(get[n-1].g) ; } class E implements Comparable<E>{ int g ; int index ; public E( int index, int g){ this.g = g ; this.index = index ; } public int compareTo( E e){ return g - e.g ; } } static int[] readGet( int n ){ int[] get = new int[n] ; for( int i = 0 ; i < n ; i++ ) get[i] = in.nextInt(); return get ; } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class A_135 { /** * @param args */ public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int[] mas=new int[n]; for(int i=0;i<n;i++){ mas[i]=in.nextInt(); } Arrays.sort(mas); PrintWriter out=new PrintWriter(System.out); boolean isEd=true; for(int i=0;i<n;i++) if(mas[i]!=1){ isEd=false; break; } if(!isEd) out.print('1'); for(int i=0;i<n-1;i++){ out.print(' '); out.print(mas[i]); } if(isEd) out.print(" 2"); out.flush(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.*; import java.util.*; public class CodeForce { private void solve() throws IOException { final int N = nextInt(); int []A = new int[N]; for(int i = 0; i < N; i++) A[i] = nextInt(); Arrays.sort(A); if(A[N-1] == 1) A[N-1] = 2; else A[N-1] = 1; Arrays.sort(A); for(int i = 0; i < N; i++) System.out.print(A[i] + " "); } public static void main(String[] args) { new CodeForce().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(new FileOutputStream(new File("output.txt"))); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } 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 nextLine() throws IOException { return reader.readLine(); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class C { public static void main(String... args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); int[] x = new int[n]; int max=0, pos=-1; for(int i=0; i<n; i++) { x[i]=sc.nextInt(); if (max<x[i]) { max=x[i]; pos=i; } } x[pos] = (max==1) ? 2 : 1; Arrays.sort(x); for(int i=0; i<n; i++) System.out.print(x[i]+" "); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.*; import java.util.*; public class TaskA { BufferedReader br; PrintWriter out; StringTokenizer stok; String nextToken() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return "-1"; } stok = new StringTokenizer(s); } return stok.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } char nextChar() throws IOException { return (char) (br.read()); } String nextLine() throws IOException { return br.readLine(); } void solve() throws IOException { int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } Arrays.sort(a); a[n - 1] = a[n - 1] == 1 ? 2 : 1; Arrays.sort(a); for (int i = 0; i < n; i++) { out.print(a[i]); out.print(' '); } } void run() throws IOException { // br = new BufferedReader(new FileReader("taska.in")); // out = new PrintWriter("taska.out"); // br = new BufferedReader(new FileReader("input.txt")); // out = new PrintWriter("output.txt"); br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); br.close(); out.close(); } public static void main(String[] args) throws IOException { // Locale.setDefault(Locale.US); new TaskA().run(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.*; import java.util.*; import java.util.prefs.NodeChangeEvent; import java.math.*; public class Main implements Runnable { private BufferedReader in; private PrintWriter out; private StringTokenizer st; private Random rnd; private void solve() throws IOException { int n = nextInt(); int[] a = new int[n]; int max = 0; for(int i = 0; i < n; i++) { a[i] = nextInt(); if(a[i] > a[max]) max = i; } int value = 1; if(a[max] == 1) value = 2; a[max] = value; Arrays.sort(a); for(int i = 0; i < n; i++) { out.print(a[i]); out.print(' '); } } public static void main(String[] args) { new Main().run(); } public void run() { try { try { in = new BufferedReader(new FileReader("INPUT.TXT")); out = new PrintWriter(new FileWriter("OUTPUT.TXT")); } catch(FileNotFoundException e) { in = new BufferedReader(new InputStreamReader((System.in))); out = new PrintWriter(System.out); } st = null; rnd = new Random(); solve(); out.close(); } catch(IOException e) { e.printStackTrace(); } } private String nextToken() throws IOException, NullPointerException { while(st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
nlogn
135_A. Replacement
CODEFORCES
import java.util.*; import java.math.*; public class codeforces { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] data=new int[n]; for(int i=0;i<n;i++) data[i]=sc.nextInt(); Arrays.sort(data); if(data[n-1]!=1) data[n-1]=1; else data[n-1]=2; Arrays.sort(data); for(int i=0;i<n;i++) { System.out.print(data[i]); if(i!=n-1) System.out.print(" "); } return; } }
nlogn
135_A. Replacement
CODEFORCES
import java.util.*; public class A { static int [] reverse = new int [257]; public static void main (String [] arg) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int [] A =new int [n]; for (int i = 0; i<A.length; ++i) A[i] = sc.nextInt(); Arrays.sort(A); if (n == 1) { System.out.println( (A[0] == 1) ? "2" : "1"); return; } else if (A[0] == A[A.length-1] && A[0] == 1) { System.out.print("1"); for (int i = 1; i<n-1; ++i) System.out.print(" " + A[i]); System.out.println(" 2"); return; } else if (A[0] == A[A.length-1]) { System.out.print("1"); for (int i = 1; i<n; ++i) System.out.print(" " + A[i]); System.out.println(); return; } for (int i = 0; i<A.length; ++i) { int prev = (i == 0) ? Integer.MAX_VALUE : A[i-1]; int next = (i == A.length-1) ? Integer.MAX_VALUE : A[i+1]; int ans = Math.min(prev, Math.min(next, A[i])); if (i == 0) ans = 1; System.out.print((i == 0) ? "" + ans : " " + ans); } System.out.println(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class C { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String[] S = in.readLine().split(" "); int[] A = new int[n]; boolean allOnes = true; for (int i = 0; i < n; i++) { A[i] = Integer.parseInt(S[i]); allOnes &= A[i] == 1; } Arrays.sort(A); if (A[A.length - 1] > 1) A[A.length - 1] = 1; else A[A.length - 1] = 2; Arrays.sort(A); for (int i = 0; i < A.length; i++) System.out.print(A[i] + " "); System.out.println(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class C implements Runnable { private void Solution() throws IOException { int n = nextInt(), max = 0, maxi = 0; ArrayList<Integer> mas = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { int num = nextInt(); if (num > max) { max = num; maxi = i; } mas.add(num); } mas.remove(maxi); mas.add(max == 1 ? 2 : 1); Collections.shuffle(mas); Collections.sort(mas); for (int i = 0; i < n; i++) System.out.print(mas.get(i) + " "); } public static void main(String[] args) { new C().run(); } BufferedReader in; StringTokenizer tokenizer; public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; Solution(); in.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(in.readLine()); return tokenizer.nextToken(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.*; import java.math.*; import java.util.*; public class Main { BufferedReader reader; FastScanner sc; void solve() throws Exception { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } Arrays.sort(arr); if (arr[n - 1] == 1) arr[n - 1] = 2; else arr[n - 1] = 1; Arrays.sort(arr); for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } System.out.println(); } public static void main(String[] args) throws Exception { new Main().solve(); } Main() throws Exception { if (System.getProperty("ONLINE_JUDGE") == null) { //System.setIn(new FileInputStream("input.txt")); //System.setOut(new PrintStream("output.txt")); } reader = new BufferedReader(new InputStreamReader(System.in)); sc = new FastScanner(reader); } } class FastScanner { BufferedReader reader; StringTokenizer strTok; public FastScanner(BufferedReader reader) { this.reader = reader; } public String nextToken() throws IOException { if (strTok == null || !strTok.hasMoreTokens()) { strTok = new StringTokenizer(reader.readLine()); } return strTok.nextToken(); } 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()); } public BigInteger nextBigInteger() throws IOException { return new BigInteger(nextToken()); } public BigDecimal nextBigDecimal() throws IOException { return new BigDecimal(nextToken()); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class C { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = null; private void solution() throws IOException { int n = nextInt(); int[] mas = new int[n]; for (int i = 0; i < n; i++) { mas[i] = nextInt(); } Arrays.sort(mas); if (mas[n - 1] == 1) { mas[n - 1] = 2; } else { mas[n - 1] = 1; } Arrays.sort(mas); for (int i = 0; i < n; i++) { System.out.print(mas[i] + " "); } } String nextToken() throws IOException { if (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(bf.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public static void main(String args[]) throws IOException { new C().solution(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.awt.geom.*; import java.io.*; import java.math.*; import java.util.*; import java.util.regex.*; import static java.lang.Math.*; public class A { public A() throws Exception { int n = in.nextInt(); int[] arr = new int[n]; for (int i=0;i<n;i++) arr[i] = in.nextInt(); if (n==1&&arr[0]==1) { System.out.println(2); return; } Arrays.sort(arr); if (arr[n-1]==1) arr[n-2] = 2; buf.append(1); for (int i=0;i<n-1;i++) buf.append(' ').append(arr[i]); buf.append('\n'); System.out.print(buf); } Scanner in = new Scanner(System.in); StringBuilder buf = new StringBuilder(); public static void main(String[] args) throws Exception { // {{{ new A(); } // }}} public static void debug(Object... arr) { // {{{ System.err.println(Arrays.deepToString(arr)); } // }}} }
nlogn
135_A. Replacement
CODEFORCES
import java.io.OutputStream; import java.io.PrintWriter; import java.util.AbstractList; import java.io.Writer; import java.util.List; import java.io.IOException; import java.util.Arrays; import java.util.InputMismatchException; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Egor Kulikov ([email protected]) */ 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(); } } class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int size = in.readInt(); int[] array = IOUtils.readIntArray(in, size); Arrays.sort(array); if (array[size - 1] == 1) array[size - 1] = 2; else array[size - 1] = 1; Arrays.sort(array); out.printLine(Array.wrap(array).toArray()); } } 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 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 static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(outputStream); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } } class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.readInt(); return array; } } abstract class Array<T> extends AbstractList<T> { public static List<Integer> wrap(int...array) { return new IntArray(array); } protected static class IntArray extends Array<Integer> { protected final int[] array; protected IntArray(int[] array) { this.array = array; } public int size() { return array.length; } public Integer get(int index) { return array[index]; } public Integer set(int index, Integer value) { int result = array[index]; array[index] = value; return result; } } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.*; import java.math.*; import java.util.*; import java.awt.geom.*; import static java.lang.Math.*; public class Solution implements Runnable { public void solve() throws Exception { int n = nextInt(); int a [] = new int [n]; for (int i = 0 ;i < n; i++) { a[i] = nextInt(); } Arrays.sort(a); if (n == 1) { if (a[0] == 1) { out.print(2); } else { out.print(1); } } else { out.print(1); for (int i = 1; i < n; i++) { if (i == n-1 && a[i] == 1) { out.print(" "+2); } else { out.print(" "+a[i-1]); } } } } /********************************************************************************************/ /* THERE IS INPUT */ BufferedReader in; PrintWriter out; StringTokenizer st; long stime=0; private String next() throws Exception { while (st == null || !st.hasMoreElements()) st = new StringTokenizer(in.readLine()); return st.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()); } public void run() { try { //Locale.setDefault(Locale.US); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); } catch (Exception ex) { throw new RuntimeException(ex); } finally { out.close(); } } public static void main(String[] args) throws Exception { new Thread(null, new Solution(), "", 1 << 25).start(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int[] a = new int[n]; boolean has_more_than_one = false; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); if (a[i] > 1) has_more_than_one = true; } Arrays.sort(a); if (n == 1) { if (a[0] == 1) out.print(2); else out.print(1); } else { out.print(1 + " "); for (int i = 1; i < n; i++) { if (has_more_than_one || i < n - 1) out.print(a[i - 1] + " "); else out.println(2); } } out.close(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.*; import java.util.*; public class A implements Runnable { private MyScanner in; private PrintWriter out; private void solve() { int n = in.nextInt(); int[] a = new int[n]; int max = -1, maxp = -1; for (int i = 0; i < n; ++i) { a[i] = in.nextInt(); if (a[i] > max) { max = a[i]; maxp = i; } } if (max == 1) { for (int i = 0; i < n - 1; ++i) { out.print(1 + " "); } out.println(2); return; } a[maxp] = 1; Arrays.sort(a); for (int i = 0; i < n; ++i) { out.print(a[i] + " "); } out.println(); } @Override public void run() { in = new MyScanner(); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } public static void main(String[] args) { new A().run(); } static class MyScanner { private BufferedReader br; private StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } private String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); return null; } } return st.nextToken(); } public String nextLine() { try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); return null; } } public String next() { return nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } }
nlogn
135_A. Replacement
CODEFORCES
import java.lang.*; import java.io.*; import java.util.*; import java.math.*; public class A implements Runnable{ public void run() { int n = nextInt(); int[] arr = new int[n]; boolean allOne = true; for (int i = 0; i < n; ++i) { arr[i] = nextInt(); if (arr[i] != 1) { allOne = false; } } Arrays.sort(arr); if (!allOne) { out.print("1 "); } for (int i = 0; i < n-1; ++i) { out.print(arr[i] + " "); } if (allOne) { out.print("2"); } out.println(); out.flush(); } private static BufferedReader br = null; private static PrintWriter out = null; private static StringTokenizer stk = null; public static void main(String[] args) { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); (new Thread(new A())).start(); } private void loadLine() { try { stk = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } private String nextLine() { try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); } return null; } private Integer nextInt() { while (stk==null||!stk.hasMoreTokens()) loadLine(); return Integer.parseInt(stk.nextToken()); } private Long nextLong() { while (stk==null||!stk.hasMoreTokens()) loadLine(); return Long.parseLong(stk.nextToken()); } private String nextWord() { while (stk==null||!stk.hasMoreTokens()) loadLine(); return (stk.nextToken()); } private Double nextDouble() { while (stk==null||!stk.hasMoreTokens()) loadLine(); return Double.parseDouble(stk.nextToken()); } }
nlogn
135_A. Replacement
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class AAA { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); int max = a[0]; int ind = 0; for (int k = 1; k < n; k++) { if (a[k] > max) { max = a[k]; ind = k; } } if (max != 1) { a[ind] = 1; Arrays.sort(a); for (int i = 0; i < a.length - 1; i++) System.out.print(a[i] + " "); System.out.println(a[a.length - 1]); } else { a[0] = 2; Arrays.sort(a); for (int i = 0; i < a.length - 1; i++) System.out.print(a[i] + " "); System.out.println(a[a.length - 1]); } } }
nlogn
135_A. Replacement
CODEFORCES
import java.util.*; public class A135 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); } Arrays.sort(arr); int[] ans = new int[n]; if (arr[n-1] == 1) { for (int i = 0 ;i < n; i++) { ans[i] = arr[i]; } ans[n-1] = 2; } else { ans[0] = 1; for (int i = 1; i < n; i++) { ans[i] = arr[i-1]; } } StringBuffer buf = new StringBuffer(); for (int i = 0; i < n; i++) { buf.append(ans[i]); if (i != n-1) buf.append(' '); } System.out.print(buf.toString()); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Main { public static void main(String[] args) throws IOException { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(r.readLine()); String[] line = r.readLine().split("[ ]+"); int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = Integer.parseInt(line[i]); Arrays.sort(a); boolean found = false; for(int i = 0; i < n && !found; i++) if(a[i] != 1)found = true; if(found){ System.out.println(1); for(int i = 1; i < n; i++) System.out.println(a[i-1]); }else{ for(int i = 0; i < n-1; i++) System.out.println(1); System.out.println(2); } } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.*; import java.util.*; public class A { BufferedReader in; StringTokenizer st; PrintWriter out; String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } double nextDouble() throws Exception { return Double.parseDouble(next()); } void solve() throws Exception { // int min = 1; // int max = 1000000000; int n = nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); Arrays.sort(a); if (a[n - 1] == 1) { for (int i = 1; i < n; i++) out.print("1 "); out.print(2); return; } // a[0]=max; Arrays.sort(a); out.print(1); for (int i = 1; i < n; i++) out.print(" " + a[i-1]); } void run() { try { Locale.setDefault(Locale.US); boolean oj = System.getProperty("ONLINE_JUDGE") != null; Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("input.txt"); Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("output.txt"); in = new BufferedReader(reader); out = new PrintWriter(writer); solve(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } public static void main(String[] args) { new A().run(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.Arrays; import java.util.Scanner; public class Beta97B { static Scanner in; static StreamTokenizer st; static int n; static int[] a; static int max = 1; public static void main(String[] args) throws IOException { // in = new Scanner(System.in); st = new StreamTokenizer(new InputStreamReader(System.in)); n = nextInt(); a = new int[n]; int ind = 0; for (int i = 0; i < n; ++i) { a[i] = nextInt(); if (a[i] > max) { max = a[i]; ind = i; } } if (max == 1) { a[0] = 2; } else { a[ind] = 1; } Arrays.sort(a); for (int i = 0; i < n; ++i) System.out.print(a[i] + " "); } private static int nextInt() throws IOException { st.nextToken(); return (int) st.nval; } }
nlogn
135_A. Replacement
CODEFORCES
//package round97; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class A { static BufferedReader bf = new BufferedReader(new InputStreamReader( System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { String s = bf.readLine(); if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } static long nextLong() throws IOException { return Long.parseLong(nextToken()); } static String nextStr() throws IOException { return nextToken(); } static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public static void main(String[] args) throws IOException { int n = nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } Arrays.sort(a); for (int q = 0; q < n; q++) { if (a[q] != 1) { out.print("1"); for (int i = 1; i < n; i++) { out.print(" " + a[i - 1]); } out.flush(); return; } } for (int i = 0; i < n - 1; i++) { out.print("1 "); } out.println("2"); out.flush(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class A { void run(){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; boolean dif = false; for(int i=0;i<n;i++)a[i]=sc.nextInt(); Arrays.sort(a); if(n==1){ System.out.println(a[0]==1?2:1);return; } int[] m = new int[n]; for(int i=1;i<n;i++)if(a[i]!=a[i-1])dif=true; m[0] = 1; for(int i=1;i<n;i++)m[i]=a[i-1]; if(!dif&&a[0]==1)m[n-1]++; for(int i=0;i<n;i++)System.out.print(m[i]+(i==n-1?"\n":" ")); } public static void main(String[] args) { new A().run(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class ProblemA { private void solve() throws IOException { Scanner stdin = new Scanner(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = Integer.valueOf(stdin.nextInt()); int[] p = new int[n]; for (int i = 0; i < n; i++) { p[i] = stdin.nextInt(); } Arrays.sort(p); if (p[n-1] == 1) { p[n-1] = 2; } else { p[n-1] = 1; out.print(p[n-1] + " "); n--; } for (int i = 0; i < n; i++) { out.print(p[i] + " "); } out.flush(); out.close(); } public static void main(String[] args) throws IOException { ProblemA solver = new ProblemA(); solver.solve(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.util.*; import java.io.*; import static java.lang.Math.*; public class A{ public static void main(String[] args) throws Exception{ new A().run(); } void run() throws Exception{ //Scanner sc = new Scanner(System.in); BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(sc.readLine()); ArrayList<Integer> a = new ArrayList<Integer>(); //a.add(1); StringTokenizer st = new StringTokenizer(sc.readLine(), " "); boolean allOne = true; for(int i = 0; i < n; i++){ int val = Integer.parseInt(st.nextToken()); if(val!=1)allOne = false; a.add(val); } if(allOne){a.remove(n-1); a.add(2);} else a.add(1); Collections.sort(a); System.out.print(a.get(0)); for(int i = 1; i < n; i++) System.out.print(" " + a.get(i)); System.out.println(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.*; import java.util.*; public class A { final String filename = new String("A").toLowerCase(); void solve() throws Exception { int n = nextInt(); int[] a = new int[n]; int m = -1; for (int i = 0; i < n; i++) { a[i] = nextInt(); if (m == -1 || a[i] > a[m]) { m = i; } } if (a[m] == 1) a[m] = 2; else a[m] = 1; Arrays.sort(a); for (int i = 0; i < n; i++) { out.print(a[i] + " "); } } void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); // in = new BufferedReader(new FileReader("input.txt")); // out = new PrintWriter("output.txt"); solve(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } BufferedReader in; StringTokenizer st; PrintWriter out; String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(nextToken()); } long nextLong() throws Exception { return Long.parseLong(nextToken()); } double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } public static void main(String[] args) { new A().run(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.Arrays; import java.util.StringTokenizer; import java.util.logging.Level; import java.util.logging.Logger; public class ProblemA { private final BufferedReader in; private final PrintStream out; private StringTokenizer tok = new StringTokenizer(""); private String nextLine = null; public static void main(String[] args) throws Exception { new ProblemA(); } private ProblemA() throws Exception { in = new BufferedReader(new InputStreamReader(System.in)); out = System.out; start(); end(); } private int nextInt() { return Integer.parseInt(nextWord()); } private String nextWord() { if (tok.hasMoreTokens()) { return tok.nextToken(); } else { while (!tok.hasMoreTokens()) { try { nextLine = in.readLine(); if (nextLine == null) { return null; } else { tok = new StringTokenizer(nextLine); } } catch (IOException ex) { Logger.getLogger(ProblemA.class.getName()).log(Level.SEVERE, null, ex); } } return tok.nextToken(); } } private void start() { int n = nextInt(); int[] a = new int[n]; boolean allOne = true; for (int i = 0; i < n; i++) { a[i] = nextInt(); if (a[i] != 1) { allOne = false; } } Arrays.sort(a); int[] res = new int[n]; res[0] = 1; for (int i = 1; i < n; i++) { res[i] = a[i - 1]; } if (allOne) { for (int i = 0; i < n - 1; i++) { out.print(a[i] + " "); } out.print(2); } else { for (int i = 0; i < n; i++) { out.print(res[i] + " "); } } } private void end() { out.close(); } }
nlogn
135_A. Replacement
CODEFORCES
import java.util.*; public class Main { public static void main(String args[]) { (new Main()).solve(); } void solve() { Scanner cin = new Scanner(System.in); while( cin.hasNextInt() ) { int n = cin.nextInt(); int arr[] = new int[n]; for(int i=0; i<n; ++i) { arr[i] = cin.nextInt(); } Arrays.sort(arr); int ret[] = new int[n]; ret[0] = 1; for(int i=0; i<n-1; ++i) { ret[i + 1] = arr[i]; } if( arr[n - 1] == 1 ) { ret[n - 1] = 2; } String glue = ""; for(int i=0; i<n; ++i) { System.out.print(glue + ret[i]); glue = " "; } System.out.println(); } } }
nlogn
135_A. Replacement
CODEFORCES