Hatena::Grouptopcoder

tsubosakaの日記

2009-05-13

SRM440 Div1 Medium

| 05:59

一個ルートノードを決めて、探索していったときに(あるノードの期待値) = a * (親ノードの期待値) + bと書け(ノードが葉のときはa=1.0,b=1.0、チーズのときはa=b=0.0となる)再帰的に計算するとルートノードでの期待値となる。一回の計算にノード数分に比例する時間がかかり、それをすべてのノードに対して実行するのでO(N^2)となる。

また、cafelierさんによると期待値 = 親の期待値 + 子孫の数*2 + 1となるのでこれだとチーズから始めればO(N)で計算できるけどこの式を思いつくのは難しいと思う(証明は天下り的に子供の期待値がこの形で書けているとして代入していけば帰納法で示せる。)

import java.util.*;

class Vertex{
  List<Integer> adjoint;
  Vertex(){
    adjoint = new ArrayList<Integer>();
  }
  int size(){
    return adjoint.size();
  }
  void add(int e){
    adjoint.add(e);
  }  
}

public class MazeWandering {  
  int cheeseId;
  public double expectedTime(String[] maze) {
    Vertex[] vs = createGraph(maze);
    double res = 0.0;
    for(int i = 0 ; i < vs.length ; i++){
      res += rec(vs , -1 , i)[1];
    }
    return res / vs.length;
  }
  
  double[] rec(Vertex vs[] , int parent , int cp){
    if(cp == cheeseId)
      return new double[]{0.0 , 0.0};
    Vertex v = vs[cp];
    double res[] = new double[2];
    res[0] = 0.0;
    res[1] = 1.0;    
    for(int adj : v.adjoint){
      if(adj == parent)continue;
      double a[] = rec(vs , cp , adj);
      res[0] += a[0] / v.size();
      res[1] += a[1] / v.size();
    }
    res[1] /= (1 - res[0]);
    res[0] = 1.0 / (v.size() * (1.0 - res[0]));
    return res;
  }
  
  private Vertex[] createGraph(String[] maze) {
    int W = maze[0].length();
    int H = maze.length;
    int ids[][] = new int[H][W];
    int id = 0;
    for (int i = 0; i < H; i++) {
      Arrays.fill(ids[i], -1);
      for (int j = 0; j < W; j++) {
        char c = maze[i].charAt(j);
        if(c == 'X')continue;
        if(c == '*')
          cheeseId = id;
        ids[i][j] = id++;
      }
    }
    int n = id;
    Vertex vs[] = new Vertex[n];
    int dx[] = { 1, -1, 0, 0 };
    int dy[] = { 0, 0, 1, -1 };
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        char c = maze[i].charAt(j);
        if (c == 'X') 
          continue;        
        int cid = ids[i][j];
        Vertex v = new Vertex();
        for (int k = 0; k < dx.length; k++) {
          int x = j + dx[k];
          int y = i + dy[k];
          if (x < 0 || y < 0 || x >= W || y >= H)
            continue;
          if (ids[y][x] < 0)
            continue;
          v.add(ids[y][x]);
        }
        vs[cid] = v;
      }
    }
    return vs;
  }  
}

2009-04-20

SRM 439 Div1 Medium

| 20:08

言われるとなるほどと思うのだが本番ではなかなか思いつかない。

まず、$の数が1つのときは A B B B ... という周期的な列になるため容易に求まる。また$の数が2つ以上のときは変数の長さが常に2倍以上になるため、s <= 32のときのみを考えればよい。

import java.util.*;
public class EndlessStringMachine {
  int cnt(String s) {
    int cnt = 0;
    for (char c : s.toCharArray())
      if (c == '$')cnt++;
    return cnt;
  }
  char rec(String in , String p , int s , int i , List<Long> ls){
    if(s == 0)return in.charAt(i);
    long plen = ls.get(s - 1);
    for(char c : p.toCharArray())
      if(c == '$')
        if(i < plen)return rec(in, p, s - 1, i, ls);
        else i -= plen;
      else if(i-- == 0)return c;
    return '-';
  }
  public String getFragment(String in, String p, int s, int L, int R) {
    L--; R--;
    long dn = cnt(p);
    StringBuilder sb = new StringBuilder();
    if (dn == 1) {
      int plen = p.length() - 1;
      long tlen = in.length() + plen * (long)s;
      for (int i = L; i <= R; i++)
        if (i < in.length())sb.append(in.charAt(i));
        else if(i >= tlen)sb.append('-');
        else sb.append( p.charAt(1 + (i - in.length()) % plen));      
    } else {
      List<Long> ls = new ArrayList<Long>();
      ls.add((long)in.length());
      for (int i = 1; i <= s; i++) {
        long li = (ls.get(i - 1) * dn) + (p.length() - dn);
        ls.add(li);
        if (li >= R)break;
      }
      for(int index = L ; index <= R ; index++)
        sb.append(rec(in, p, ls.size() - 1 , index, ls));      
    }
    return sb.toString();
  }
}

2009-01-15

SRM 286 Div1 Medium

| 09:33

常にプレイヤー0から始めるとすれば各プレイヤーの勝利数は(green,red,black)の値で一意に定まる.残りの色の数を状態としてメモ付き探索.

