Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Solutions:
- - Slime Combining:
- n = int(raw_input())
- print " ".join(str(i+1) for i in range(20,-1,-1) if (n&(1<<i)) > 0)
- - Guess the permutation:
- import sys
- f = sys.stdin
- n = int(f.readline())
- arr = [max(map(int, f.readline().split())) for i in range(n)]
- arr[arr.index(n-1)] = n
- print " ".join(map(str,arr))
- - Constellation:
- import sys
- import random
- from fractions import gcd
- f = sys.stdin
- n = int(f.readline())
- x,y = zip(*[map(int, f.readline().split()) for i in range(n)])
- st = random.randint(0, n-1)
- dist = [(x[i]-x[st])*(x[i]-x[st])+(y[i]-y[st])*(y[i]-y[st]) for i in range(n)]
- def getSlope(x1,y1):
- g = gcd(abs(x1),abs(y1))
- x1,y1 = x1/g,y1/g
- if x1 < 0 or (x1 == 0 and y1 < 0):
- x1,y1 = -x1,-y1
- return (x1,y1)
- s = {}
- for i in xrange(n):
- if i == st: continue
- slope = getSlope(x[i] - x[st], y[i] - y[st])
- if slope not in s or dist[i] < dist[s[slope]]:
- s[slope] = i
- lst = sorted(s.values(), key=lambda x: dist[x])
- print st+1, lst[0]+1, lst[1]+1
- - Hamiltonian Spanning Tree:
- import java.io.*;
- import java.util.*;
- public class HamiltonianTree {
- private static InputReader in;
- private static PrintWriter out;
- public static ArrayList<Integer>[] graph;
- public static void main (String[] args) {
- in = new InputReader(System.in);
- out = new PrintWriter(System.out, true);
- int n = in.nextInt();
- long x = in.nextInt(), y = in.nextInt();
- if (x > y) {
- int[] deg = new int[n];
- for (int i = 0; i < n-1; i++) {
- int a = in.nextInt()-1, b = in.nextInt()-1;
- deg[a]++;
- deg[b]++;
- }
- int max = 0;
- for (int i = 0; i < n; i++) {
- max = Math.max(max, deg[i]);
- }
- out.println(y * (n - 2) + (max == n-1 ? x : y));
- } else {
- graph = new ArrayList[n];
- for (int i = 0; i < n; i++) graph[i] = new ArrayList<>();
- for (int i = 0; i < n-1; i++) {
- int a = in.nextInt()-1, b = in.nextInt()-1;
- graph[a].add(b);
- graph[b].add(a);
- }
- ans = 0;
- dfs(0, -1);
- out.println((n-1-ans)*y + ans*x);
- }
- out.close();
- System.exit(0);
- }
- public static long ans;
- public static int dfs(int node, int par) {
- int left = 2;
- for (int next : graph[node]) {
- if (next == par) continue;
- int x = dfs(next, node);
- if (left > 0 && x == 1) {
- ans++;
- left--;
- }
- }
- return left > 0 ? 1 : 0;
- }
- static class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) {
- reader = new BufferedReader(new InputStreamReader(stream), 32768);
- tokenizer = null;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- }
- }
- - Robot Arm:
- import java.io.*;
- import java.util.*;
- public class RobotArmFast {
- private static InputReader in;
- private static PrintWriter out;
- public static double[] sin, cos;
- static {
- cos = new double[360];
- sin = new double[360];
- for (int i = 0; i < 360; i++) {
- cos[i] = Math.cos(i / 180. * Math.PI);
- sin[i] = Math.sin(i / 180. * Math.PI);
- }
- }
- public static int[] length, angle;
- public static double[] x, y;
- public static int[] r;
- public static void combine(int idx) {
- int a1 = 2*idx+1, a2 = 2*idx+2;
- x[idx] = x[a1] + x[a2] * cos[r[a1]] - y[a2] * sin[r[a1]];
- y[idx] = y[a1] + y[a2] * cos[r[a1]] + x[a2] * sin[r[a1]];
- r[idx] = (r[a1] + r[a2]) % 360;
- }
- public static void update(int idx, int s, int e, int pos, int which, int delta) {
- if (pos == s && pos == e) {
- if (which == 1) length[pos] += delta;
- else {angle[pos] -= delta; if (angle[pos] < 0) angle[pos] += 360;}
- x[idx] = cos[angle[pos]] * length[pos];
- y[idx] = sin[angle[pos]] * length[pos];
- r[idx] = angle[pos];
- return;
- }
- int mid = (s + e) / 2;
- if (mid >= pos) update(2*idx+1, s, mid, pos, which, delta);
- else update(2*idx+2, mid+1, e, pos, which, delta);
- combine(idx);
- }
- public static void init(int idx, int s, int e) {
- if (s == e) {
- length[s] = 1;
- angle[s] = 0;
- } else {
- int mid = (s + e) / 2;
- init(2*idx+1, s, mid);
- init(2*idx+2, mid+1, e);
- }
- x[idx] = e-s+1;
- y[idx] = 0;
- r[idx] = 0;
- }
- public static void main (String[] args) {
- in = new InputReader(System.in);
- out = new PrintWriter(System.out, true);
- int n = in.nextInt(), m = in.nextInt();
- length = new int[n+1];
- angle = new int[n+1];
- x = new double[4*n];
- y = new double[4*n];
- r = new int[4*n];
- init(0, 1, n);
- StringBuilder b = new StringBuilder();
- for (int i = 0; i < m; i++) {
- int which = in.nextInt(), segment = in.nextInt(), delta = in.nextInt();
- update(0, 1, n, segment, which, delta);
- b.append(String.format("%.10f %.10f\n", x[0], y[0]));
- }
- out.print(b.toString());
- out.close();
- System.exit(0);
- }
- static class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) {
- reader = new BufferedReader(new InputStreamReader(stream), 32768);
- tokenizer = null;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- }
- }
- - Double Knapsack:
- import java.io.*;
- import java.util.*;
- public class DoubleKnapsack {
- private static InputReader in;
- private static PrintWriter out;
- public static void main(String[] args) {
- in = new InputReader(System.in);
- out = new PrintWriter(System.out, true);
- int n = in.nextInt();
- int[] arr = new int[n];
- int[] brr = new int[n];
- long sa = 0, sb = 0;
- for (int i = 0; i < n; i++) sa += arr[i] = in.nextInt();
- for (int i = 0; i < n; i++) sb += brr[i] = in.nextInt();
- boolean swap = false;
- if (sa > sb) {
- swap = true;
- int[] t = arr;
- arr = brr;
- brr = t;
- }
- long xs = 0, ys = 0;
- int j = -1;
- long[] prev = new long[n];
- Arrays.fill(prev, -1);
- prev[0] = 0;
- for (int i = 0; i < n; i++) {
- xs += arr[i];
- while (j+1 < n && ys+brr[j+1] <= xs) {
- ys += brr[++j];
- }
- int diff = (int)(xs-ys);
- if (prev[diff] != -1) {
- int px = (int)(prev[diff]%n)-1, py = (int)(prev[diff]/n)-1;
- if (swap) {
- printAns(py, j);
- printAns(px, i);
- } else {
- printAns(px, i);
- printAns(py, j);
- }
- out.close();
- System.exit(0);
- }
- prev[diff] = (j+1)*(long)n + (i+1);
- }
- }
- public static void printAns(int from, int to) {
- out.println(to-from);
- for (int i = from+1; i <= to; i++) {
- if (i != from+1) out.print(" ");
- out.print(i+1);
- }
- out.println();
- }
- static class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) {
- reader = new BufferedReader(new InputStreamReader(stream));
- tokenizer = null;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- }
- }
- - Combining Slimes:
- import java.io.*;
- import java.util.*;
- public class SlimeCombining {
- private static InputReader in;
- private static PrintWriter out;
- public static double EPS = 1e-15;
- public static int maxp = 50;
- public static void main (String[] args) {
- in = new InputReader(System.in);
- out = new PrintWriter(System.out, true);
- int n = in.nextInt();
- int p = in.nextInt();
- double p2 = p / 1000000000., p4 = 1 - p2;
- double[][] a = new double[maxp][maxp];
- double[][] b = new double[maxp][maxp];
- for (int len = 1; len < maxp; len++) {
- for (int pow = 1; pow < maxp; pow++) {
- if (pow == 1) a[len][pow] += p2;
- if (pow == 2) {
- a[len][pow] += p4;
- b[len][pow] += p4;
- }
- a[len][pow] += a[len-1][pow-1] * a[len][pow-1];
- b[len][pow] += a[len-1][pow-1] * b[len][pow-1];
- }
- }
- for (int len = maxp - 1; len >= 1; len--) {
- for (int pow = 1; pow < maxp; pow++) {
- a[len][pow] *= 1 - a[len-1][pow];
- b[len][pow] *= 1 - a[len-1][pow];
- }
- }
- // value of a slime that has been merged i times
- long[] vals = new long[maxp];
- for (int i = 0; i < maxp; i++) vals[i] = i;//1l << i;
- // manually do first few cases
- double[][] dp = new double[maxp][maxp];
- double[][] sum = new double[maxp][maxp];
- for (int cur = 1; cur < maxp; cur++)
- dp[maxp-1][cur] = vals[cur];
- // manual dp
- for (int i = maxp-2; i >= 0; i--) {
- for (int cur = 1; cur < maxp; cur++) {
- for (int next = 1; next < maxp; next++) {
- if (cur == next) continue;
- if (cur == 1) {
- dp[i][cur] += b[maxp-i-1][next] * dp[i+1][next];
- sum[i][cur] += b[maxp-i-1][next];
- } else {
- if (cur < next) continue;
- dp[i][cur] += a[maxp-i-1][next] * dp[i+1][next];
- sum[i][cur] += a[maxp-i-1][next];
- }
- }
- }
- for (int cur = 1; cur < maxp; cur++) {
- dp[i][cur] = vals[cur] + dp[i][cur] / sum[i][cur];
- }
- }
- if (n < maxp) {
- int k = (int)n;
- double exp = 0;
- for (int i = 1; i < maxp; i++) {
- exp += a[k][i] * dp[maxp-k][i];
- }
- out.printf("%.15f\n", exp);
- out.close();
- System.exit(0);
- }
- double[] vec = new double[maxp+1];
- System.arraycopy(dp[0], 0, vec, 0, maxp);
- vec[maxp] = 1;
- double[][] mat = new double[maxp+1][maxp+1];
- mat[maxp][maxp] = 1;
- for (int i = 1; i < maxp; i++) {
- for (int j = 1; j < maxp; j++) {
- if (i == j) continue;
- if (i == 1) {
- mat[i][j] += b[maxp-1][j] / sum[0][i];
- } else {
- if (i < j) continue;
- mat[i][j] += a[maxp-1][j] / sum[0][i];
- }
- }
- mat[i][maxp] += vals[i];
- }
- mat = mat_exp(mat, n - maxp);
- vec = vec_mult(mat, vec);
- double exp = 0;
- for (int i = 1; i < maxp; i++) {
- exp += a[maxp-1][i] * vec[i];
- }
- out.printf("%.15f\n", exp);
- out.close();
- System.exit(0);
- }
- public static double[] vec_mult(double[][] A, double[] B) {
- double[] C = new double[A.length];
- for (int i = 0; i < A.length; i++) {
- for (int j = 0; j < B.length; j++) {
- C[i] += A[i][j] * B[j];
- }
- }
- return C;
- }
- private static double[][] mat_exp(double[][] A, long e) {
- if (e == 0) {
- double[][] ret = new double[A.length][A.length];
- for (int i = 0; i < A.length; i++) ret[i][i] = 1.0;
- return ret;
- }
- if (e == 1)
- return A;
- else if (e % 2 == 0) {
- double[][] A1 = mat_exp(A, e / 2);
- return matrix_mult(A1, A1);
- } else
- return matrix_mult(A, mat_exp(A, e - 1));
- }
- private static double[][] matrix_mult(double[][] A, double[][] B) {
- double[][] C = new double[A.length][A.length];
- for (int i = 0; i < A.length; i++)
- for (int j = 0; j < A.length; j++)
- for (int k = 0; k < A.length; k++)
- C[i][k] += A[i][j] * B[j][k];
- return C;
- }
- static class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) {
- reader = new BufferedReader(new InputStreamReader(stream), 32768);
- tokenizer = null;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment