src
stringlengths
95
64.6k
complexity
stringclasses
7 values
problem
stringlengths
6
50
from
stringclasses
1 value
import java.io.*; import java.text.*; import java.util.*; public class CottageVillage { static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static String LINE() throws Exception { return stdin.readLine(); } static String TOKEN() throws Exception { while (st == null || !st.hasMoreTokens())st = new StringTokenizer(LINE()); return st.nextToken(); } static int INT() throws Exception {return Integer.parseInt(TOKEN());} static long LONG() throws Exception {return Long.parseLong(TOKEN());} static double DOUBLE() throws Exception {return Double.parseDouble(TOKEN());} static DecimalFormat DF = new DecimalFormat("0.000",new DecimalFormatSymbols(Locale.ENGLISH)); public static final double EPSILON = 1E-9; public static void main(String[] args) throws Exception { int N = INT(), T = INT(); House[] list = new House[N]; for(int i = 0;i<N;i++) { list[i] = new House(INT(),INT()); } Arrays.sort(list); int cnt = 2; for(int i = 1;i<N;i++) { int room = list[i].center-list[i-1].center; if(2*T<2*room-list[i].side-list[i-1].side)cnt += 2; else if(2*T==2*room-list[i].side-list[i-1].side)cnt++; } System.out.println(cnt); } private static class House implements Comparable<House> { int center, side; House(int c, int s) { this.center = c; this.side = s; } public int compareTo(House h) { return this.center-h.center; } } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class A { static class Entity implements Comparable { public Entity(int x, int a) { this.x = x; this.a = a; } public int x, a; public int compareTo(Object t) { Entity o = (Entity) t; return x == o.x ? 0 : x < o.x ? -1 : 1; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(), t = 2 * scanner.nextInt(); if (1 == n) { System.out.println(2); } else { int rez = 2; ArrayList<Entity> list = new ArrayList<Entity>(); for (int i = 0; i < n; i++) { list.add(new Entity(scanner.nextInt(), scanner.nextInt())); } Collections.sort(list); for (int i = 1; i < n; i++) { int num = 2 * (list.get(i).x - list.get(i - 1).x) - list.get(i).a - list.get(i - 1).a; if (t < num) { rez += 2; } else if (t == num) { rez++; } } System.out.println(rez); } } }
nlogn
15_A. Cottage Village
CODEFORCES
/* * Hello! You are trying to hack my solution, are you? =) * Don't be afraid of the size, it's just a dump of useful methods like gcd, or n-th Fib number. * And I'm just too lazy to create a new .java for every task. * And if you were successful to hack my solution, please, send me this test as a message or to [email protected]. * It can help me improve my skills and i'd be very grateful for that. * Sorry for time you spent reading this message. =) * Good luck, unknown rival. =) * */ import java.io.*; import java.math.*; import java.util.*; public class Abra { // double d = 2.2250738585072012e-308; void solve() throws IOException { int n = nextInt(), t = nextInt(); TreeMap<Integer, Integer> tm = new TreeMap<Integer, Integer>(); for (int i = 0; i < n; i++) tm.put(nextInt(), nextInt()); double tx = -1e9; int c = 2; for (Map.Entry<Integer, Integer> m : tm.entrySet()) { if (tx == -1e9) { tx = m.getKey() + m.getValue() / 2.0 + t; continue; } if (m.getKey() - m.getValue() / 2.0 >= tx) c++; if (m.getKey() - m.getValue() / 2.0 > tx) c++; tx = m.getKey() + m.getValue() / 2.0 + t; } out.print(c); } public static void main(String[] args) throws IOException { new Abra().run(); } StreamTokenizer in; PrintWriter out; boolean oj; BufferedReader br; 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"); br = new BufferedReader(reader); in = new StreamTokenizer(br); out = new PrintWriter(writer); } long beginTime; void run() throws IOException { beginTime = System.currentTimeMillis(); long beginMem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); init(); solve(); long endMem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); long endTime = System.currentTimeMillis(); if (!oj) { System.out.println("Memory used = " + (endMem - beginMem)); System.out.println("Total memory = " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())); System.out.println("Running time = " + (endTime - beginTime)); } out.flush(); } 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; } myLib lib = new myLib(); void time() { System.out.print("It's "); System.out.println(System.currentTimeMillis() - beginTime); } static class myLib { 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.charAt(i) - '0'; } 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; } int digitCubesSum(int x) { int a = 0; while (x > 0) { a += (x % 10) * (x % 10) * (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 pif3D(double ax, double ay, double az, double bx, double by, double bz) { return Math.sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by) + (az - bz) * (az - bz)); } double pif3D(double[] a, double[] b) { return Math.sqrt((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]) + (a[2] - b[2]) * (a[2] - b[2])); } long gcd(long a, long b) { if (a == 0 || b == 0) return 1; 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 == 0 || b == 0) return 1; 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) { return a * b / gcd(a, b); } int lcm(int a, int b) { 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[] findPrimes(int x) { boolean[] forErato = new boolean[x - 1]; List<Integer> t = new Vector<Integer>(); int l = 0, j = 0; for (int i = 2; i < x; i++) { if (forErato[i - 2]) continue; t.add(i); l++; j = i * 2; while (j < x) { forErato[j - 2] = true; j += i; } } int[] primes = new int[l]; Iterator<Integer> iterator = t.iterator(); for (int i = 0; iterator.hasNext(); i++) { primes[i] = iterator.next().intValue(); } return primes; } 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; int[] ml = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; public myDate(int da, int ma, int ya) { d = da; m = ma; y = ya; if ((ma > 12 || ma < 1 || da > ml[ma - 1] || da < 1) && !(d == 29 && m == 2 && y % 4 == 0)) { d = 1; m = 1; y = 9999999; } } void incYear(int x) { for (int i = 0; i < x; i++) { y++; if (m == 2 && d == 29) { m = 3; d = 1; return; } if (m == 3 && d == 1) { if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) { m = 2; d = 29; } return; } } } boolean less(myDate x) { if (y < x.y) return true; if (y > x.y) return false; if (m < x.m) return true; if (m > x.m) return false; if (d < x.d) return true; if (d > x.d) return false; return true; } 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; } int rifmQuality(String a, String b) { if (a.length() > b.length()) { String c = a; a = b; b = c; } int c = 0, d = b.length() - a.length(); for (int i = a.length() - 1; i >= 0; i--) { if (a.charAt(i) == b.charAt(i + d)) c++; else break; } return c; } String numSym = "0123456789ABCDEF"; String ZFromXToYNotation(int x, int y, String z) { if (z.equals("0")) return "0"; String a = ""; // long q = 0, t = 1; BigInteger q = BigInteger.ZERO, t = BigInteger.ONE; for (int i = z.length() - 1; i >= 0; i--) { q = q.add(t.multiply(BigInteger.valueOf(z.charAt(i) - 48))); t = t.multiply(BigInteger.valueOf(x)); } while (q.compareTo(BigInteger.ZERO) == 1) { a = numSym.charAt((int) (q.mod(BigInteger.valueOf(y)).intValue())) + a; q = q.divide(BigInteger.valueOf(y)); } return a; } double angleFromXY(int x, int y) { if ((x == 0) && (y > 0)) return Math.PI / 2; if ((x == 0) && (y < 0)) return -Math.PI / 2; if ((y == 0) && (x > 0)) return 0; if ((y == 0) && (x < 0)) return Math.PI; if (x > 0) return Math.atan((double) y / x); else { if (y > 0) return Math.atan((double) y / x) + Math.PI; else return Math.atan((double) y / x) - Math.PI; } } static boolean isNumber(String x) { try { Integer.parseInt(x); } catch (NumberFormatException ex) { return false; } return true; } static boolean stringContainsOf(String x, String c) { for (int i = 0; i < x.length(); i++) { if (c.indexOf(x.charAt(i)) == -1) return false; } return true; } long pow(long a, long n) { // b > 0 if (n == 0) return 1; long k = n, b = 1, c = a; while (k != 0) { if (k % 2 == 0) { k /= 2; c *= c; } else { k--; b *= c; } } return b; } int pow(int a, int n) { // b > 0 if (n == 0) return 1; int k = n, b = 1, c = a; while (k != 0) { if (k % 2 == 0) { k /= 2; c *= c; } else { k--; b *= c; } } return b; } double pow(double a, int n) { // b > 0 if (n == 0) return 1; double k = n, b = 1, c = a; while (k != 0) { if (k % 2 == 0) { k /= 2; c *= c; } else { k--; b *= c; } } return b; } double log2(double x) { return Math.log(x) / Math.log(2); } int lpd(int[] primes, int x) {// least prime divisor int i; for (i = 0; primes[i] <= x / 2; i++) { if (x % primes[i] == 0) { return primes[i]; } } ; return x; } int np(int[] primes, int x) {// number of prime number for (int i = 0; true; i++) { if (primes[i] == x) return i; } } int[] dijkstra(int[][] map, int n, int s) { int[] p = new int[n]; boolean[] b = new boolean[n]; Arrays.fill(p, Integer.MAX_VALUE); p[s] = 0; b[s] = true; for (int i = 0; i < n; i++) { if (i != s) p[i] = map[s][i]; } while (true) { int m = Integer.MAX_VALUE, mi = -1; for (int i = 0; i < n; i++) { if (!b[i] && (p[i] < m)) { mi = i; m = p[i]; } } if (mi == -1) break; b[mi] = true; for (int i = 0; i < n; i++) if (p[mi] + map[mi][i] < p[i]) p[i] = p[mi] + map[mi][i]; } return p; } boolean isLatinChar(char x) { if (((x >= 'a') && (x <= 'z')) || ((x >= 'A') && (x <= 'Z'))) return true; else return false; } boolean isBigLatinChar(char x) { if (x >= 'A' && x <= 'Z') return true; else return false; } boolean isSmallLatinChar(char x) { if (x >= 'a' && x <= 'z') return true; else return false; } boolean isDigitChar(char x) { if (x >= '0' && x <= '9') return true; else return false; } class NotANumberException extends Exception { private static final long serialVersionUID = 1L; String mistake; NotANumberException() { mistake = "Unknown."; } NotANumberException(String message) { mistake = message; } } class Real { String num = "0"; long exp = 0; boolean pos = true; long length() { return num.length(); } void check(String x) throws NotANumberException { if (!stringContainsOf(x, "0123456789+-.eE")) throw new NotANumberException("Illegal character."); long j = 0; for (long i = 0; i < x.length(); i++) { if ((x.charAt((int) i) == '-') || (x.charAt((int) i) == '+')) { if (j == 0) j = 1; else if (j == 5) j = 6; else throw new NotANumberException("Unexpected sign."); } else if ("0123456789".indexOf(x.charAt((int) i)) != -1) { if (j == 0) j = 2; else if (j == 1) j = 2; else if (j == 2) ; else if (j == 3) j = 4; else if (j == 4) ; else if (j == 5) j = 6; else if (j == 6) ; else throw new NotANumberException("Unexpected digit."); } else if (x.charAt((int) i) == '.') { if (j == 0) j = 3; else if (j == 1) j = 3; else if (j == 2) j = 3; else throw new NotANumberException("Unexpected dot."); } else if ((x.charAt((int) i) == 'e') || (x.charAt((int) i) == 'E')) { if (j == 2) j = 5; else if (j == 4) j = 5; else throw new NotANumberException("Unexpected exponent."); } else throw new NotANumberException("O_o."); } if ((j == 0) || (j == 1) || (j == 3) || (j == 5)) throw new NotANumberException("Unexpected end."); } public Real(String x) throws NotANumberException { check(x); if (x.charAt(0) == '-') pos = false; long j = 0; String e = ""; boolean epos = true; for (long i = 0; i < x.length(); i++) { if ("0123456789".indexOf(x.charAt((int) i)) != -1) { if (j == 0) num += x.charAt((int) i); if (j == 1) { num += x.charAt((int) i); exp--; } if (j == 2) e += x.charAt((int) i); } if (x.charAt((int) i) == '.') { if (j == 0) j = 1; } if ((x.charAt((int) i) == 'e') || (x.charAt((int) i) == 'E')) { j = 2; if (x.charAt((int) (i + 1)) == '-') epos = false; } } while ((num.length() > 1) && (num.charAt(0) == '0')) num = num.substring(1); while ((num.length() > 1) && (num.charAt(num.length() - 1) == '0')) { num = num.substring(0, num.length() - 1); exp++; } if (num.equals("0")) { exp = 0; pos = true; return; } while ((e.length() > 1) && (e.charAt(0) == '0')) e = e.substring(1); try { if (e != "") if (epos) exp += Long.parseLong(e); else exp -= Long.parseLong(e); } catch (NumberFormatException exc) { if (!epos) { num = "0"; exp = 0; pos = true; } else { throw new NotANumberException("Too long exponent"); } } } public Real() { } String toString(long mantissa) { String a = "", b = ""; if (exp >= 0) { a = num; if (!pos) a = '-' + a; for (long i = 0; i < exp; i++) a += '0'; for (long i = 0; i < mantissa; i++) b += '0'; if (mantissa == 0) return a; else return a + "." + b; } else { if (exp + length() <= 0) { a = "0"; if (mantissa == 0) { return a; } if (mantissa < -(exp + length() - 1)) { for (long i = 0; i < mantissa; i++) b += '0'; return a + "." + b; } else { if (!pos) a = '-' + a; for (long i = 0; i < mantissa; i++) if (i < -(exp + length())) b += '0'; else if (i + exp >= 0) b += '0'; else b += num.charAt((int) (i + exp + length())); return a + "." + b; } } else { if (!pos) a = "-"; for (long i = 0; i < exp + length(); i++) a += num.charAt((int) i); if (mantissa == 0) return a; for (long i = exp + length(); i < exp + length() + mantissa; i++) if (i < length()) b += num.charAt((int) i); else b += '0'; return a + "." + b; } } } } boolean containsRepeats(int... num) { Set<Integer> s = new TreeSet<Integer>(); for (int d : num) if (!s.contains(d)) s.add(d); else return true; return false; } int[] rotateDice(int[] a, int n) { int[] c = new int[6]; if (n == 0) { c[0] = a[1]; c[1] = a[5]; c[2] = a[2]; c[3] = a[0]; c[4] = a[4]; c[5] = a[3]; } if (n == 1) { c[0] = a[2]; c[1] = a[1]; c[2] = a[5]; c[3] = a[3]; c[4] = a[0]; c[5] = a[4]; } if (n == 2) { c[0] = a[3]; c[1] = a[0]; c[2] = a[2]; c[3] = a[5]; c[4] = a[4]; c[5] = a[1]; } if (n == 3) { c[0] = a[4]; c[1] = a[1]; c[2] = a[0]; c[3] = a[3]; c[4] = a[5]; c[5] = a[2]; } if (n == 4) { c[0] = a[0]; c[1] = a[2]; c[2] = a[3]; c[3] = a[4]; c[4] = a[1]; c[5] = a[5]; } if (n == 5) { c[0] = a[0]; c[1] = a[4]; c[2] = a[1]; c[3] = a[2]; c[4] = a[3]; c[5] = a[5]; } return c; } int min(int... a) { int c = Integer.MAX_VALUE; for (int d : a) if (d < c) c = d; return c; } int max(int... a) { int c = Integer.MIN_VALUE; for (int d : a) if (d > c) c = d; return c; } int pos(int x) { if (x > 0) return x; else return 0; } long pos(long x) { if (x > 0) return x; else return 0; } double maxD(double... a) { double c = Double.MIN_VALUE; for (double d : a) if (d > c) c = d; return c; } double minD(double... a) { double c = Double.MAX_VALUE; for (double d : a) if (d < c) c = d; return c; } int[] normalizeDice(int[] a) { int[] c = a.clone(); if (c[0] != 0) if (c[1] == 0) c = rotateDice(c, 0); else if (c[2] == 0) c = rotateDice(c, 1); else if (c[3] == 0) c = rotateDice(c, 2); else if (c[4] == 0) c = rotateDice(c, 3); else if (c[5] == 0) c = rotateDice(rotateDice(c, 0), 0); while (c[1] != min(c[1], c[2], c[3], c[4])) c = rotateDice(c, 4); return c; } boolean sameDice(int[] a, int[] b) { for (int i = 0; i < 6; i++) if (a[i] != b[i]) return false; return true; } final double goldenRatio = (1 + Math.sqrt(5)) / 2; final double aGoldenRatio = (1 - Math.sqrt(5)) / 2; long Fib(int n) { if (n < 0) if (n % 2 == 0) return -Math.round((pow(goldenRatio, -n) - pow(aGoldenRatio, -n)) / Math.sqrt(5)); else return -Math.round((pow(goldenRatio, -n) - pow(aGoldenRatio, -n)) / Math.sqrt(5)); return Math.round((pow(goldenRatio, n) - pow(aGoldenRatio, n)) / Math.sqrt(5)); } class japaneeseComparator implements Comparator<String> { @Override public int compare(String a, String b) { int ai = 0, bi = 0; boolean m = false, ns = false; if (a.charAt(ai) <= '9' && a.charAt(ai) >= '0') { if (b.charAt(bi) <= '9' && b.charAt(bi) >= '0') m = true; else return -1; } if (b.charAt(bi) <= '9' && b.charAt(bi) >= '0') { if (a.charAt(ai) <= '9' && a.charAt(ai) >= '0') m = true; else return 1; } a += "!"; b += "!"; int na = 0, nb = 0; while (true) { if (a.charAt(ai) == '!') { if (b.charAt(bi) == '!') break; return -1; } if (b.charAt(bi) == '!') { return 1; } if (m) { int ab = -1, bb = -1; while (a.charAt(ai) <= '9' && a.charAt(ai) >= '0') { if (ab == -1) ab = ai; ai++; } while (b.charAt(bi) <= '9' && b.charAt(bi) >= '0') { if (bb == -1) bb = bi; bi++; } m = !m; if (ab == -1) { if (bb == -1) continue; else return 1; } if (bb == -1) return -1; while (a.charAt(ab) == '0' && ab + 1 != ai) { ab++; if (!ns) na++; } while (b.charAt(bb) == '0' && bb + 1 != bi) { bb++; if (!ns) nb++; } if (na != nb) ns = true; if (ai - ab < bi - bb) return -1; if (ai - ab > bi - bb) return 1; for (int i = 0; i < ai - ab; i++) { if (a.charAt(ab + i) < b.charAt(bb + i)) return -1; if (a.charAt(ab + i) > b.charAt(bb + i)) return 1; } } else { m = !m; while (true) { if (a.charAt(ai) <= 'z' && a.charAt(ai) >= 'a' && b.charAt(bi) <= 'z' && b.charAt(bi) >= 'a') { if (a.charAt(ai) < b.charAt(bi)) return -1; if (a.charAt(ai) > b.charAt(bi)) return 1; ai++; bi++; } else if (a.charAt(ai) <= 'z' && a.charAt(ai) >= 'a') return 1; else if (b.charAt(bi) <= 'z' && b.charAt(bi) >= 'a') return -1; else break; } } } if (na < nb) return 1; if (na > nb) return -1; return 0; } } Random random = new Random(); } void readIntArray(int[] a) throws IOException { for (int i = 0; i < a.length; i++) a[i] = nextInt(); } String readChars(int l) throws IOException { String r = ""; for (int i = 0; i < l; i++) r += (char) br.read(); return r; } class myFraction { long num = 0, den = 1; void reduce() { long d = lib.gcd(num, den); num /= d; den /= d; } myFraction(long ch, long zn) { num = ch; den = zn; reduce(); } myFraction add(myFraction t) { long nd = lib.lcm(den, t.den); myFraction r = new myFraction(nd / den * num + nd / t.den * t.num, nd); r.reduce(); return r; } public String toString() { return num + "/" + den; } } class myPoint { myPoint(int a, int b) { x = a; y = b; } int x, y; boolean equals(myPoint a) { if (x == a.x && y == a.y) return true; else return false; } public String toString() { return x + ":" + y; } } /* * class cubeWithLetters { String consts = "ЧКТФЭЦ"; char[][] letters = { { * 'А', 'Б', 'Г', 'В' }, { 'Д', 'Е', 'З', 'Ж' }, { 'И', 'Л', 'Н', 'М' }, { * 'О', 'П', 'С', 'Р' }, { 'У', 'Х', 'Щ', 'Ш' }, { 'Ы', 'Ь', 'Я', 'Ю' } }; * * char get(char x) { if (consts.indexOf(x) != -1) return x; for (int i = 0; * i < 7; i++) { for (int j = 0; j < 4; j++) { if (letters[i][j] == x) { if * (j == 0) return letters[i][3]; else return letters[i][j - 1]; } } } * return '!'; } * * void subrotate(int x) { char t = letters[x][0]; letters[x][0] = * letters[x][3]; letters[x][3] = letters[x][2]; letters[x][2] = * letters[x][1]; letters[x][1] = t; } * * void rotate(int x) { subrotate(x); char t; if (x == 0) { t = * letters[1][0]; letters[1][0] = letters[2][0]; letters[2][0] = * letters[3][0]; letters[3][0] = letters[5][2]; letters[5][2] = t; * * t = letters[1][1]; letters[1][1] = letters[2][1]; letters[2][1] = * letters[3][1]; letters[3][1] = letters[5][3]; letters[5][3] = t; } if (x * == 1) { t = letters[2][0]; letters[2][0] = letters[0][0]; letters[0][0] = * letters[5][0]; letters[5][0] = letters[4][0]; letters[4][0] = t; * * t = letters[2][3]; letters[2][3] = letters[0][3]; letters[0][3] = * letters[5][3]; letters[5][3] = letters[4][3]; letters[4][3] = t; } if (x * == 2) { t = letters[0][3]; letters[0][3] = letters[1][2]; letters[1][2] = * letters[4][1]; letters[4][1] = letters[3][0]; letters[3][0] = t; * * t = letters[0][2]; letters[0][2] = letters[1][1]; letters[1][1] = * letters[4][0]; letters[4][0] = letters[3][3]; letters[3][3] = t; } if (x * == 3) { t = letters[2][1]; letters[2][1] = letters[4][1]; letters[4][1] = * letters[5][1]; letters[5][1] = letters[0][1]; letters[0][1] = t; * * t = letters[2][2]; letters[2][2] = letters[4][2]; letters[4][2] = * letters[5][2]; letters[5][2] = letters[0][2]; letters[0][2] = t; } if (x * == 4) { t = letters[2][3]; letters[2][3] = letters[1][3]; letters[1][3] = * letters[5][1]; letters[5][1] = letters[3][3]; letters[3][3] = t; * * t = letters[2][2]; letters[2][2] = letters[1][2]; letters[1][2] = * letters[5][0]; letters[5][0] = letters[3][2]; letters[3][2] = t; } if (x * == 5) { t = letters[4][3]; letters[4][3] = letters[1][0]; letters[1][0] = * letters[0][1]; letters[0][1] = letters[3][2]; letters[3][2] = t; * * t = letters[4][2]; letters[4][2] = letters[1][3]; letters[1][3] = * letters[0][0]; letters[0][0] = letters[3][1]; letters[3][1] = t; } } * * public String toString(){ return " " + letters[0][0] + letters[0][1] + * "\n" + " " + letters[0][3] + letters[0][2] + "\n" + letters[1][0] + * letters[1][1] + letters[2][0] + letters[2][1] + letters[3][0] + * letters[3][1] + "\n" + letters[1][3] + letters[1][2] + letters[2][3] + * letters[2][2] + letters[3][3] + letters[3][2] + "\n" + " " + * letters[4][0] + letters[4][1] + "\n" + " " + letters[4][3] + * letters[4][2] + "\n" + " " + letters[5][0] + letters[5][1] + "\n" + " " * + letters[5][3] + letters[5][2] + "\n"; } } * * * Vector<Integer>[] a; int n, mc, c1, c2; int[] col; * * void wave(int x, int p) { for (Iterator<Integer> i = a[x].iterator(); * i.hasNext(); ) { int t = i.next(); if (t == x || t == p) continue; if * (col[t] == 0) { col[t] = mc; wave(t, x); } else { c1 = x; c2 = t; } } } * * void solve() throws IOException { * * String s = "ЕПОЕЬРИТСГХЖЗТЯПСТАПДСБИСТЧК"; //String s = * "ЗЬУОЫТВЗТЯПУБОЫТЕАЫШХЯАТЧК"; cubeWithLetters cube = new * cubeWithLetters(); for (int x = 0; x < 4; x++) { for (int y = x + 1; y < * 5; y++) { for (int z = y + 1; z < 6; z++) { cube = new cubeWithLetters(); * out.println(cube.toString()); cube.rotate(x); * out.println(cube.toString()); cube.rotate(y); * out.println(cube.toString()); cube.rotate(z); * out.println(cube.toString()); out.print(x + " " + y + " " + z + " = "); * for (int i = 0; i < s.length(); i++) { out.print(cube.get(s.charAt(i))); * } out.println(); } } } * * int a = nextInt(), b = nextInt(), x = nextInt(), y = nextInt(); * out.print((lib.min(a / (x / lib.gcd(x, y)), b / (y / lib.gcd(x, y))) * (x * / lib.gcd(x, y))) + " " + (lib.min(a / (x / lib.gcd(x, y)), b / (y / * lib.gcd(x, y))) * (y / lib.gcd(x, y)))); } */ }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class AA { static class Pair implements Comparable<Pair> { public int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair p) { if (p.x != x) return x - p.x; return y - p.y; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int t = in.nextInt() * 2; List<Pair> l = new ArrayList<Pair>(); for (int i = 0; i < n; i++) { int c = in.nextInt() * 2; int a = in.nextInt(); l.add(new Pair(c - a, c + a)); } Collections.sort(l); int ret = 2; for (int i = 1; i < n; i++) { if (l.get(i).x - l.get(i-1).y > t) ret += 2; else if (l.get(i).x - l.get(i-1).y == t) ret += 1; } System.out.println(ret); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.fill; import static java.util.Arrays.binarySearch; import static java.util.Arrays.sort; 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(""); void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int N = nextInt(); int T = nextInt(); Pair[] p = new Pair [N]; for (int i = 0; i < N; i++) p[i] = new Pair(nextInt(), nextInt()); sort(p); int ans = 2; for (int i = 1; i < N; i++) { int dif = (2 * p[i].x - p[i].a) - (2 * p[i - 1].x + p[i - 1].a); if (dif == 2 * T) ans++; else if (dif > 2 * T) ans += 2; } out.println(ans); out.close(); } class Pair implements Comparable<Pair> { int x, a; public Pair(int xx, int aa) { x = xx; a = aa; } @Override public int compareTo(Pair p) { return x < p.x ? -1 : 1; } } 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
15_A. Cottage Village
CODEFORCES
import java.io.BufferedReader; import static java.lang.Math.*; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class CodeforcesA implements Runnable { public static final String taskname = "A"; BufferedReader in; PrintWriter out; StringTokenizer tok; public static void main(String[] args) { new Thread(new CodeforcesA()).start(); } static class Square implements Comparable<Square>{ int x, a; public Square(int x, int a) { this.x = x; this.a = a; } @Override public int compareTo(Square o) { return x - o.x; } int distance(Square a, int t) { double dist = a.x - x - this.a / 2.0 - a.a / 2.0; if(dist > t) return 2; else if(abs(dist - t) < 1e-9) return 1; return 0; } public String toString() { return x + " " + a; } } void solve() throws IOException { int n = nextInt(), t = nextInt(); Square[] x = new Square[n]; for(int i = 0; i < n; ++i) { x[i] = new Square(nextInt(), nextInt()); } Arrays.sort(x); long res = 2; for(int i = 0; i < n - 1; ++i) { res += x[i].distance(x[i + 1], t); } out.println(res); } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); //in = new BufferedReader(new FileReader(new File("input.txt"))); out = new PrintWriter(System.out); //out = new PrintWriter(new File("output.txt")); solve(); out.flush(); out.close(); in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } String nextLine() throws IOException { tok = null; return in.readLine(); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } String nextToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class CottageVillage { class cl { int x=0; int a=0; cl(int x, int a){ this.x=x; this.a=a; } } class cmp implements Comparator<cl> { public int compare(cl d1, cl d2) { return d1.x<d2.x ? -1 : 1; } } public CottageVillage() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); cl[] w = new cl[n]; for(int i=0; i<n; i++) w[i] = new cl(sc.nextInt(), sc.nextInt()); Arrays.sort(w, new cmp()); int cnt=2, diff=0; for(int i=1; i<n; i++) { diff = Math.abs(2*w[i].x-2*w[i-1].x-w[i].a-w[i-1].a)-2*k; if (diff>0) cnt+=2; else if (diff==0) cnt++; } System.out.println(cnt); } public static void main(String... args) { new CottageVillage(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; import java.text.*; import java.math.*; public class A { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int t = Integer.parseInt(st.nextToken()); State[] s = new State[n]; for(int i = 0; i < n; i++) { st = new StringTokenizer(br.readLine()); s[i] = new State(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())); } Arrays.sort(s); int num = 2; for(int i = 1; i < s.length; i++) { int dist = s[i].x - s[i-1].x; dist *= 2; int size = s[i].d + s[i-1].d; size += 2 * t; if(dist == size) num++; else if(dist > size) num += 2; } System.out.println(num); } static class State implements Comparable<State> { public int x,d; public State(int a, int b) { x=a; d=b; } public int compareTo(State s) { return x - s.x; } } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; public class Task15a { public static class House implements Comparable<House>{ int x, s; public House(int x, int s) { super(); this.x = x; this.s = s; } public int compareTo(House o) { return x - o.x; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int t = sc.nextInt() * 2; House[] hs = new House[n]; for (int i = 0; i < n; i++){ hs[i] = new House(sc.nextInt()*2, sc.nextInt()); } Arrays.sort(hs); int res = 2; for (int i = 0; i < n - 1; i++) { int curr = hs[i+1].x - hs[i].x - hs[i+1].s - hs[i].s; if (curr > t) res += 2; if (curr == t) res += 1; } System.out.println(res); } }
nlogn
15_A. Cottage Village
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 _P015A{ Scanner sc=new Scanner(System.in); int INF=1<<28; double EPS=1e-9; int n, t; int[][] a; void run(){ n=sc.nextInt(); t=sc.nextInt(); a=new int[n][2]; for(int i=0; i<n; i++){ a[i][0]=sc.nextInt(); a[i][1]=sc.nextInt(); } solve(); } void solve(){ sort(a, new Comparator<int[]>(){ @Override public int compare(int[] a0, int[] a1){ return a0[0]-a1[0]; } }); int ans=2; for(int i=0; i<n-1; i++){ int s=(a[i+1][0]*2-a[i+1][1])-(a[i][0]*2+a[i][1]); if(s>t*2){ ans+=2; }else if(s==t*2){ ans++; } } println(ans+""); } void println(String s){ System.out.println(s); } void print(String s){ System.out.print(s); } void debug(Object... os){ System.err.println(Arrays.deepToString(os)); } public static void main(String[] args){ new _P015A().run(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; public class ProblemA { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int t = s.nextInt(); TreeMap<Integer,Integer> map = new TreeMap<Integer,Integer>(); // int x = 0 ; while(s.hasNextInt()) { int i = s.nextInt(); int j = s.nextInt(); map.put(i,j); // x++; // if(x == 2) // break; } int count = 0; double left = -100000; double right; int size; for(Integer i : map.keySet()) { size = map.get(i); right = (double)i - (double)size/2.0; if(right - left > t) { count+=2; } if(right - left == t) { count++; } left = (double)i + (double)size/2.0; } System.out.println(count); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; public class CottageVillage { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int size=sc.nextInt(); int side=sc.nextInt(); ArrayList<Pair> lis=new ArrayList<Pair>(); for(int x=0;x<size;x++) { lis.add(new Pair(sc.nextInt(), sc.nextInt())); } Collections.sort(lis); int count=2; for(int x=0;x<lis.size()-1;x++) { Pair a=lis.get(x); Pair b=lis.get(x+1); double na=a.x+a.len/2; double nb=b.x-b.len/2; //System.out.println(na+" "+nb); if(na<nb) { double dif=Math.abs(nb-na); if(dif==side)count++; else if(dif>side)count+=2; } } System.out.println(count); } } class Pair implements Comparable<Pair> { int x; double len; public Pair(int a,int b) { x=a; len=b; } public int compareTo(Pair o) { return x-o.x; } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; public class Main { // static Scanner in; static PrintWriter out; static StreamTokenizer in; static int next() throws Exception {in.nextToken(); return (int) in.nval;} public static void main(String[] args) throws Exception { // in = new Scanner(System.in); out = new PrintWriter(System.out); in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); int n = next(); int t = 2*next(); int[] x = new int[n]; int[] a = new int[n]; for (int i = 0; i < n; i++) { x[i] = 2* next() + 2000; a[i] = next(); } int[] srt = new int[n]; for (int i = 0; i < n; i++) srt[i] = 10000 * x[i] + a[i]; Arrays.sort(srt); for (int i = 0; i < n; i++) { x[i] = srt[i] / 10000; a[i] = srt[i] % 10000; } int answ = 2; for (int i = 0; i < n - 1; i++) { if (x[i + 1] - x[i] > a[i] + a[i + 1] + t) answ++; if (x[i + 1] - x[i] >= a[i] + a[i + 1] + t) answ++; } out.println(answ); out.close(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; public class Beta15PA { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Beta15PA temp = new Beta15PA(); temp.solve(); } public void solve() { Scanner scan = new Scanner(System.in); int n = scan.nextInt(), t = scan.nextInt(); House[] houses = new House[n]; for(int i=0; i<n; i++) { houses[i] = new House(scan.nextInt(), scan.nextInt()); } Arrays.sort(houses); int res = 2; for(int i=0; i<n-1; i++) { double cnt = houses[i+1].coordinate - houses[i].coordinate; cnt -= 1.0*(houses[i+1].side+houses[i].side)/2; if(cnt>t) res += 2; else if(Math.abs(cnt-t)<1e-7) res += 1; } System.out.println(res); } public class House implements Comparable<House> { public int coordinate, side; public House(int coordinate, int side) { this.coordinate = coordinate; this.side = side; } @Override public int compareTo(House arg0) { // TODO Auto-generated method stub return this.coordinate - arg0.coordinate; } } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class ProblemA { public static void main(String[] args) throws IOException { BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); String[] line = s.readLine().split(" "); int n = Integer.valueOf(line[0]); int ht = Integer.valueOf(line[1]); int[][] house = new int[n][2]; Set<Integer> candidates = new HashSet<Integer>(); for (int i = 0 ; i < n ; i++) { String[] data = s.readLine().split(" "); house[i][0] = Integer.valueOf(data[0]) * 2; house[i][1] = Integer.valueOf(data[1]); candidates.add(house[i][0] - house[i][1] - ht); candidates.add(house[i][0] + house[i][1] + ht); } int ans = 0; for (int p : candidates) { int f = p - ht; int t = p + ht; boolean isok = true; for (int i = 0 ; i < n ; i++) { if (house[i][0] + house[i][1] <= f) { } else if (house[i][0] - house[i][1] >= t) { } else { isok = false; break; } } if (isok) { ans++; } } out.println(ans); out.flush(); } public static void debug(Object... os){ System.err.println(Arrays.deepToString(os)); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.Scanner; import java.util.TreeMap; /** * * @author camoroh13 */ public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); // Scanner sc = new Scanner(new FileInputStream("/home/camoroh13/NetBeansProjects/JavaApplication1/src/input.txt")); int n = sc.nextInt(); int t = sc.nextInt(); // int[][] h = new int[n][2]; TreeMap<Integer, Integer> h = new TreeMap<Integer, Integer>(); for (int i=0; i < n; i++) { int key = sc.nextInt(); h.put(key, sc.nextInt()); } int ans = 2; Integer lastKey = h.firstKey(); Integer last = h.get(lastKey); h.remove(lastKey); for (int i=1; i < n; i++) { int key = h.firstKey(); int val = h.get(key); //System.out.println(Math.abs(key-val*1.0/2 - (lastKey + last*1.0/2)) + "-" + key + "-"+val); if (Math.abs(key-val*1.0/2 - (lastKey + last*1.0/2)) == t) { ans++; } else if (Math.abs(key-val*1.0/2 - (lastKey + last*1.0/2)) > t) { ans += 2; } lastKey = key; last = val; h.remove(lastKey); } System.out.println(ans); sc.close(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.awt.Point; import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class Solution15A { 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 Solution15A().run(); } 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); } } static class Utils { private Utils() {} public static void mergeSort(int[] a) { mergeSort(a, 0, a.length - 1); } private static void mergeSort(int[] a, int leftIndex, int rightIndex) { final int MAGIC_VALUE = 50; if (leftIndex < rightIndex) { if (rightIndex - leftIndex <= MAGIC_VALUE) { insertionSort(a, leftIndex, rightIndex); } else { int middleIndex = (leftIndex + rightIndex) / 2; mergeSort(a, leftIndex, middleIndex); mergeSort(a, middleIndex + 1, rightIndex); merge(a, leftIndex, middleIndex, rightIndex); } } } private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - leftIndex + 1; int length2 = rightIndex - middleIndex; int[] leftArray = new int[length1]; int[] rightArray = new int[length2]; System.arraycopy(a, leftIndex, leftArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = leftArray[i++]; } else { a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++]; } } } private static void insertionSort(int[] a, int leftIndex, int rightIndex) { for (int i = leftIndex + 1; i <= rightIndex; i++) { int current = a[i]; int j = i - 1; while (j >= leftIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } } class Square implements Comparable<Square>{ public Square(int x, int a){ this.x = x; this.a = a; } @Override public int compareTo(Square o) { if(this.x > o.x) return 1; if(this.x < o.x) return -1; return 0; } public int a, x; } void solve() throws IOException{ int n = readInt(); int t = readInt(); Square[] houses = new Square[n]; for(int i = 0; i < n; i++){ int a = readInt(); int b = readInt(); houses[i] = new Square(a, b); } Arrays.sort(houses); int count = 0; for(int i = 0; i < n; i++){ if(i == 0) count++; else{ if(houses[i].x - houses[i].a/2.0 - t > houses[i-1].x + houses[i-1].a/2.0) count++; } if(i == n - 1) count++; else{ if(houses[i].x + houses[i].a/2.0 + t <= houses[i+1].x - houses[i+1].a/2.0) count++; } } out.println(count); } static double distance(long x1, long y1, long x2, long y2){ return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } static long gcd(long a, long b){ while(a != b){ if(a < b) a -=b; else b -= a; } return a; } static long lcm(long a, long b){ return a * b /gcd(a, b); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.Scanner; /** * @author Son-Huy TRAN * */ public class P15A_CottageVillage { /** * @param args */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int t = scanner.nextInt(); scanner.nextLine(); int[] x = new int[n]; int[] a = new int[n]; for (int i = 0; i < n; i++) { x[i] = scanner.nextInt(); a[i] = scanner.nextInt(); scanner.nextLine(); } scanner.close(); for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (x[i] > x[j]) { swap(x, i, j); swap(a, i, j); } } } int countPositions = 2; for (int i = 1; i < n; i++) { double left = x[i - 1] + a[i - 1] * 1.0 / 2; double right = x[i] - a[i] * 1.0 / 2; double length = right - left; if (length == (double) t) { countPositions++; } else if (length > t) { countPositions += 2; } } System.out.println(countPositions); } private static void swap(int[] numbers, int i, int j) { int temp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = temp; } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.awt.Point; import java.util.Arrays; import java.util.Scanner; public class p15a { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int t = in.nextInt(); if(n == 1) { System.out.println(2); return; } house[] all = new house[n]; for (int i = 0; i < all.length; i++) { all[i] = new house(in.nextInt(),in.nextInt()); } Arrays.sort(all); int count = 0; for (int i = 0; i < all.length; i++) { double left = all[i].center - (all[i].side*1.0/2); double right = all[i].center + (all[i].side*1.0/2); if(i == 0) { count++; double left2 = all[i+1].center - (all[i+1].side*1.0/2); if(right+t<left2) { count++; } continue; } if(i == all.length-1) { count++; double right2 = all[i-1].center + (all[i-1].side*1.0/2); if(left-t>= right2) { count++; } continue; } double left2 = all[i+1].center - (all[i+1].side*1.0/2); double right2 = all[i-1].center + (all[i-1].side*1.0/2); if(right+t<left2) { count++; } if(left-t>=right2) count++; } System.out.println(count); } } class house implements Comparable<house>{ int center; int side; public house(int a , int b) { center = a; side = b; } public int compareTo(house o) { return center-o.center; } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class P015A { public static void main(String[] args) { Scanner inScanner = new Scanner(System.in); int n = inScanner.nextInt(); int t = inScanner.nextInt(); House[] houses = new House[n]; for (int i = 0; i < n; i++) houses[i] = new House(inScanner.nextInt(), inScanner.nextInt()); Arrays.sort(houses); int sum = 2; for (int i = 1; i < n; i++) { double space = houses[i].leftX - houses[i - 1].rightX; if (space >= t) sum++; if (space > t) sum++; } System.out.println(sum); } private static class House implements Comparable<House> { int x; double leftX; double rightX; public House(int x, int size) { super(); this.x = x; leftX = x - (double) size / 2; rightX = x + (double) size / 2; } @Override public int compareTo(House o) { return x - o.x; } } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.Scanner; public class A015 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), t = in.nextInt(); int[] centers = new int[n], sides = new int[n]; for (int x = 0; x < n; x++) { centers[x] = in.nextInt(); sides[x] = in.nextInt(); } int count = 0; big: for (int x = -4000; x <= 4000; x++) { boolean touch = false; for (int i = 0; i < n; i++) { int d = 2*centers[i] - x; d = d > 0 ? d : -d; int s = t + sides[i]; if (s == d) { touch = true; } else if (s > d) { continue big; } } if (touch) count++; } System.out.println(count); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; public class Main { public static void main(String[]args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int t = sc.nextInt(); double [] left = new double[n]; double [] right = new double[n]; for (int i = 0; i<n; i++){ int x = sc.nextInt(); int a = sc.nextInt(); double l = x - (double)a/2; double r = x + (double)a/2; left[i] = l; right[i] = r; } int answer = 2; quickSort(left, right, 0, n-1); for (int i = 0; i<n-1; i++){ if (left[i+1] - right[i] == t){ answer++; } if (left[i+1] - right[i] > t){ answer += 2; } } System.out.println(answer); } //////////////////////////////////////////////////////////////// int partition(double arr[], int left, int right) { int i = left, j = right; double tmp; double pivot = arr[(left + right) / 2]; while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } }; return i; } void quickSort(double arr[], int left, int right) { int index = partition(arr, left, right); if (left < index - 1) quickSort(arr, left, index - 1); if (index < right) quickSort(arr,index, right); } ///////////////////////////////////////////////////////////////////// static int partition(double arr[], double arr2[], int left, int right) { int i = left, j = right; double tmp; double tmp2; double pivot = arr[(left + right) / 2]; while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; tmp2 = arr2[i]; arr2[i] = arr2[j]; arr2[j] = tmp2; i++; j--; } }; return i; } static void quickSort(double arr[], double[]arr2, int left, int right) { int index = partition(arr, arr2, left, right); if (left < index - 1) quickSort(arr, arr2, left, index - 1); if (index < right) quickSort(arr, arr2, index, right); } //////////////////////////////////////////////////////////////////////// }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class CottageVillage { public static void main(String[] args){ Scanner scan = new Scanner(System.in); while(scan.hasNext()){ int counter = 0; int numbCottages = scan.nextInt(); int t = scan.nextInt(); House[] cottages = new House[numbCottages]; for(int i =0; i<numbCottages; i++){ int centre = scan.nextInt(); int length = scan.nextInt(); double beginning = centre - ((double)length)/2; double end = centre + ((double)length)/2; cottages[i]= new House(beginning, end); } Arrays.sort(cottages); //check righthand side of first cottage /* if(cottages[0].end + t < cottages[1].beginning) counter++; //check lefthand side of last cottage if(cottages[numbCottages-1].beginning -t > cottages[numbCottages-2].end) counter++; */ for(int i =0; i<numbCottages-1; i++){ if(cottages[i].end + t <= cottages[i+1].beginning){ counter++; // System.out.println(counter + "left hand"); } if (cottages[i+1].beginning - t >= cottages[i].end){ counter++; // System.out.println(counter + "right hand"); } if (Math.abs((cottages[i].end + t - cottages[i+1].beginning)) < 1e-8){ counter--; } } System.out.println(counter+2); } } static class House implements Comparable<House>{ double beginning; double end; House(double _beginning, double _end){ beginning = _beginning; end = _end; } @Override public int compareTo(House house) { // if(this.beginning<house.beginning){ // return -1; // } // else if(this.beginning==house.beginning){ // return 0; // } // else{ // return 1; // } return Double.valueOf(beginning).compareTo(house.beginning); } } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.awt.Point; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; /** * * @author epiZend */ public class Cottage { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int t = sc.nextInt(); List<Point> houses = new ArrayList<Point>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); int a = sc.nextInt(); houses.add(new Point(x, a)); } Collections.sort(houses, new Comparator<Point>() { @Override public int compare(Point o1, Point o2) { return ((Integer) o1.x).compareTo(o2.x); } }); int pos = 2; for (int i = 0; i < n - 1; i++) { double end = houses.get(i).x + (houses.get(i).y+0.0)/2; double start = houses.get(i+1).x - (houses.get(i+1).y+0.0)/2; //System.out.println("end "+end+" start "+start); double diff = start-end; //System.out.println("diff"); if (Math.abs(diff-t) < 0.0000001) { pos++; } if (diff > t) { pos += 2; } } System.out.println(pos); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Solution { BufferedReader in; PrintWriter out; StringTokenizer st; static class Pair implements Comparable<Pair> { int x, a; Pair(int x, int a) { this.x = x; this.a = a; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return 0; } } boolean isCross(double l1, double r1, double l2, double r2) { double r = min(r1, r2); double l = max(l1, l2); return r > l; } boolean check(double xl, double xr, double[] l, double[] r, int n) { boolean ok = false; for (int j = 0; j < n; ++j) ok |= isCross(xl, xr, l[j], r[j]); return ok; } void solve() throws IOException { int n = ni(); double t = ni(); double[] l = new double[n]; double[] r = new double[n]; for (int i = 0; i < l.length; i++) { double x = ni(); double len = ni(); l[i] = x - len / 2.0; r[i] = x + len / 2.0; } HashSet<Double> set = new HashSet<Double>(); for (int i = 0; i < n; ++i) { double xl = l[i] - t; double xr = l[i]; boolean ok = check(xl, xr, l, r, n); if (!ok) set.add(xl); xl = r[i]; xr = r[i] + t; ok = check(xl, xr, l, r, n); if (!ok) set.add(xl); } out.println(set.size()); } 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
15_A. Cottage Village
CODEFORCES
import java.util.*; public class a { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); double w = in.nextDouble(); int tot = 2; Interval[] houses = new Interval[n]; for(int i=0; i<n; i++) { double center = in.nextDouble(); double wid = in.nextDouble(); houses[i] = new Interval(center-wid/2,center+wid/2); } Arrays.sort(houses); for(int i=1; i<n; i++) { double dist = houses[i].s - houses[i-1].e; if(dist+1e-6 >= w) { tot+=2; if(Math.abs(w-dist) < 1e-6) tot--; } } System.out.println(tot); } } class Interval implements Comparable<Interval> { double s, e; Interval(double a, double b) { s=a; e=b; } public int compareTo(Interval i) { return (int)Math.signum(s-i.s); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.text.ChoiceFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class A { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int t = scan.nextInt(); List<Double> coords = new ArrayList<Double>(); while (n-- > 0) { double x = scan.nextDouble(); double a = scan.nextDouble() / 2; coords.add(x - a); coords.add(x + a); } Collections.sort(coords); int count = 2; ChoiceFormat f = new ChoiceFormat("-1#0|0#1|0<2"); for (int i = 1; i < coords.size()-2; i+=2) { count += new Integer(f.format(coords.get(i+1)-coords.get(i)-t)); } System.out.println(count); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class A { BufferedReader in; StringTokenizer st; PrintWriter out; static class House implements Comparable<House> { final int x, a; House(int x, int a) { this.x = x; this.a = a; } public int compareTo(House h) { return x - h.x; } } void solve() throws IOException { int n = nextInt(); int t = nextInt(); House[] h = new House[n]; for (int i = 0; i < n; ++i) { h[i] = new House(2 * nextInt(), nextInt()); } Arrays.sort(h); int ans = 2; for (int i = 1; i < n; ++i) { House prev = h[i - 1]; House curr = h[i]; int diff = 2 * t + curr.a + prev.a; if (curr.x - prev.x == diff) { ++ans; } else if (curr.x - prev.x > diff) { ans += 2; } } out.println(ans); } public void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); eat(""); solve(); out.close(); in.close(); } void eat(String s) { st = new StringTokenizer(s); } String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } eat(line); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) throws IOException { new A().run(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; public class R015A { final double EPS = 1e-9; boolean isEqual(double x, double y) { return Math.abs(x-y) <= EPS * Math.max(Math.abs(x), Math.abs(y)); } class Pair implements Comparable<Pair>{ double left; double right; Pair(double left, double right) { this.left = left; this.right = right; } public String toString() { return "(" + left + "," + right + ")"; } public int hashCode() { return (int)(left * 17 + right * 31); } public boolean equals(Object o) { if(!(o instanceof Pair)) return false; Pair that = (Pair)o; return isEqual(this.left, that.left) && isEqual(this.right, that.right); } public int compareTo(Pair that) { if(this.left != that.left) return (int)(this.left - that.left); return (int)(this.right - that.right); } } public R015A() { } private void process() { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int t = scanner.nextInt(); int[] x = new int[n]; int[] a = new int[n]; for(int i=0; i<n; i++) { x[i] = scanner.nextInt(); a[i] = scanner.nextInt(); } List<Pair> pairs = new ArrayList<Pair>(); for(int i=0; i<n; i++) { double left = x[i] - a[i] / 2.0; double right= x[i] + a[i] / 2.0; pairs.add(new Pair(left, right)); } Collections.sort(pairs); Set<Pair> newPairs = new HashSet<Pair>(); newPairs.add(new Pair(pairs.get(0).left - t, pairs.get(0).left)); for(int i=0; i<pairs.size()-1; i++) { if(pairs.get(i+1).left - pairs.get(i).right >= t) { newPairs.add(new Pair(pairs.get(i).right, pairs.get(i).right + t)); newPairs.add(new Pair(pairs.get(i+1).left - t, pairs.get(i+1).left)); } } newPairs.add(new Pair(pairs.get(pairs.size()-1).right, pairs.get(pairs.size()-1).right + t)); System.out.println(newPairs.size()); } public static void main(String[] args) { new R015A().process(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.awt.Point; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class ProblemA { static ArrayList<Point2> houses = new ArrayList<Point2>(); public static void main(String[] args) { ProblemA a = new ProblemA(); Scanner in = new Scanner(System.in); while(in.hasNextInt()){ int n = in.nextInt(); double t = in.nextDouble(); for (int k=0;k<n;k++){ houses.add(a.new Point2(in.nextDouble(),in.nextDouble())); } Collections.sort(houses); int ans = 2; for (int k=0;k<n-1;k++){ Point2 cur = houses.get(k); Point2 next = houses.get(k+1); double dist = (next.x - next.y/2) - (cur.x + cur.y/2); if (dist == t) ans ++; if (dist > t ) ans+=2; } System.out.println(ans); } } public class Point2 implements Comparable<Point2>{ public double x; public double y; public Point2(double one, double two){ x = one; y = two; } public int compareTo(Point2 other){ if (x - other.x > 0) return 1; if (x - other.x < 0) return -1; return 0; } public String toString(){ return "x:" + x + " y:" + y; } } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; public class House { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int t = sc.nextInt(); ArrayList<HS> list = new ArrayList<HS>(); for (int i = 0; i < n; i++) { list.add(new HS(sc.nextInt(),sc.nextInt())); } Collections.sort(list); int count = 0; if(n >= 1) count = 2; for(int i = 0; i < list.size() - 1; i++){ double d = Math.abs(list.get(i + 1).x - list.get(i).x); d -= ((1.0*list.get(i).a/2.0) + (1.0*list.get(i + 1).a/2.0)); if ((d >= t)&& ((d-t) <= 0.00000001)){ count++; } else if(d > t){ count+= 2; } } System.out.println(count); } } class HS implements Comparable<HS> { public int x; public int a; public HS(int x, int a) { this.x = x; this.a = a; } public int compareTo(HS o) { return x - o.x; } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; public class Main { Scanner in; PrintWriter out; static class House implements Comparable <House>{ int len; int pos; House(Scanner in){ pos = in.nextInt() * 2; len = in.nextInt() * 2; } public int compareTo(House arg0) { return this.pos-arg0.pos; } } void solve(){ int n = in.nextInt(); int size = in.nextInt(); House []h = new House[n]; for (int i = 0; i < h.length; i++){ h[i] = new House(in); } Arrays.sort(h); int ans = 2; for (int i = 0; i < h.length - 1; i++){ int next = i + 1; int sz = h[next].pos - h[i].pos - (h[next].len + h[i].len) / 2; if (sz == size * 2) { ans ++; } else if (sz > size * 2) { ans += 2; } } out.println(ans); } public void run(){ in = new Scanner(System.in); out = new PrintWriter(System.out); try { solve(); } finally { out.close(); } } void asserT(boolean e){ if (!e){ throw new Error(); } } public static void main(String[] args) { new Main().run(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class CottageVillage { public static void main(String... args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); TreeMap<Integer, Integer> tm = new TreeMap<Integer, Integer>(); while (n-->0) { tm.put(sc.nextInt(), sc.nextInt()); } int cnt=2, x=0, a=0; double diff=0; for(Map.Entry<Integer, Integer> e : tm.entrySet()) { if (x!=0 || a!=0) { diff = Math.abs(e.getKey()-x-e.getValue()*0.5-a*0.5); if (diff-k>0) cnt+=2; else if (diff-k==0) cnt++; } x=e.getKey(); a=e.getValue(); } System.out.println(cnt); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { StreamTokenizer in; PrintWriter out; static public void main(String[] args) throws IOException { new Main().run(); } int ni() throws IOException { in.nextToken(); return (int) in.nval; } void run() throws IOException { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(new OutputStreamWriter(System.out)); int n = ni(), t = ni(); if(n == 0) { out.println(0); out.flush(); return; } House[] h = new House[n]; for(int i = 0; i < n; i++) { h[i] = new House(); h[i].x = ni(); h[i].a = ni(); } Arrays.sort(h); int ret = 2; for(int i = 0; i < n - 1; i++) { if(2*(h[i + 1].x - h[i].x) - h[i].a - h[i + 1].a > 2*t) ret+=2; else if(2*(h[i + 1].x - h[i].x) - h[i].a - h[i + 1].a == 2*t) ret++; } out.println(ret); out.flush(); } void run1() throws IOException { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(new OutputStreamWriter(System.out)); int t = ni(); long n = ni(), m = ni(); long x1 = ni(), y1 = ni(), x2 = ni(), y2 = ni(); long tx1 = Math.min(x1, x2), tx2 = x1 + x2 - tx1; long ty1 = Math.min(y1, y2), ty2 = y1 + y2 - ty1; long dx = tx2 - tx1; long dy = ty2 - ty1; } class House implements Comparable<House> { int x, a; public int compareTo(House h) { return x < h.x ? -1 : 1; } } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class Solution { static class Cottage implements Comparable<Cottage> { public int x; public double a; public Cottage(int x, int a) { this.x = x; this.a = a; } public int compareTo(Cottage c) { return x - c.x; } } static final double e = 1e-9; public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int t = in.nextInt(); Cottage[] cottages = new Cottage[n]; for (int i = 0; i < n; i++) cottages[i] = new Cottage(in.nextInt(), in.nextInt()); Arrays.sort(cottages); int ans = 2; for (int i = 1; i < cottages.length; i++) { double diff = cottages[i].x - cottages[i - 1].x - cottages[i - 1].a / 2 - cottages[i].a / 2; ans = Math.abs(diff - t) < e ? ans + 1 : diff - t < -e ? ans : ans + 2; } out.print(ans); out.close(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; import java.io.*; /** * Created by HREN_VAM. */ public class A implements Runnable{ BufferedReader in; PrintWriter out; StringTokenizer st; public static final String filename = "a"; class I implements Comparable<I>{ int x; int a; I(int x, int a){ this.x = x; this.a = a; } public int compareTo(I o){ return Double.compare(x, o.x); } } public void solve() throws IOException{ int n = nextInt(); int t = nextInt(); I[] a = new I[n]; for(int i = 0;i < n;i ++){ a[i] = new I(nextInt(), nextInt()); } int res = 2; Arrays.sort(a); for(int i = 1;i < n;i ++){ if((a[i].x - a[i - 1].x - 1.0 * (a[i].a + a[i - 1].a) / 2) >= t)res ++; if((a[i].x - a[i - 1].x - 1.0 * (a[i].a + a[i - 1].a) / 2) > t)res ++; } out.println(res); } public void run(){ try{ Locale.setDefault(Locale.US); in = new BufferedReader(new InputStreamReader(System.in)); //in = new BufferedReader(new FileReader(filename + ".in")); out = new PrintWriter(System.out); //out = new PrintWriter(new FileWriter(filename + ".out")); st = new StringTokenizer(""); solve(); out.close(); } catch(IOException e){ throw new RuntimeException(e); } } public static void main(String[] args){ new Thread(new A()).start(); } public String nextToken() throws IOException{ while(!st.hasMoreTokens()){ st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws IOException{ return Integer.parseInt(nextToken()); } public double nextDouble() throws IOException{ return Double.parseDouble(nextToken()); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; public class Main implements Runnable { private BufferedReader in; private PrintWriter out; private StringTokenizer st; private void eat(String line) { st = new StringTokenizer(line); } private String next() throws IOException { while(!st.hasMoreTokens()) { String line = in.readLine(); if(line == null) return null; eat(line); } return st.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(next()); } public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); eat(""); go(); out.close(); } catch(Exception e) { e.printStackTrace(); System.exit(-1); } } public static void main(String[] args) { new Thread(new Main()).start(); } public void go() throws IOException { int n = nextInt(), t = nextInt(); int[] x = new int[n], a = new int[n]; for(int i = 0; i < n; ++i) { x[i] = nextInt(); a[i] = nextInt(); } out.println(new Algo().solve(n, t, x, a)); } } final class Algo { public int solve(int n, int t, final int[] x, final int[] a) { Integer[] order = new Integer[n]; for(int i = 0; i < n; ++i) order[i] = i; Arrays.sort(order, new Comparator<Integer>() { public int compare(Integer a, Integer b) { return x[a] - x[b]; } }); int result = 2; for(int i = 0; i + 1 < n; ++i) { int u = order[i], v = order[i + 1]; int dist = 2 * (x[v] - x[u]) - (a[v] + a[u]); if(dist > 2 * t) result += 2; else if(dist == 2 * t) ++result; } return result; } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; public class Village{ static class House implements Comparable<House>{ public double center, length; public House(double center, double length){ this.center = center; this.length = length; } public double getRight(){ return center + length/2; } public double getLeft(){ return center - length/2; } public int compareTo(House h){ return this.center < h.center ? -1 : this.center == h.center ? 0 : 1; } } public static void main(String[] args){ Scanner in = new Scanner(System.in); String[] fline = in.nextLine().split("\\s+"); int N = Integer.parseInt(fline[0]); int T = Integer.parseInt(fline[1]); House[] houses = new House[N]; for (int i = 0; i < N; i++){ String[] house = in.nextLine().split("\\s+"); houses[i] = new House(Double.parseDouble(house[0]), Double.parseDouble(house[1])); } Arrays.sort(houses); int count = 2; for (int i = 0; i < houses.length - 1; i++){ //how many positions between house i and house i + 1 can we fit in? double diff = houses[i+1].getLeft() - houses[i].getRight(); if (diff < T) continue; if (Math.abs(diff - T) < 1E-12) count++; else count+=2; } System.out.println(count); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; public class Main { public Main() { super(); } public static void main(String... args) { Main main = new Main(); main.start(); } public void start() { Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int n = in.nextInt(); int t = in.nextInt(); House list[] = new House[n]; for (int i = 0; i < n; i++) { int x = in.nextInt(); int a = in.nextInt(); list[i] = new House(x, a); } Arrays.sort(list); int c = 2; for (int i = 1; i < n; i++) { float d = list[i].left - list[i - 1].right; if (d == t) c++; else if (d > t) c += 2; } System.out.println(c); } } class House implements Comparable<House> { public int x; public float left, right; public House(int x, int a) { this.x = x; float h = a / 2f; this.left = x - h; this.right = x + h; } public int compareTo(House h) { return this.x == h.x ? 0 : this.x < h.x ? -1 : 1; } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; import java.awt.*; public class A { static Comparator<Point> cmp = new Comparator<Point>() { public int compare(Point a, Point b) { if(a.x < b.x) return -1; else if(a.x > b.x) return 1; return 0; } }; public static void main(String args[]) { Scanner scan = new Scanner(System.in); while(scan.hasNextInt()) { int n = scan.nextInt(); int k = scan.nextInt(); Point[] a = new Point[n]; for(int i=0;i < n;i++) { a[i] = new Point(); a[i].x = scan.nextInt(); a[i].y = scan.nextInt(); } Arrays.sort(a, cmp); int rtn = 0; ArrayList<Double> ans = new ArrayList<Double>(); for(int i=0;i < n;i++) { //Left double lb = a[i].x - (a[i].y / 2.0) - k; double pos = lb + (k/2.0); boolean good = true; for(int j=0;j < ans.size();j++) if(Math.abs(ans.get(j) - pos) < 0.0000001) good = false; if(good && (i == 0 || a[i-1].x + (a[i-1].y / 2.0) <= lb)) { rtn++; ans.add(pos); } double rb = a[i].x + (a[i].y / 2.0) + k; pos = rb - (k/2.0); good = true; for(int j=0;j < ans.size();j++) if(Math.abs(ans.get(j) - pos) < 0.0000001) good = false; if(good && (i == n-1 || a[i+1].x - (a[i+1].y / 2.0) >= rb)) { rtn++; ans.add(pos); } } System.out.println(rtn); } } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; public class Main implements Runnable { class Segment { int l, r; Segment(int l, int r) { this.l = l; this.r = r; } @Override public boolean equals(Object obj) { Segment o = (Segment)obj; return l == o.l && r == o.r; } @Override public int hashCode() { return 1000 * l + r; } } public void _main() throws IOException { int n = nextInt(); int t = nextInt(); int[] x = new int[n]; int[] a = new int[n]; for (int i = 0; i < n; i++) { x[i] = nextInt(); a[i] = nextInt(); } Set<Segment> set = new HashSet<Segment>(); for (int i = 0; i < n; i++) { int l = 2 * x[i] + a[i]; int r = 2 * x[i] + a[i] + 2 * t; boolean ok = true; for (int j = 0; j < n; j++) { if (i == j) continue; int L = Math.max(l, 2 * x[j] - a[j]); int R = Math.min(r, 2 * x[j] + a[j]); if (L < R) { ok = false; break; } } if (ok) set.add(new Segment(l, r)); l = 2 * x[i] - a[i] - 2 * t; r = 2 * x[i] - a[i]; ok = true; for (int j = 0; j < n; j++) { if (i == j) continue; int L = Math.max(l, 2 * x[j] - a[j]); int R = Math.min(r, 2 * x[j] + a[j]); if (L < R) { ok = false; break; } } if (ok) set.add(new Segment(l, r)); } out.print(set.size()); } 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
15_A. Cottage Village
CODEFORCES
import java.util.*; public class test { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int t=in.nextInt(); House[] houses=new House[n]; for(int i=0;i<n;i++) { houses[i]=new House(in.nextInt(),in.nextInt()); } Arrays.sort(houses); int count=2; for(int i=0;i<n-1;i++) { double start=houses[i].x+(double)houses[i].a/2; double end=houses[i+1].x-(double)houses[i+1].a/2; if(end-start==t) count++; if(end-start>t) count+=2; } System.out.println(count); } } class House implements Comparable<House> { int x; int a; public House(int _x, int _a) { x=_x; a=_a; } @Override public int compareTo(House o) { return x-o.x; } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; /** * @author Alexander Grigoryev * Created on 28.07.2011 */ public class Main { static Scanner in = new Scanner(System.in); public static void main(String[] args) { int n = in.nextInt(); int t = in.nextInt(); List<Integer> v = new ArrayList<Integer>(n); for(int i = 0; i < n; i++) { int a = in.nextInt(); int b = in.nextInt(); v.add(2 * a - b); v.add(2 * a + b); } Collections.sort(v); int ans = 2; for(int i = 2; i < v.size(); i += 2) { if(v.get(i) - v.get(i - 1) > 2 * t) ans += 2; if(v.get(i) - v.get(i - 1) == 2 * t) ans++; } System.out.println(ans); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.math.*; import java.util.*; public class Village { static Scanner in = new Scanner( new BufferedReader( new InputStreamReader( System.in ) ) ); public static void main( String[] args ) { int n = in.nextInt(), t = 2*in.nextInt(), h[][] = new int[n][2], ans = 2; for( int i = 0; i < n; i++ ) { h[i][0] = 2*in.nextInt(); h[i][1] = in.nextInt(); } Arrays.sort( h, new Comp() ); for( int i = 1; i < n; i++ ) { int d = (h[i][0]-h[i][1])-(h[i-1][0]+h[i-1][1]); if( d > t ) ans += 2; else if( d == t ) ans++; } System.out.println( ans ); } static class Comp implements Comparator<int[]> { public int compare( int[] a, int[] b ) { return a[0]-b[0]; } } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; /** * */ /** * @author burdakovd * */ public class A { static class House { int x, a; } /** * @param args */ public static void main(final String[] args) { final Scanner in = new Scanner(System.in); final PrintWriter out = new PrintWriter(System.out); try { final int n = in.nextInt(); final int t = in.nextInt(); final House[] h = new House[n]; for (int i = 0; i < h.length; ++i) { h[i] = new House(); h[i].x = in.nextInt(); h[i].a = in.nextInt(); } Arrays.sort(h, new Comparator<House>() { @Override public int compare(final House o1, final House o2) { return Integer.valueOf(o1.x).compareTo(o2.x); } }); int ans = 2; for (int i = 1; i < n; ++i) { final int dspace = 2 * h[i].x - h[i].a - (2 * h[i - 1].x + h[i - 1].a); if (dspace == 2 * t) { ++ans; } else if (dspace > 2 * t) { ans += 2; } } out.println(ans); } finally { in.close(); out.close(); } } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; import java.util.Comparator; 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(), t = nextInt() * 2; int[][] a = new int[n][2]; for (int i=0; i<n; i++) { a[i][0] = nextInt() * 2; a[i][1] = nextInt() * 2; } Arrays.sort(a, new Comparator<int[]>() { public int compare(int[] a, int[] b) { return a[0]>b[0]?1:a[1]<b[1]?-1:0; } }); int s = 2; for (int i=0; i<n-1; i++) { int g = (a[i+1][0]-a[i][0])-(a[i+1][1]+a[i][1])/2; if (g > t) s += 2; if (g == t) s+= 1; } out.println(s); out.flush(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Collections; public class A { public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] s = in.readLine().split(" "); int n = Integer.parseInt(s[0]); int t = Integer.parseInt(s[1]) * 2; int[] walls = new int[n*2]; for (int i=0; i<n; i++) { s = in.readLine().split(" "); int x = Integer.parseInt(s[0]) * 2; int a = Integer.parseInt(s[1]); walls[i*2] = x-a; walls[i*2+1] = x+a; } Arrays.sort(walls); int count = 2; for (int i=1; i<n*2-2; i+=2) { int space = walls[i+1] - walls[i]; if ( space == t) count += 1; else if ( space > t) count += 2; } System.out.println (count); } catch (NumberFormatException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; public class village { static int[] X, A; public void solve() { Scanner in = new Scanner(System.in); int N = in.nextInt(), T = in.nextInt(); X = new int[N]; A = new int[N]; for(int i = 0; i < N; i++) { X[i] = in.nextInt(); A[i] = in.nextInt(); } if(N == 1) { System.out.println("2"); return; } List<Integer> x = new ArrayList<Integer>(); for(int i = 0; i < N; i++) { x.add(i); } Collections.sort(x, new Comp()); int places = 0; for(int i = 0; i < N-1; i++) { double space = (X[x.get(i+1)]-X[x.get(i)]-A[x.get(i+1)]/2.0-A[x.get(i)]/2.0); if(space < T) { continue; } if(space - T < 1e-9) { places++; } else if(space > T) { places+=2; } } System.out.println(places+2); } public class Comp implements Comparator<Integer> { public int compare(Integer i1, Integer i2) { return X[i1]-X[i2]; } } public static void main(String[] args) { new village().solve(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import static java.util.Arrays.*; import static java.lang.Math.*; import java.util.*; import java.io.*; public class A implements Runnable { public static void main(String [] args) throws IOException { new Thread(null, new A(), "", 1 << 20).start(); } String file = "input"; BufferedReader input; PrintWriter out; public void run() { try { //input = new BufferedReader(new FileReader(file + ".in")); input = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedOutputStream(System.out)); solve(); input.close(); out.close(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } int N, T; void solve() throws IOException { StringTokenizer st = tokens(); N = nextInt(st); T = nextInt(st); T *= 2; ArrayList<Pair> list = new ArrayList<Pair>(); for(int i = 0; i < N; i++) { st = tokens(); int c = nextInt(st), L = nextInt(st); c *= 2; L *= 2; list.add(new Pair(c - L / 2, c + L / 2)); } Collections.sort(list); HashSet<Integer> set = new HashSet<Integer>(); for(int i = 0; i < list.size(); i++) { if(i == 0 || list.get(i).x - list.get(i - 1).y >= T) { set.add(list.get(i).x - T / 2); } if(i == list.size() - 1 || list.get(i + 1).x - list.get(i).y >= T) { set.add(list.get(i).y + T / 2); } } System.out.println(set.size()); } class Pair implements Comparable<Pair> { int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair p) { if(x != p.x) return x - p.x; return y - p.y; } } StringTokenizer tokens() throws IOException { return new StringTokenizer(input.readLine()); } String next(StringTokenizer st) { return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(input.readLine()); } int nextInt(StringTokenizer st) { return Integer.parseInt(st.nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(input.readLine()); } double nextDouble(StringTokenizer st) { return Double.parseDouble(st.nextToken()); } void print(Object... o) { out.println(deepToString(o)); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class A { static class Sort implements Comparable<Sort> { int x,a; public int compareTo(Sort o) { if (this.x==o.x) return this.a-o.a; return this.x-o.x; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int t = sc.nextInt(); Sort[]a = new Sort[n]; for (int i = 0; i < n; i++) { a[i] = new Sort(); a[i].x = sc.nextInt(); a[i].a = sc.nextInt(); } Arrays.sort(a); int ans = 2; for (int i = 1; i < n; i++) { double d = a[i].x-a[i].a / 2.0-a[i-1].x-a[i-1].a / 2.0; if (d==t) ans++; else if (d > t) ans += 2; } System.out.println(ans); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.security.KeyException; import java.util.HashMap; import java.util.Scanner; import java.util.TreeMap; public class P15A { public static void main (String [] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(), t = scan.nextInt(); TreeMap<Integer,Integer> hm = new TreeMap<Integer, Integer>(); for (int i = 0; i < n; i++) hm.put(scan.nextInt(),scan.nextInt()); int _x = 0, _a = 0, res = 2; boolean started = false; for (Integer key : hm.keySet()) { if (!started) { _x = key; _a = hm.get(_x); started = true; continue; } if (key - _x - ((Integer)hm.get(key) + _a)/2.0 > t) res +=2; else if (key - _x - ((Integer)hm.get(key) + _a)/2.0 == t) res++; _x = key; _a = hm.get(_x); } System.out.println(res); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.Arrays; import java.util.Scanner; public class A { static InputStreamReader in = new InputStreamReader(System.in); static BufferedReader bf = new BufferedReader(in); static StreamTokenizer st = new StreamTokenizer(bf); static Scanner sc = new Scanner(System.in); static class Sort implements Comparable<Sort> { int x, a; public int compareTo(Sort arg0) { if (this.x > arg0.x) return 1; return 0; } } public static void main(String[] args) throws IOException { int n = nextInt(); double t = nextInt(); Sort[] p = new Sort[n]; for (int i = 0; i < n; i++) { p[i] = new Sort(); p[i].x = nextInt(); p[i].a = nextInt(); } int ans = 2; Arrays.sort(p); for (int i = 1; i < p.length; i++) { double k = p[i].x - p[i].a / 2.0 - p[i - 1].x - p[i - 1].a / 2.0; if (t == k) ans++; else if (k > t) ans += 2; } System.out.println(ans); } private static String nextString() throws IOException { st.nextToken(); return st.sval; } private static int nextInt() throws IOException { st.nextToken(); return (int) st.nval; } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; public class CottageVillage { public Scanner in = new Scanner(System.in); public PrintStream out = System.out; public int n, t; public Pair[] v; public void main() { n = in.nextInt(); t = in.nextInt(); int i; v = new Pair[n]; for(i=0;i<n;++i) v[i] = new Pair(in.nextInt() * 2, in.nextInt()); Arrays.sort(v); int res = 2; for(i=0;i+1<n;++i) { if(v[i].x + v[i].y + 2*t == v[i+1].x - v[i+1].y) ++res; else if(v[i].x+v[i].y+2*t < v[i+1].x-v[i+1].y) res +=2; } out.println(res); }//end public void main() //int pair private class Pair implements Comparable<Pair> { public int x, y; public Pair(int xx, int yy) { x = xx; y = yy; } public int compareTo(Pair u) { if(x!=u.x) return x-u.x; return y-u.y; } public String toString() { return "(" + x + "," + y + ")"; } } public static void main(String[] args) { (new CottageVillage()).main(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.awt.Point; import java.awt.geom.Point2D; import java.util.Arrays; import java.util.Scanner; public class A { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int t=in.nextInt(); pt[] P=new pt[n]; for (int i=0; i<n; ++i) P[i]=new pt(in.nextInt(), in.nextInt()); Arrays.sort(P); int res=2; for (int i=0; i+1<n; ++i) { double d=P[i+1].x-P[i].x-P[i+1].a/2.-P[i].a/2.; if (Math.abs(d-t) <= 1e-11) ++res; else if (d>t) res+=2; } System.out.println(res); } } class pt implements Comparable<pt> { int x,a; pt(int x, int a) { this.x=x; this.a=a; } public int compareTo(pt o) { return x-o.x; } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; import java.io.*; public class a { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int t = sc.nextInt(); int[][] xa = new int[n][2]; for(int i=0; i<n; ++i) { xa[i][0] = sc.nextInt(); xa[i][1] = sc.nextInt(); } Arrays.sort(xa, new Comparator<int[]>(){ @Override public int compare(int[] a0, int[] a1){ return a0[0]-a1[0]; } }); int ans=2; for(int i=0; i<n-1; i++){ int s=(xa[i+1][0]*2-xa[i+1][1])-(xa[i][0]*2+xa[i][1]); if(s>t*2){ ans+=2; }else if(s==t*2){ ans++; } } System.out.println(ans+""); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import static java.lang.System.out; public class Flatville { public static void main( String args[] ) { class SquareHouse implements Comparable<SquareHouse> { public SquareHouse( double posLeft, double sideLen ) { _posLeft = posLeft; _sideLen = sideLen; } public double posLeft() { return _posLeft; } public double posRight() { return _posLeft + _sideLen; } public int compareTo( SquareHouse house ) { double dist = _posLeft - house.posLeft(); if ( dist < 0 ) return -1; else if ( dist > 0 ) return 1; else return 0; } private double _posLeft; private double _sideLen; } Scanner scanner = new Scanner( System.in ); // Read the header final int nHouses = scanner.nextInt(); final double sideLen = scanner.nextDouble(); ArrayList<SquareHouse> houses = new ArrayList<SquareHouse>(); // Read the houses for ( int iHouse = 0; iHouse < nHouses; ++iHouse ) { double pos = scanner.nextDouble(); double size = scanner.nextDouble(); double posLeft = pos - size / 2.0; houses.add( new SquareHouse( posLeft, size ) ); } // Sort the houses Collections.sort( houses ); int nPositions = 2; for ( int iHouse = 0; iHouse < nHouses - 1; ++iHouse ) { double space = houses.get( iHouse + 1 ).posLeft() - houses.get( iHouse ).posRight(); if ( sideLen < space ) nPositions += 2; else if ( sideLen == space ) nPositions++; } out.println( nPositions ); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.Comparator; 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 TaskA()).start(); // new Template().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 t = in.readInt(); final int[] x = new int[n]; int[] a = new int[n]; for (int i = 0; i < n; i++) { x[i] = in.readInt(); a[i] = in.readInt(); } Integer[] o = new Integer[n]; for (int i = 0; i < n; i++) o[i] = i; Arrays.sort(o, new Comparator<Integer>() { public int compare(Integer o1, Integer o2) { return x[o1] - x[o2]; } }); int ans = 2; for (int i = 1; i < n; i++) { int d = x[o[i]] - x[o[i - 1]]; d = 2 * d - a[o[i]] - a[o[i - 1]] - 2 * t; if (d > 0) ans += 2; else if (d == 0) ans++; } 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
15_A. Cottage Village
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; /** * Date 22.11.2011 * Time 17:49:45 * Author Woodey * $ */ public class A15 { final double eps = 10e-9; class Pair implements Comparable<Pair>{ int x; int length; Pair(int x, int length) { this.x = x; this.length = length; } public int compareTo(Pair p) { return x - p.x; } } private void Solution() throws IOException { int n = nextInt(), t = nextInt(), ans = 2; Pair[] pairs = new Pair[n]; for (int i = 0; i < n; i ++) { int x = nextInt(), length = nextInt(); pairs[i] = new Pair(x, length); } Arrays.sort(pairs); for (int i = 0; i < n-1; i ++) { double place = pairs[i+1].x - pairs[i].x - (double) pairs[i+1].length/2 - (double) pairs[i].length/2; if (place > t) ans += 2; else if ((int) (place+eps) == t) ans ++; } System.out.println(ans); } public static void main(String[] args) { new A15().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
15_A. Cottage Village
CODEFORCES
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Prob015A { public static void main( String[] Args ) { Scanner scan = new Scanner( System.in ); int numHouses = scan.nextInt(); int side = scan.nextInt() * 2; ArrayList<Integer> sides = new ArrayList<Integer>(); for ( int x = 0; x < numHouses; x++ ) { int c = scan.nextInt() * 2; int s = scan.nextInt(); int l = c - s; int r = c + s; int li = Collections.binarySearch( sides, l ); int ri = Collections.binarySearch( sides, r ); if ( li >= 0 && ri >= 0 ) { sides.remove( li ); sides.remove( li ); } else if ( li >= 0 ) sides.set( li, r ); else if ( ri >= 0 ) sides.set( ri, l ); else { sides.add( -li - 1, r ); sides.add( -li - 1, l ); } } int possibilities = 2; for ( int x = 1; x < sides.size() - 1; x += 2 ) if ( sides.get( x + 1 ) - sides.get( x ) > side ) possibilities += 2; else if ( sides.get( x + 1 ) - sides.get( x ) == side ) possibilities += 1; System.out.println( possibilities ); } }
nlogn
15_A. Cottage Village
CODEFORCES
//package round15; import java.io.BufferedOutputStream; import java.io.PrintStream; 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; public void solve() { int n = ni(); int t = ni(); Pair[] p = new Pair[n]; for(int i = 0;i < n;i++){ p[i] = new Pair(); p[i].x = ni(); p[i].a = ni(); } Arrays.sort(p); int ct = 2; for(int i = 0;i < n - 1;i++){ float d = p[i + 1].x - (float)p[i + 1].a / 2 - p[i].x - (float)p[i].a / 2; if(Math.abs(d - t) < EPS){ ct++; }else if(d > t){ ct += 2; } } out.println(ct); } double EPS = 0.0001; private static class Pair implements Comparable<Pair> { public int x; public int a; @Override public int compareTo(Pair o) { return x - o.x; } } public void run() throws Exception { // in = new Scanner(new StringReader("2 3 0 4 5 2")); in = new Scanner(System.in); System.setOut(new PrintStream(new BufferedOutputStream(System.out))); out = new PrintWriter(System.out); // int n = in.nextInt(); int n = 1; for(int i = 1;i <= n;i++){ long t = System.currentTimeMillis(); solve(); out.flush(); // System.err.printf("%04d/%04d %7d%n", i, n, System.currentTimeMillis() - t); } } 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
15_A. Cottage Village
CODEFORCES
//package round15; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; public class A { static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static int nextInt() throws IOException{ in.nextToken(); return (int)in.nval; } static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException{ int n = nextInt(), t = nextInt(), x[] = new int[n], a[] = new int[n]; for (int i=0; i<n; i++){ x[i] = nextInt(); a[i] = nextInt(); } for (int i=0; i<n-1; i++) for (int j=i+1; j<n; j++) if (x[i] > x[j]){ int p = x[i]; x[i] = x[j]; x[j] = p; p = a[i]; a[i] = a[j]; a[j] = p; } double l[] = new double[n]; double r[] = new double[n]; for (int i=0; i<n; i++){ l[i] = x[i]-a[i]/2.0; r[i] = x[i]+a[i]/2.0; } int res = 2; for (int i=1; i<n; i++){ if (Math.abs(l[i]-r[i-1]-t) < 0.000001) res++; else if (l[i]-r[i-1] > t) res += 2; } out.println(res); out.flush(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; public class Solution { private BufferedReader in; private PrintWriter out; private StringTokenizer st; static class House implements Comparable<House> { int x, a; @Override public int compareTo(House o) { return x - o.x; } public House(int x, int a) { this.x = x; this.a = a; } } void solve() throws IOException { int n = nextInt(); int t = nextInt(); House[] hs = new House[n]; for (int i = 0; i < n; ++i) { hs[i] = new House(nextInt(), nextInt()); } Arrays.sort(hs); int ans = 2; for (int i = 0; i < n - 1; ++i) { if (hs[i].a + hs[i + 1].a + 2 * t < 2 * (hs[i + 1].x - hs[i].x)) { ans += 2; } else if (hs[i].a + hs[i + 1].a + 2 * t == 2 * (hs[i + 1].x - hs[i].x)) { ans++; } } out.println(ans); } Solution() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); eat(""); solve(); in.close(); out.close(); } private void eat(String str) { st = new StringTokenizer(str); } String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } eat(line); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) throws IOException { new Solution(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class Main { static double eps = 1e-8; public static void main(String[] args) { Scanner r = new Scanner(System.in); int n = r.nextInt(); int t = r.nextInt(); House[] a = new House[n]; for(int i = 0; i < n; i++){ double c = r.nextInt(); double l = r.nextInt(); a[i] = new House(c-l/2, l); } Arrays.sort(a); int res = 0; for(int i = 0; i < n-1; i++){ double dist = a[i+1].s - (a[i].s+a[i].l); if(Math.abs(dist - t) < eps)res++; else if(dist > t)res += 2; } System.out.println(res+2); } } class House implements Comparable<House>{ double s, l; public House(double si, double li){ s = si; l = li; } @Override public int compareTo(House b) { if(s < b.s)return -1; else return 1; } }
nlogn
15_A. Cottage Village
CODEFORCES
//~ 22:04:48 import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); String out = ""; String[] p = br.readLine().split("[ ]"); int n = Integer.valueOf(p[0]); double t = Double.valueOf(p[1]); int offset = 5000; boolean[] flags = new boolean[offset+5000]; for(int i=0;i<n;i++){ int[] q = toIntArray(br.readLine()); int c = 2*q[0]; for(int j=-q[1];j<q[1];j++){ flags[c+offset+j] = true; //~ System.out.println(c+offset+j); } } int buf = 0; int last = -1; int index = 0; for(;index<flags.length;index++){ if(flags[index]){ if(last==-1){ buf++; }else{ //~ System.out.println(last); //~ System.out.println(index); if(Math.abs(index-last-(2*t+1))<1e-10) buf++; else if(index-last>2*t+1) buf+=2; } last = index; } } buf ++; out = ""+buf+"\r\n"; bw.write(out,0,out.length()); br.close(); bw.close(); } static int[] toIntArray(String line){ String[] p = line.trim().split("\\s+"); int[] out = new int[p.length]; for(int i=0;i<out.length;i++) out[i] = Integer.valueOf(p[i]); return out; } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; public class CottageVillage { Scanner in; PrintWriter out; CottageVillage() { in = new Scanner(System.in); out = new PrintWriter(System.out); } public void finalize() { out.flush(); in.close(); out.close(); } int ans(House a, House b, int t) { int diff = b.cordl - a.cordr; if(diff < t) return 0; if(diff == t) return 1; return 2; } void solve() { int n = in.nextInt(), t = in.nextInt() * 2; House[] hs = new House[n]; for(int i = 0; i < n; ++i) { int c = in.nextInt(), l = in.nextInt(); hs[i] = new House(2 * c - l, 2 * c + l); } Arrays.sort(hs); //atleast 2 possible configs int co = 2; for(int i = 0; i < n - 1; ++i) co += ans(hs[i], hs[i + 1], t); out.println(co); } public static void main(String[] args) throws FileNotFoundException { CottageVillage t = new CottageVillage(); t.solve(); t.finalize(); } } class House implements Comparable<House> { public int cordl, cordr; public House(int c, int l) { cordl = c; cordr = l; } @Override public int compareTo(House h) { return cordl - h.cordl; } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; public class Main implements Runnable { class House implements Comparable<House> { int x; int a; public House(int x, int a) { this.x = x; this.a = a; } @Override public int compareTo(House other) { return x - other.x; } } // private void solution() throws IOException { // int t = in.nextInt(); // while (t-- > 0) { // int n =in.nextInt(); // int m = in.nextInt(); // int x1 = in.nextInt(); // int y1 = in.nextInt(); // int x2 = in.nextInt(); // int y2 = in.nextInt(); // // } // } private void solution() throws IOException { int n = in.nextInt(); int t = in.nextInt(); House[] h = new House[n]; for (int i = 0; i < h.length; ++i) { h[i] = new House(in.nextInt(), in.nextInt()); } Arrays.sort(h); int res = 2; for (int i = 0; i < h.length - 1; ++i) { double dist = h[i + 1].x - h[i + 1].a / 2.0 - (h[i].x + h[i].a / 2.0); if (dist >= t) { if (dist == t) { ++res; } else { res += 2; } } } out.println(res); } public void run() { try { solution(); in.reader.close(); out.close(); } catch (Exception 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
15_A. Cottage Village
CODEFORCES
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; public class a { private void solve() throws Exception { int n = nextInt(), t = nextInt(); int[] x = new int[n], a = new int[n]; for (int i = 0; i < n; ++i){ x[i] = nextInt(); a[i] = nextInt(); } for (int i = 0; i < n; ++i) for (int j = 0; j < n - 1; ++j){ if (x[j] > x[j + 1]){ int tmp = x[j]; x[j] = x[j + 1]; x[j + 1] = tmp; tmp = a[j]; a[j] = a[j + 1]; a[j + 1] = tmp; } } int res = 2; for (int i = 1; i < n; ++i){ int betw = (x[i] - x[i - 1]) * 2 - a[i] - a[i - 1]; if (betw == t * 2) ++res; else if (betw > t * 2) res += 2; } out.print(res); } public void run() { try { solve(); } catch (Exception e) { NOO(e); } finally { out.close(); } } PrintWriter out; BufferedReader in; StringTokenizer St; void NOO(Exception e) { e.printStackTrace(); System.exit(1); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } String nextToken() { while (!St.hasMoreTokens()) { try { String line = in.readLine(); St = new StringTokenizer(line); } catch (Exception e) { NOO(e); } } return St.nextToken(); } private a(String name) { try { in = new BufferedReader(new FileReader(name + ".in")); St = new StringTokenizer(""); out = new PrintWriter(new FileWriter(name + ".out")); } catch (Exception e) { NOO(e); } } private a() { try { in = new BufferedReader(new InputStreamReader(System.in)); St = new StringTokenizer(""); out = new PrintWriter(System.out); } catch (Exception e) { NOO(e); } } public static void main(String[] args) { new a().run(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Main { BufferedReader in; PrintWriter out; StringTokenizer st; void solve() throws IOException { int n=ni();//have bult yet int t=ni();//new house int[] center=new int[n]; int[] width=new int[n]; for(int i=0;i<n;i++){ center[i]=ni(); width[i]=ni(); } for(int i=0;i<n;i++){ for(int j=i;j<n;j++){ if(center[i]>center[j]){ int cent=center[i]; int wid=width[i]; center[i]=center[j]; width[i]=width[j]; center[j]=cent; width[j]=wid; } } } int count=2; for(int i=0;i<n-1;i++){ //min way double ideal=(double)width[i]/2+(double)width[i+1]/2+t; //real way double real=center[i+1]-center[i]; //out.println(ideal); //out.println(real); if(ideal==real)count++; else{ if(ideal<real)count=count+2; } } out.println(count); } public Main() 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 Main(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.awt.Point; import java.io.*; import java.util.*; import static java.lang.Math.*; public class ProblemA_15 { 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()); } public static void main(String[] args){ new ProblemA_15().run(); } 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 t=readInt(); Point[] a=new Point[n]; for (int i=0; i<n; i++){ a[i]=new Point(readInt(), readInt()); } int count=2; Arrays.sort(a, new Comparator<Point>(){ @Override public int compare(Point p1, Point p2) { return p1.x-p2.x; } }); for (int i=1; i<n; i++){ double li=a[i-1].x+(double)a[i-1].y/2; double ri=a[i].x-(double)a[i].y/2; if (ri-li>t){ count+=2; } if (ri-li==t){ count++; } } out.print(count); } }
nlogn
15_A. Cottage Village
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 IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer split = new StringTokenizer(in.readLine()); int n = Integer.parseInt(split.nextToken()); double t = Double.parseDouble(split.nextToken()); House[] xaxis = new House[n]; for (int i = 0; i < n; i++) { split = new StringTokenizer(in.readLine()); xaxis[i] = new House(Double.parseDouble(split.nextToken()), Double.parseDouble(split.nextToken())); } Arrays.sort(xaxis); int count = 2; for (int i = 0; i < xaxis.length - 1; i++) { double free = (xaxis[i + 1].getX() - xaxis[i + 1].getSize() / 2.0) - (xaxis[i].getX() + xaxis[i].getSize() / 2.0); if (free / t == 1.0) { count++; } else if (free / t > 1.0) { count += 2; } } System.out.println(count); } } class House implements Comparable<House> { private double x; private double size; public House(double x, double size) { this.x = x; this.size = size; } public double getX() { return x; } public double getSize() { return size; } public int compareTo(House o) { if (this.x > o.getX()) { return 1; } else if (this.x == o.getX()) { return 0; } return -1; } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class Main { private static Node[] node; public static void main(String[] args) { Scanner cin = new Scanner(System.in); int ret = 2, del; int n = cin.nextInt(); int t = cin.nextInt() * 2; node = new Node[n]; for (int i = 0; i < n; i++) { int x = cin.nextInt(); int a = cin.nextInt(); node[i] = new Node(x * 2 - a, x * 2 + a); } Arrays.sort(node); for (int i = 1; i < n; i++) { del = node[i].l - node[i - 1].r; if (del > t) { ret += 2; } else if (del == t) { ret++; } } System.out.println(ret); } private static class Node implements Comparable<Node> { public int l; public int r; public Node(int l, int r) { // TODO Auto-generated constructor stub this.l = l; this.r = r; } @Override public int compareTo(Node arg0) { // TODO Auto-generated method stub return l - arg0.l; } } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) throws IOException { Scanner sc = new Scanner(System.in/*new File("input.txt")*/); int n = sc.nextInt(), t = sc.nextInt(), x, a, kol = 2; ArrayList<Double> al = new ArrayList<Double>(); for(int i=0;i<n;i++) { x = sc.nextInt(); a = sc.nextInt(); al.add(x - a/2.); al.add(x + a/2.); } Collections.sort(al); double d0 = 0; int k = 0, kn = al.size(); for(Double d: al) { if (k == 2) { if (d-d0>t) kol+=2; else if (d-d0==t) kol++; d0 = d; k = 1; } else { k++; d0 = d; } } System.out.print(kol); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; import static java.lang.Math.*; public class BetaRound15_A implements Runnable { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws IOException { 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()); } @Override 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); } } public static void main(String[] args) { new Thread(new BetaRound15_A()).start(); } class House implements Comparable<House> { int left, right; House(int left, int right) { this.left = left; this.right = right; } @Override public int compareTo(House h) { return this.left - h.left; } } int getAns(House h1, House h2, int t) { int d = h2.left - h1.right; if (d < t) return 0; if (d == t) return 1; return 2; } void solve() throws IOException { int n = readInt(); int t = readInt() * 2; House[] h = new House[n]; for (int i = 0; i < n; i++) { int c = readInt() * 2; int b = readInt(); h[i] = new House(c - b, c + b); } Arrays.sort(h); int ans = 2; for (int i = 1; i < n; i++) { ans += getAns(h[i - 1], h[i], t); } out.print(ans); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; import java.awt.*; public class A { static Comparator<Point> cmp = new Comparator<Point>() { public int compare(Point a, Point b) { if(a.x < b.x) return -1; else if(a.x > b.x) return 1; return 0; } }; public static void main(String args[]) { Scanner scan = new Scanner(System.in); while(scan.hasNextInt()) { int n = scan.nextInt(); int k = scan.nextInt(); Point[] a = new Point[n]; for(int i=0;i < n;i++) { a[i] = new Point(); a[i].x = scan.nextInt(); a[i].y = scan.nextInt(); } Arrays.sort(a, cmp); int rtn = 0; ArrayList<Double> ans = new ArrayList<Double>(); for(int i=0;i < n;i++) { //Left double lb = a[i].x - (a[i].y / 2.0) - k; double pos = lb + (k/2.0); boolean good = true; for(int j=0;j < ans.size();j++) if(Math.abs(ans.get(j) - pos) < 0.0000001) good = false; if(good && (i == 0 || a[i-1].x + (a[i-1].y / 2.0) <= lb)) { rtn++; ans.add(pos); } double rb = a[i].x + (a[i].y / 2.0) + k; pos = rb - (k/2.0); good = true; for(int j=0;j < ans.size();j++) if(Math.abs(ans.get(j) - pos) < 0.0000001) good = false; if(good && (i == n-1 || a[i+1].x - (a[i+1].y / 2.0) >= rb)) { rtn++; ans.add(pos); } } System.out.println(rtn); } } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.text.*; import java.util.*; public class A { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int t = scan.nextInt(); List<Double> coords = new ArrayList<Double>(); while (n-- > 0) { double x = scan.nextDouble(); double a = scan.nextDouble() / 2; coords.add(x - a); coords.add(x + a); } Collections.sort(coords); int count = 2; ChoiceFormat f = new ChoiceFormat("-1#0|0#1|0<2"); for (int i = 1; i < coords.size()-2; i+=2) { count += new Integer(f.format(coords.get(i+1)-coords.get(i)-t)); } System.out.println(count); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.awt.Point; import java.awt.geom.Point2D; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class A { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int t = in.nextInt() * 2; Point[] A = new Point[n]; for (int i = 0; i < n; i++) { int center = in.nextInt() * 2; int side = in.nextInt(); A[i] = new Point(center - side, center + side); } Arrays.sort(A, new Comparator<Point>() { public int compare(Point x, Point y) { return x.x - y.x; } }); int ans = 2; for (int i = 1; i < n; i++) { if (A[i].x - A[i - 1].y > t) ans += 2; else if (A[i].x - A[i - 1].y == t) ans++; } System.out.println(ans); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; public class main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int houses = sc.nextInt(); int size = sc.nextInt(); hizzy[] array = new hizzy[houses]; long total =2; for(int a=0;a<houses;a++)array[a]=new hizzy(sc.nextInt(),sc.nextInt()); Arrays.sort(array); for(int a=0;a<houses-1;a++){ double L = array[a].loc+array[a].size/2; double R = array[a+1].loc-array[a+1].size/2; if(R-L>size)total+=2; else if((R-L)==size)total++; } System.out.println(total); } } class hizzy implements Comparable{ double loc; double size; hizzy(double l, double s){ this.loc=l; this.size=s; } public int compareTo(Object o) { hizzy other = (hizzy) o; return (int) (this.loc-other.loc); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; public class Village { private class House implements Comparable<House> { Double Coordinate; Double Sidelength; private House(double coordinate, double sidelength) { Coordinate = coordinate; Sidelength = sidelength; } public int compareTo(House o) { return Coordinate.compareTo(o.Coordinate); } } private void solve() { Scanner in = new Scanner(System.in); in.next(); double requireside = in.nextDouble(); ArrayList<House> coordinate = new ArrayList<House>(); while (in.hasNext()) { double coo = in.nextDouble(); double side = in.nextDouble(); coordinate.add(new House(coo, side)); } Collections.sort(coordinate); ArrayList<Double> edges = new ArrayList<Double>(); int count = 2; for (int i = 0; i < coordinate.size(); i++) { edges.add(coordinate.get(i).Coordinate - (double) coordinate.get(i).Sidelength / (double) 2); edges.add(coordinate.get(i).Coordinate + (double) coordinate.get(i).Sidelength / (double) 2); } ArrayList<Double> difference = new ArrayList<Double>(); for (int i = 1; i < edges.size() - 1; i++) { difference.add(Math.abs(edges.get(i) - edges.get(i + 1))); } for (int i = 0; i < difference.size(); i += 2) { if (difference.get(i) == requireside) count++; else if (difference.get(i) > requireside) count += 2; } System.out.println(count); } public static void main(String args[]) { Village v = new Village(); v.solve(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; public class A { public static void main(String[] args) { new A(new Scanner(System.in)); } public A(Scanner in) { int n = in.nextInt(); int t = in.nextInt(); int tt = 2*t; rect[] rs = new rect[n]; for (int i=0; i<n; i++) rs[i] = new rect(in.nextInt(), in.nextInt()); Arrays.sort(rs); int res = 2; for (int i=1; i<n; i++) { rect a = rs[i-1]; rect b = rs[i]; int d = b.p-a.p; int dd = a.t+b.t; int tv = 2*d-dd; if (tt == tv) res++; if (tv > tt) res+=2; } System.out.printf("%d%n", res); } } class rect implements Comparable<rect> { int p; int t; public rect(int pp, int tt) { p = pp; t = tt; } public int compareTo(rect rhs) { return p-rhs.p; } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; import static java.lang.Math.*; public class Main implements Runnable { class Home implements Comparable<Home> { @Override public int compareTo(Home arg0) { return st - arg0.st; } int st, end; } public void solve() throws IOException { int n = nextInt(), t = nextInt() * 2; Home[] h = new Home[n]; for(int i = 0; i < n; ++i) { int x = nextInt() * 2, a = nextInt() * 2; h[i] = new Home(); h[i].st = x - a / 2; h[i].end = x + a / 2; } Arrays.sort(h); int ans = 2; for(int i = 0; i + 1 < n; ++i) { int delta = h[i + 1].st - h[i].end; if (delta == t) ans++; if (delta > t) ans += 2; } pw.println(ans); } static final String filename = "A"; static final boolean fromConsole = true; public void run() { try { if (!fromConsole) { in = new BufferedReader(new FileReader(filename + ".in")); pw = new PrintWriter(filename + ".out"); } else { in = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); } st = new StringTokenizer(""); long st = System.currentTimeMillis(); solve(); //pw.printf("\nWorking time: %d ms\n", System.currentTimeMillis() - st); pw.close(); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } private StringTokenizer st; private BufferedReader in; private PrintWriter pw; boolean hasNext() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return false; } st = new StringTokenizer(line); } return st.hasMoreTokens(); } String next() throws IOException { return hasNext() ? st.nextToken() : null; } int nextInt() throws IOException { return Integer.parseInt(next()); } BigInteger nextBigInteger() throws IOException { return new BigInteger(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) { new Thread(new Main()).start(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.util.*; public class A { Scanner in = new Scanner(System.in); public class Houses implements Comparable<Houses>{ double x; double a; public Houses(double xVal, double aVal){ x = xVal-aVal/2; a = aVal; } @Override public int compareTo(Houses o) { return (int) (x - o.x); } } public void solve2(ArrayList<Houses> list,int t){ Collections.sort(list); int count = 2; //beginning and end for(int f = 0; f < list.size()-1; f++){ if(list.get(f+1).x-list.get(f).x-list.get(f).a > t){ count+=2; } else if(list.get(f+1).x-list.get(f).x-list.get(f).a == t){ count++; } } System.out.println(count); } public void solve(){ ArrayList<Houses> list = new ArrayList<Houses>(); int n = in.nextInt(); int t = in.nextInt(); for(int i = 0; i < n; i++){ list.add(new Houses(in.nextDouble(),in.nextDouble())); } solve2(list,t); } public static void main(String[] args){ A p = new A(); p.solve(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.math.*; import java.util.*; public class Village { static Scanner in = new Scanner( new BufferedReader( new InputStreamReader( System.in ) ) ); public static void main( String[] args ) { int n = in.nextInt(), t = 2*in.nextInt(), h[][] = new int[n][2], ans = 2; for( int i = 0; i < n; i++ ) { h[i][0] = 2*in.nextInt(); h[i][1] = in.nextInt(); } Arrays.sort( h, new Comp() ); for( int i = 1; i < n; i++ ) { int d = (h[i][0]-h[i][1])-(h[i-1][0]+h[i-1][1]); if( d>t ) ans += 2; if( d==t ) ans++; } System.out.println( ans ); } static class Comp implements Comparator<int[]> { public int compare( int[] a, int[] b ) { return a[0]-b[0]; } } }
nlogn
15_A. Cottage Village
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(); } class House { int x, t; public House(int x, int t) { this.x = x; this.t = t; } } void solve() { int n = nextInt(); int t = nextInt(); House[] h = new House[n]; for (int i = 0; i < n; i++) { h[i] = new House(nextInt(), nextInt()); } Arrays.sort(h, new Comparator<House>() { @Override public int compare(House o1, House o2) { return o1.x < o2.x ? -1 : o1.x > o2.x ? 1 : 0; } }); int ans = 0; for (int i = 0; i < n; i++) { if (i == 0 || (h[i].x - h[i - 1].x) * 2 - h[i].t - h[i - 1].t >= 2 * t) { ++ans; } if (i == n - 1 || (h[i + 1].x - h[i].x) * 2 - h[i + 1].t - h[i].t > 2 * t) { ++ans; } } out.println(ans); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.text.ChoiceFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class A { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int t = scan.nextInt(); List<List<Double>> coords = new ArrayList<List<Double>>(); while (n-- > 0) { double x = scan.nextDouble(); double a = scan.nextDouble() / 2; coords.add(Arrays.asList(x - a, x + a)); } Collections.sort(coords, new Comparator<List<Double>>() { @Override public int compare(List<Double> o1, List<Double> o2) { return o1.get(0).compareTo(o2.get(0)); } }); int count = 2; ChoiceFormat f = new ChoiceFormat("-1#0|0#1|0<2"); for (int i = 0; i < coords.size()-1; i++) { double l = coords.get(i+1).get(0)-coords.get(i).get(1)-t; count += new Integer(f.format(l)); } System.out.println(count); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.lang.*; import java.io.*; import java.util.*; import java.math.*; public class Solution implements Runnable{ 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 Solution())).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()); } public void run() { int n = nextInt(); int t = nextInt(); double[] d = new double[2*n]; for (int i = 0; i < n; ++i) { double x = nextDouble(); double a = nextDouble(); d[2*i] = x-a/2; d[2*i+1] = x+a/2; } Arrays.sort(d); int res = 2; for (int i = 1; i < 2*n-1; i+=2) { if (d[i+1] - d[i] >= t) { ++res; } if (d[i+1] - d[i] > t) { ++res; } } out.println(res); out.flush(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class CodeForces { public void solve() throws IOException { int n=nextInt(); int t=nextInt(); double larr[]=new double [n]; double rarr[]=new double [n]; for(int i=0;i<n;i++){ double x=nextDouble(); double r=nextDouble(); larr[i]=x-r/2; rarr[i]=x+r/2; } Arrays.sort(larr); Arrays.sort(rarr); int counter=2; for(int i=1;i<n;i++){ if(larr[i]-rarr[i-1]>t){ counter+=2; } else if(larr[i]-rarr[i-1]==t){ counter++; } } writer.print(counter); } public static void main(String[] args) { new CodeForces().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); //reader = new BufferedReader(new FileReader("input.txt")); tokenizer = null; writer = new PrintWriter(System.out); //writer = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); // long t=new Date().getTime(); solve(); // writer.println(t-new Date().getTime()); 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(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Locale; import java.util.StringTokenizer; public class Solution implements Runnable { public static void main(String[] args) { (new Thread(new Solution())).start(); } BufferedReader in; PrintWriter out; StringTokenizer st; String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } class Dom implements Comparable<Dom>{ int x, a; public int compareTo(Dom o) { return x - o.x; } } void solve() throws Exception { int n = nextInt(); int t = nextInt() * 2; Dom[] a = new Dom[n]; for (int i = 0; i < n; i++) { a[i] = new Dom(); a[i].x = nextInt() * 2; a[i].a = nextInt(); } Arrays.sort(a); int ans = 2; for (int i = 0; i < n - 1; i++) { if (t < a[i + 1].x - a[i + 1].a - a[i].x - a[i].a) ans += 2; if (t == a[i + 1].x - a[i + 1].a - a[i].x - a[i].a) ans += 1; } out.print(ans); } public void run() { try { Locale.setDefault(Locale.US); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); } catch (Exception e) { e.printStackTrace(); } out.flush(); } }
nlogn
15_A. Cottage Village
CODEFORCES
import java.io.*; import java.util.Arrays; import java.util.Random; import java.util.StringJoiner; import java.util.StringTokenizer; import java.util.concurrent.ThreadLocalRandom; import java.util.function.Function; public class Main { static int T; public static void main(String[] args) { FastScanner sc = new FastScanner(System.in); T = sc.nextInt(); PrintWriter pw = new PrintWriter(System.out); for (int i = 0; i < T; i++) { int n = sc.nextInt(); int[] a = sc.nextIntArray(n); int[] ans = solve(n, a); StringJoiner j = new StringJoiner(" "); for (int each : ans) { j.add(String.valueOf(each)); } pw.println(j.toString()); } pw.flush(); } static int[] solve(int N, int[] A) { // a/b が1に近いものを探す shuffle(A); Arrays.sort(A); int cur = A[0]; int time = 1; double r = 0; int prev = -1; int a = -1; int b = -1; for (int i = 1; i < N; i++) { if( cur == A[i] ) { time++; if( time == 2 ) { if( prev != -1 ) { double r1 = (double)prev/cur; if( r1 > r ) { r = r1; a = prev; b = cur; } } prev = cur; } if( time == 4 ) { return new int[]{cur, cur, cur, cur}; } } else { time = 1; cur = A[i]; } } return new int[]{a, a, b, b}; } static void shuffle(int[] a) { Random r = ThreadLocalRandom.current(); for (int i = a.length-1; i >= 0; i--) { int j = r.nextInt(i+1); int t = a[i]; a[i] = a[j]; a[j] = t; } } @SuppressWarnings("unused") static class FastScanner { private BufferedReader reader; private StringTokenizer tokenizer; FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } long nextLong() { return Long.parseLong(next()); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArray(int n, int delta) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt() + delta; return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } static <A> void writeLines(A[] as, Function<A, String> f) { PrintWriter pw = new PrintWriter(System.out); for (A a : as) { pw.println(f.apply(a)); } pw.flush(); } static void writeLines(int[] as) { PrintWriter pw = new PrintWriter(System.out); for (int a : as) pw.println(a); pw.flush(); } static void writeLines(long[] as) { PrintWriter pw = new PrintWriter(System.out); for (long a : as) pw.println(a); pw.flush(); } static int max(int... as) { int max = Integer.MIN_VALUE; for (int a : as) max = Math.max(a, max); return max; } static int min(int... as) { int min = Integer.MAX_VALUE; for (int a : as) min = Math.min(a, min); return min; } static void debug(Object... args) { StringJoiner j = new StringJoiner(" "); for (Object arg : args) { if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg)); else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg)); else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg)); else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg)); else j.add(arg.toString()); } System.err.println(j.toString()); } }
nlogn
1027_C. Minimum Value Rectangle
CODEFORCES
import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class Sockets { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(), m = in.nextInt(), socket = in.nextInt(); int[] filters = new int[n]; for (int i = 0; i < n; i++ ) { filters[i] = in.nextInt(); } Arrays.sort(filters); int result = 0, index = n - 1; while ( m > socket && index >= 0) { socket += filters[index] - 1; result += 1; index -= 1; } out.println(m > socket ? -1 : result); out.close(); } }
nlogn
257_A. Sockets
CODEFORCES
import java.io.*; import java.util.*; import static java.lang.Math.*; public class A implements Runnable { final boolean LOCAL = System.getProperty("ONLINE_JUDGE") == null; BufferedReader in; PrintWriter out; StringTokenizer tok; public static void main(String[] args) { new Thread(null, new A(), "", 256*1024*1024).start(); } public void run() { try { long t1 = 0, t2 = 0, m1 = 0, m2 = 0; if (LOCAL) { t1 = System.currentTimeMillis(); m1 = Runtime.getRuntime().freeMemory(); } Locale.setDefault(Locale.US); if (LOCAL) { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } else { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } tok = new StringTokenizer(""); solve(); in.close(); out.close(); if (LOCAL) { t2 = System.currentTimeMillis(); m2 = Runtime.getRuntime().freeMemory(); System.err.println("Time = " + (t2 - t1) + " ms."); System.err.println("Memory = " + ((m1 - m2) / 1024) + " KB."); } } catch (Throwable e) { e.printStackTrace(System.err); throw new RuntimeException(); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { String line = in.readLine(); if (line == null) return null; tok = new StringTokenizer(line); } 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()); } static class Mergesort { private Mergesort() {} public static void sort(int[] a) { mergesort(a, 0, a.length - 1); } public static void sort(long[] a) { mergesort(a, 0, a.length - 1); } public static void sort(double[] a) { mergesort(a, 0, a.length - 1); } private static final int MAGIC_VALUE = 42; private static void mergesort(int[] a, int leftIndex, int rightIndex) { if (leftIndex < rightIndex) { if (rightIndex - leftIndex <= MAGIC_VALUE) { insertionSort(a, leftIndex, rightIndex); } else { int middleIndex = (leftIndex + rightIndex) / 2; mergesort(a, leftIndex, middleIndex); mergesort(a, middleIndex + 1, rightIndex); merge(a, leftIndex, middleIndex, rightIndex); } } } private static void mergesort(long[] a, int leftIndex, int rightIndex) { if (leftIndex < rightIndex) { if (rightIndex - leftIndex <= MAGIC_VALUE) { insertionSort(a, leftIndex, rightIndex); } else { int middleIndex = (leftIndex + rightIndex) / 2; mergesort(a, leftIndex, middleIndex); mergesort(a, middleIndex + 1, rightIndex); merge(a, leftIndex, middleIndex, rightIndex); } } } private static void mergesort(double[] a, int leftIndex, int rightIndex) { if (leftIndex < rightIndex) { if (rightIndex - leftIndex <= MAGIC_VALUE) { insertionSort(a, leftIndex, rightIndex); } else { int middleIndex = (leftIndex + rightIndex) / 2; mergesort(a, leftIndex, middleIndex); mergesort(a, middleIndex + 1, rightIndex); merge(a, leftIndex, middleIndex, rightIndex); } } } private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - leftIndex + 1; int length2 = rightIndex - middleIndex; int[] leftArray = new int[length1]; int[] rightArray = new int[length2]; System.arraycopy(a, leftIndex, leftArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = leftArray[i++]; } else { a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++]; } } } private static void merge(long[] a, int leftIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - leftIndex + 1; int length2 = rightIndex - middleIndex; long[] leftArray = new long[length1]; long[] rightArray = new long[length2]; System.arraycopy(a, leftIndex, leftArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = leftArray[i++]; } else { a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++]; } } } private static void merge(double[] a, int leftIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - leftIndex + 1; int length2 = rightIndex - middleIndex; double[] leftArray = new double[length1]; double[] rightArray = new double[length2]; System.arraycopy(a, leftIndex, leftArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = leftArray[i++]; } else { a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++]; } } } private static void insertionSort(int[] a, int leftIndex, int rightIndex) { for (int i = leftIndex + 1; i <= rightIndex; i++) { int current = a[i]; int j = i - 1; while (j >= leftIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } private static void insertionSort(long[] a, int leftIndex, int rightIndex) { for (int i = leftIndex + 1; i <= rightIndex; i++) { long current = a[i]; int j = i - 1; while (j >= leftIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } private static void insertionSort(double[] a, int leftIndex, int rightIndex) { for (int i = leftIndex + 1; i <= rightIndex; i++) { double current = a[i]; int j = i - 1; while (j >= leftIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } } void debug(Object... o) { if (LOCAL) { System.err.println(Arrays.deepToString(o)); } } //------------------------------------------------------------------------------ void solve() throws IOException { int n = readInt(); int m = readInt(); int k = readInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = readInt(); } Mergesort.sort(a); for (int need = 0; need <= n; need++) { int cnt = k; for (int i = 0; i < need; i++) { cnt += a[n - i - 1] - 1; } if (cnt >= m) { out.println(need); return; } } out.println(-1); } }
nlogn
257_A. Sockets
CODEFORCES
import java.io.*; import java.math.BigInteger; import java.util.*; public class A { String line; StringTokenizer inputParser; BufferedReader is; FileInputStream fstream; DataInputStream in; String FInput=""; void openInput(String file) { if(file==null)is = new BufferedReader(new InputStreamReader(System.in));//stdin else { try{ fstream = new FileInputStream(file); in = new DataInputStream(fstream); is = new BufferedReader(new InputStreamReader(in)); }catch(Exception e) { System.err.println(e); } } } void readNextLine() { try { line = is.readLine(); inputParser = new StringTokenizer(line, " "); //System.err.println("Input: " + line); } catch (IOException e) { System.err.println("Unexpected IO ERROR: " + e); } catch (NullPointerException e) { line=null; } } int NextInt() { String n = inputParser.nextToken(); int val = Integer.parseInt(n); //System.out.println("I read this number: " + val); return val; } long NextLong() { String n = inputParser.nextToken(); long val = Long.parseLong(n); //System.out.println("I read this number: " + val); return val; } String NextString() { String n = inputParser.nextToken(); return n; } void closeInput() { try { is.close(); } catch (IOException e) { System.err.println("Unexpected IO ERROR: " + e); } } public static void main(String [] argv) { //String filePath="input.txt"; //String filePath="D:\\_d\\learn\\coursera\\algorithms and design II\\data\\knapsack2.txt"; String filePath=null; if(argv.length>0)filePath=argv[0]; new A(filePath); } public void readFInput() { for(;;) { try { readNextLine(); FInput+=line+" "; } catch(Exception e) { break; } } inputParser = new StringTokenizer(FInput, " "); } public A(String inputFile) { openInput(inputFile); readNextLine(); int N=NextInt(), M=NextInt(), K=NextInt(); int [] v = new int[N]; readNextLine(); for(int i=0; i<N; i++) { v[i]=NextInt(); } Arrays.sort(v); M-=(K-1); int id=N-1; int ret=0; while(M>1&&id>=0) { M++; M-=v[id--]; ret++; } if(id<0&&M>1)ret=-1; System.out.println(ret); closeInput(); } public static void out(Object s) { try { FileWriter fstream = new FileWriter("output.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write(s.toString()); out.close(); }catch (Exception e){ System.err.println("Error: " + e.getMessage()); } } }
nlogn
257_A. Sockets
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.Arrays; import java.util.StringTokenizer; public class A { static StringTokenizer st; static BufferedReader in; public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = nextInt(); int m = nextInt(); int k = nextInt(); int[]a = new int[n+1]; for (int i = 1; i <= n; i++) { a[i] = nextInt(); } if (k >= m) { System.out.println(0); return; } Arrays.sort(a, 1, n+1); int ans = 0; for (int i = n; i >= 1; i--) { ans++; k--; k += a[i]; if (k >= m) { System.out.println(ans); return; } } System.out.println(-1); pw.close(); } private static int nextInt() throws IOException{ return Integer.parseInt(next()); } private static long nextLong() throws IOException{ return Long.parseLong(next()); } private static double nextDouble() throws IOException{ return Double.parseDouble(next()); } private static String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } }
nlogn
257_A. Sockets
CODEFORCES
import java.util.*; import java.io.*; public class Sockets { BufferedReader in; PrintWriter out; StringTokenizer st; 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()); } void solve() throws Exception { int n = nextInt(); int m = nextInt(); int k = nextInt(); List<Integer> fs = new ArrayList<Integer>(); for (int i = 0; i < n; i++) fs.add(nextInt()); Collections.sort(fs); Collections.reverse(fs); int c = k; for (int i = 0; i < fs.size(); i++) { if (c >= m) { out.println(i); return; } c += (fs.get(i)-1); } if (c>=m) out.println(fs.size()); else out.println(-1); } void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); // in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter(System.out); solve(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { out.close(); } } public static void main(String[] args) { new Sockets().run(); } }
nlogn
257_A. Sockets
CODEFORCES
import java.io.BufferedReader; import java.io.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author c0der */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); int[] a = new int[ n ]; for (int i=0; i<n; i++) a[i] = in.nextInt(); if ( m <= k ) out.println( 0 ); else { m -= k - 1; Arrays.sort( a ); int ans = 0; for (int i=n-1; i>=0; i--) { ans++; //out.println(m+" "+a[i]); m -= a[ i ]; if ( m <= 0 ) break; m++; } if ( m > 0 ) out.println( -1 ); else out.println( ans ); } } } class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = null; } public String next() { while (st==null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
nlogn
257_A. Sockets
CODEFORCES
import java.util.Arrays; import java.util.InputMismatchException; import java.io.*; /** * Generated by Contest helper plug-in * Actual solution is at the bottom */ public class Main { public static void main(String[] args) { InputReader in = new StreamInputReader(System.in); PrintWriter out = new PrintWriter(System.out); run(in, out); } public static void run(InputReader in, PrintWriter out) { Solver solver = new TaskB(); solver.solve(1, in, out); Exit.exit(in, out); } } abstract class InputReader { private boolean finished = false; public abstract int read(); public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); 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; } public void setFinished(boolean finished) { this.finished = finished; } public abstract void close(); } class StreamInputReader extends InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public StreamInputReader(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 void close() { try { stream.close(); } catch (IOException ignored) { } } } class Exit { private Exit() { } public static void exit(InputReader in, PrintWriter out) { in.setFinished(true); in.close(); out.close(); } } interface Solver { public void solve(int testNumber, InputReader in, PrintWriter out); } class TaskB implements Solver { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.readInt(); int m = in.readInt(); int k = in.readInt(); int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = in.readInt(); Arrays.sort(a); if(k>=m) { out.println(0); } else { for(int i=n-1;i>=0;i--) { k += (a[i]-1); if(k>=m) { out.println(n-i); return; } } if(k<m) out.println(-1); } } }
nlogn
257_A. Sockets
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 n = nextInt(), k = nextInt(), s = nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = -nextInt(); Arrays.sort(a); for(int i=0;i<n;i++) { if (s>=k) { out.println(i); return; } s += -a[i]; s--; } if (s<k) out.println(-1); else out.println(n); } void run() { try { Locale.setDefault(Locale.US); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } public static void main(String[] args) { new A().run(); } }
nlogn
257_A. Sockets
CODEFORCES
//package Round_159; import java.util.*; import java.io.*; import static java.lang.Math.*; public class a { void solve() throws Exception { int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); int a[] = new int[n]; for (int i = 0; i<n; i++){ a[i] = in.nextInt(); } Arrays.sort(a); int sum = 0; if (k >= m){ out.println(0); return; } sum = a[n-1] + k - 1; int j = 1; for (int i = n-2; i >=0 && sum < m; i--, j++){ sum += a[i] - 1; } if (sum < m){ out.println(-1); }else{ out.println(j); } } FastScanner in; PrintWriter out; String input = ""; String output = ""; void run() { try { if (input.length() > 0) { in = new FastScanner(new BufferedReader(new FileReader(input))); } else in = new FastScanner(new BufferedReader(new InputStreamReader( System.in))); if (output.length() > 0) out = new PrintWriter(new FileWriter(output)); else out = new PrintWriter(System.out); solve(); out.flush(); out.close(); } catch (Exception ex) { ex.printStackTrace(); out.flush(); out.close(); } finally { out.close(); } } public static void main(String[] args) { new a().run(); } class FastScanner { BufferedReader bf; StringTokenizer st; public FastScanner(BufferedReader bf) { this.bf = bf; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(bf.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public String nextLine() throws IOException { return bf.readLine(); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
nlogn
257_A. Sockets
CODEFORCES
import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.PrintStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author coderbd */ 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 m, n, k; n = in.readInt(); m = in.readInt(); k = in.readInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.readInt() - 1; Arrays.sort(a); int ans = -1; if (k >= m) ans = 0; else for (int i = 0; i < n; i++) { k += a[n-i-1]; if (k >= m) { ans = i + 1; break; } } System.out.println(ans); } } class InputReader { private boolean finished = false; 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 peek() { if (numChars == -1) return -1; if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } 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 = 0L; 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(); } public static 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) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public 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 == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m *= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) read(); return value == -1; } public String next() { return readString(); } } 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 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 printLine(char[] array) { writer.print(array); } public void printFormat(String format, Object...objects) { writer.printf(format, objects); } public void close() { writer.close(); } public void flush() { writer.flush(); } }
nlogn
257_A. Sockets
CODEFORCES
import java.awt.Point; import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class Start { 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 Start().run(); // Sworn to fight and die } public static void mergeSort(int[] a) { mergeSort(a, 0, a.length - 1); } private static void mergeSort(int[] a, int levtIndex, int rightIndex) { final int MAGIC_VALUE = 50; if (levtIndex < rightIndex) { if (rightIndex - levtIndex <= MAGIC_VALUE) { insertionSort(a, levtIndex, rightIndex); } else { int middleIndex = (levtIndex + rightIndex) / 2; mergeSort(a, levtIndex, middleIndex); mergeSort(a, middleIndex + 1, rightIndex); merge(a, levtIndex, middleIndex, rightIndex); } } } private static void merge(int[] a, int levtIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - levtIndex + 1; int length2 = rightIndex - middleIndex; int[] levtArray = new int[length1]; int[] rightArray = new int[length2]; System.arraycopy(a, levtIndex, levtArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = levtIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = levtArray[i++]; } else { a[k] = levtArray[i] <= rightArray[j] ? levtArray[i++] : rightArray[j++]; } } } private static void insertionSort(int[] a, int levtIndex, int rightIndex) { for (int i = levtIndex + 1; i <= rightIndex; i++) { int current = a[i]; int j = i - 1; while (j >= levtIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } 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); } } class LOL implements Comparable<LOL> { int x; int y; int num; public LOL(int x, int y,int num) { this.x = x; this.y = y; this.num = num; } @Override public int compareTo(LOL o) { return x - o.x; // ----> // return o.x - x; // <---- // return o.y-y; } } public void solve() throws IOException { int n = readInt(); int m = readInt(); int k = readInt(); int [] a = new int [n]; for (int i = 0; i < n; i++){ a[i] = readInt(); } mergeSort(a); if (k>=m){ out.print(0); return; } int ans = 0; k--; for (int i = n-1; i >=0; i--){ ans += a[i]; if (ans + k >= m){ out.print(n-i); return; } else { k--; } } out.print(-1); } }
nlogn
257_A. Sockets
CODEFORCES
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); Arrays.sort(a); int ans = 0, r = k, p = n-1; while (r < m && p >= 0) { r = r - 1 + a[p]; p--; ans++; } if (r < m) out.println("-1"); else out.println(ans); out.flush(); } }
nlogn
257_A. Sockets
CODEFORCES