public class BallGift {
  long memo[][][][];
  int players;
  long[] rec(int green , int red , int black, int pnum){
    if(memo[green][red][black] != null){
      return memo[green][red][black];
    }
    long res[] = new long[players];
    if(green == 0 && red == 0 && black == 0){
      //player 0 win
      res[0] = 1;
    }else{      
      if(green > 0){
        long next[] = rec(green - 1 , red , black , pnum);
        for(int i = 0 ; i < pnum ; i++){
          res[(i + 1) % pnum] += next[i];
        }
      }
      if(red > 0){
        long next[] = rec(green  , red - 1, black , pnum);
        for(int i = 0 ; i < pnum ; i++){
          res[pnum - 1 - i] += next[i];
        }
      }
      if(black > 0){
        long next[] = rec(green  , red , black - 1, pnum - 1);
        //remove player 0
        for(int i = 1 ; i < pnum ; i++){
          res[i] += next[i - 1];
        }
      }
    }
    return memo[green][red][black] = res;
  }
  public int bestPosition(int players, int green, int red, int black) {
    this.players = players;
    memo = new long[green + 1][red + 1][black + 1][];    
    long winnum[] = rec(green, red, black , players);
    int res = 0;
    for(int i = 1 ; i < winnum.length ; i++)
      if(winnum[res] < winnum[i])
        res = i;
    return res;
  }
}

2009-01-14

SRM 173 Div1 Hard

| 19:25

与えられた点からn個選んだ時に選んだ点の凸包の面積を最大にせよという問題.

  • 候補点は凸包の中から選べばよい
  • 始点を固定した後に、一個前の点と残りの選べる点の個数を状態としてメモ付き探索を行う
  • 再帰式はf(start , p , n) = max_q ( f(start , q , n - 1) + |start-p-q|)と書ける
import java.util.*;

public class ElectronicScarecrows {
  class Point {
    double x, y;

    Point(double a, double b) {
      this.x = a;
      this.y = b;
    }

    @Override
    public String toString() {
      return x + " : " + y;
    }

    double norm() {
      return x * x + y * y;
    }
  }
  double cross(Point a, Point b, Point c) {
    double res = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
    return res;
  }
  double cmp(Point a , Point b){
    if(a.x != b.x)return a.x - b.x;
    return a.y - b.y;
  }
  double dist(Point a , Point b){
    double dx = a.x - b.x;
    double dy = a.y - b.y;
    return dx * dx + dy * dy;
  }
  List<Point> convexHull(List<Point> ps) {
    List<Point> res = new ArrayList<Point>();
    if(ps.size() < 3)return res;
    int N = ps.size();
    int p = 0;
    for(int i = 1 ; i < N ; i++)
      if(cmp(ps.get(i) , ps.get(p)) < 0)
        p = i;
    boolean used[] = new boolean[N];
    int st = p;
    do{
      int n = -1;
      double dist = 0.0;
      for(int i = 0 ; i < N ; i++){
        if(i == p)continue;
        if(used[i])continue;
        if(n == -1)n = i;
        double cross = cross(ps.get(p) , ps.get(i) , ps.get(n));
        double d  = dist(ps.get(i) , ps.get(p));
        if(cross < 0){
          n = i;
          dist = d;
        }else if(Math.abs(cross) < 1.0E-11){
          if(d > dist){
            dist = d;
            n = i;
          }
        }
      }
      res.add(ps.get(p));
      p = n;
      used[p] = true;
    }while(st != p);
    return res;
  }
  double triarea(Point p0 , Point p1 , Point p2){
    return Math.abs(cross(p0 , p1 , p2)) * 0.5;
  }
  double memo[][];
  Point ps[];
  double rec(int pstart , int prev , int n){
    if(n == 0)return 0.0;
    if(memo[prev][n] >= 0.0)
      return memo[prev][n];
    double res = 0.0;
    int p = (prev + 1) % ps.length;
    while(p != pstart){
      double t = triarea(ps[pstart] , ps[prev] , ps[p]);
      double r = rec(pstart , p , n - 1);
      res = Math.max(res, t + r);
      p = (p + 1) % ps.length;      
    }   
    return memo[prev][n] = res;
  }
  public double largestArea(int[] x, int[] y, int n) {
    List<Point> plist = new ArrayList<Point>();
    for(int i = 0 ; i < x.length ; i++){
      plist.add(new Point(x[i] , y[i]));
    }
    List<Point> hull = convexHull(plist);
    this.ps = hull.toArray(new Point[0]);
    int hs = hull.size();
    double res = 0.0;
    for(int pstart = 0 ; pstart < hull.size() ; pstart++){
      memo = new double[hs][n];
      for(int i = 0 ; i < hs ; i++)
          Arrays.fill(memo[i] , -1);
      for(int p0 = pstart + 1 ; p0 < hs ; p0++){
        double r = rec(pstart , p0  , n - 2);
        res = Math.max(res, r);
      }
    }
    return res;
  }
}

RoberGrotlyRoberGrotly2017/05/08 23:31Amoxicilina Internet Where To Buy Levitra Deezer <a href=http://byuvaigranonile.com>viagra</a> Malegra Amoxicillin Trihydrate 250 Dosage Propecia Zwanger Worden