码迷,mamicode.com
首页 > 其他好文 > 详细

topcoder srm 470 div1

时间:2017-11-05 23:36:15      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:期望   raw   部分   character   bre   update   4.0   oid   max   

problem1 link

首先预处理在已选字母的状态为$state$时是否可达。

然后就是按照题目进行dp。设$f[i]$表示已选字母集合为$i$时的结果。

每次可以根据$i$中含有的字母是奇数还是偶数个来确定现在该轮到谁选择。

problem2 link

交点可以分为三部分:

(1)已经确定的线之间的交点;

(2)未确定的线之间的交点;

(3)已确定的线与未确定线之间的交点。

第一部分比较容易计算。

对于第二部分,设$f[i]$表示$i$对点之间连线交点期望。那么对于$f[i+1]$来说,就是在最后添加一对点,然后枚举这一对点中的一个与对面集合中那个点相连,可以得到:$f[i+1]=\frac{1}{i+1}\sum_{k=1}^{i+1}(f[i]+k-1)=f[i]+\frac{i-1}{2}$,进而可以直接计算出$f[n]$的通项公式。

对于第三部分,可以枚举下半部分每个未匹配的点与每条确定的点有交点的概率。

problem3 link

 

code for problem1

import java.util.*;
import java.math.*;
import static java.lang.Math.*;

public class DoorsGame {

	static final int N = 1 << 16;

	int[] bitNum = new int[N];


	boolean[] g1 = null;
	boolean[] g2 = null;

	int[] f = new int[N];

	int validMask = 0;

	public int determineOutcome(String doors, int trophy) {
		for (int i = 1; i < N; ++ i) {
			bitNum[i] = bitNum[i >> 1] + (i & 1);
		}
		String s1 = doors.substring(0, trophy);
		String s2 = (new StringBuilder(doors.substring(trophy))).reverse().toString();
		g1 = cal(s1);
		g2 = cal(s2);
		Arrays.fill(f, Integer.MAX_VALUE);
		for (int i = 0; i < doors.length(); ++ i) {
			validMask |= 1 << (doors.charAt(i) - ‘A‘);
		}
		return dfs(0);
	}

	int dfs(int mask) {

		if (g1[mask]) {
			if (g2[mask]) {
				return 0;
			}
			return bitNum[mask];
		}
		else {
			if (g2[mask]) {
				return -bitNum[mask];
			}
		}
		if (f[mask] != Integer.MAX_VALUE) {
			return f[mask];
		}

		if (contains(bitNum[mask], 0)) {
			for (int i = 0; i < 16; ++ i) {
				if (!contains(mask, i) && contains(validMask, i)) {
					int ans = dfs(mask | (1 << i));
					if (f[mask] == Integer.MAX_VALUE) {
						f[mask] = ans;
						continue;
					}
					if (ans < 0) {
						if (f[mask] >= 0 || f[mask] < ans) {
							f[mask] = ans;
						}
					}
					else if (ans == 0) {
						if (f[mask] > 0) {
							f[mask] = ans;
						}
					}
					else {
						if (f[mask] > 0 && f[mask] < ans) {
							f[mask] = ans;
						}
					}
				}
			}
		}
		else {
			for (int i = 0; i < 16; ++ i) {
				if (!contains(mask, i)) {
					int ans = dfs(mask | (1 << i));
					if (f[mask] == Integer.MAX_VALUE) {
						f[mask] = ans;
						continue;
					}
					if (ans < 0) {
						if (f[mask] < 0 && f[mask] > ans) {
							f[mask] = ans;
						}
					}
					else if (ans == 0) {
						if (f[mask] < 0) {
							f[mask] = ans;
						}
					}
					else {
						if (f[mask] <= 0 || f[mask] > ans) {
							f[mask] = ans;
						}
					}
				}
			}
		}
		return f[mask];
	}

	boolean[] cal(String s) {
		boolean[] result = new boolean[N];
		for (int i = 0; i < N; ++ i) {
			int p = 0;
			while (p < s.length()) {
				if (contains(i, s.charAt(p) - ‘A‘)) {
					p += 1;
				}
				else {
					break;
				}
			}
			if (p == s.length()) {
				result[i] = true;
			}
		}
		return result;
	}

	boolean contains(int mask, int t) {
		return (mask & (1 << t)) != 0;
	}

}

 

code for problem2

import java.util.*;
import java.math.*;
import static java.lang.Math.*;

public class DrawingLines {
	
	public double countLineCrossings(int n, int[] startDot, int[] endDot) {
		double result = 0;

		final int m = startDot.length;
		for (int i = 0; i < m; ++ i) {
			for (int j = i + 1; j < m; ++ j) {
				boolean upper = startDot[i] < startDot[j];
				boolean lower = endDot[i] < endDot[j];
				if (upper != lower) {
					result += 1;
				}
			}
		}
		result += (n - m) * (n - m - 1) / 4.0;
		boolean[] b = new boolean[n + 1];
		int[] pSum = new int[n + 1];
		for (int i = 0; i < m; ++ i) {
			b[endDot[i]] = true;
			pSum[startDot[i]] = 1;
		}
		for (int i = 1; i <= n; ++ i) {
			pSum[i] += pSum[i - 1];
		}
		if (n == m) {
			return result;
		}
		for (int i = 1; i <= n; ++ i) {
			if (b[i]) {
				continue;
			}
			for (int j = 0; j < m; ++ j) {
				if (endDot[j] < i) {
					result += 1.0 * (startDot[j] - pSum[startDot[j]]) / (n - m);
				}
				else {
					result += 1.0 * (n - startDot[j] - (m - pSum[startDot[j]])) / (n - m);
				}
			}
		}
		return result;
	}
}

  

code for problem3

import java.util.*;
import java.math.*;
import static java.lang.Math.*;

public class BuildingRoads {

	int getCost(char c) {
		if (‘a‘ <= c && c <= ‘z‘) {
			return c - ‘a‘ + 1;
		}
		if (‘A‘ <= c && c <= ‘Z‘) {
			return (c - ‘A‘ + 1) * 100;
		}
		if (‘1‘ <= c && c <= ‘9‘) {
			return (c - ‘0‘) * 10000;
		}
		if (c == ‘0‘) {
			return 100000;
		}
		return 0;
	}

	Map<Character,Integer> map = new HashMap<>();

	int getIndex(Character c) {
		if (c == ‘!‘ || c == ‘@‘ || c == ‘#‘ || c == ‘$‘) {
			if (map.containsKey(c)) {
				return map.get(c) + 1;
			}
			int s = map.size();
			map.put(c, s * 2);
			return s * 2;
		}
		return -1;
	}

	int[][] g = null;
	int n,m;
	int[][][] f = null;
	List<Integer> listx = new ArrayList<>();
	List<Integer> listy = new ArrayList<>();

	public int destroyRocks(String[] field) {
		n = field.length;
		m = field[0].length();
		g = new int[n][m];

		for (int i = 0; i < n; ++ i) {
			for (int j = 0; j < m; ++ j) {
				g[i][j] = getCost(field[i].charAt(j));
				int id = getIndex(field[i].charAt(j));
				if (id != -1) {
					while (listx.size() < id + 1) {
						listx.add(0);
						listy.add(0);
					}
					listx.set(id, i);
					listy.set(id, j);
				}
			}
		}
		final int M = listx.size();
		f = new int[1 << M][n][m];
		for (int i = 0; i < (1 << M); ++ i) {
			for (int j = 0; j < n; ++ j) {
				Arrays.fill(f[i][j], -1);
			}
		}
		for (int i = 0; i < M; ++ i) {
			f[1 << i][listx.get(i)][listy.get(i)] = 0;
		}
		for (int i = 0; i < (1 << M); ++ i) {
			update(i);
		}
		int[] dp = new int[1 << (M / 2)];
		Arrays.fill(dp, -1);
		for (int i = 0; i < dp.length; ++ i) {
			int mask = 0;
			for (int j = 0; j < M / 2; ++ j) {
				if (contains(i, 1 << j)) {
					mask |= 3 << (j * 2);
				}
			}
			for (int x = 0; x < n; ++ x) {
				for (int y = 0; y < m; ++ y) {
					int t = f[mask][x][y] + g[x][y];
					if (t != -1 && (dp[i] == -1 || dp[i] > t)) {
						dp[i] = t;
					}
				}
			}
		}
		for (int i = 0; i < dp.length; ++ i) {
			for (int j = 0; j < i; ++ j) {
				if (!contains(i, j)) {
					continue;
				}
				if (dp[j] != -1 && dp[i ^ j] != -1) {
					int t = dp[j] + dp[i ^ j];
					if (dp[i] == -1 || dp[i] > t) {
						dp[i] = t;
					}
				}
			}
		}
		return dp[dp.length - 1];
	}

	static class Node {
		int cost;
		int x;
		int y;

		Node() {}
		Node(int cost, int x, int y) {
			this.cost = cost;
			this.x = x;
			this.y = y;
		}
	}
	public static class NodeCompare implements Comparator<Node> {
		@Override
		public int compare(Node c1, Node c2) {
			return c1.cost - c2.cost;
		}
	};

	Queue<Node> queue = new PriorityQueue<>(new NodeCompare());

	void upd(int x, int y, int cost, int mask) {
		if (f[mask][x][y] == -1 || f[mask][x][y] > cost) {
			f[mask][x][y] = cost;
			queue.offer(new Node(cost, x, y));
		}
	}

	boolean contains(int x, int y) {
		return (x & y) == y;
	}

	int[] dx = {1, -1, 0, 0};
	int[] dy = {0, 0, 1, -1};

	void update(int mask) {
		for (int i = 0; i < mask; ++ i) {
			if (!contains(mask, i)) {
				continue;
			}
			for (int x = 0; x < n; ++ x) {
				for (int y = 0; y < m; ++ y) {
					if (f[i][x][y] != -1 && f[mask ^ i][x][y] != -1) {
						int t = f[i][x][y] + f[mask ^ i][x][y];
						if (f[mask][x][y] == -1 || f[mask][x][y] > t) {
							f[mask][x][y] = t;
						}
					}
				}
			}
		}
		while (!queue.isEmpty()) {
			queue.poll();
		}
		for (int x = 0; x < n; ++ x) {
			for (int y = 0; y < m; ++ y) {
				if (f[mask][x][y] != -1) {
					queue.add(new Node(f[mask][x][y], x, y));
				}
			}
		}
		while (!queue.isEmpty()) {
			Node node = queue.poll();
			int cost = node.cost;
			int x = node.x;
			int y = node.y;
			if (f[mask][x][y] != cost) {
				continue;
			}
			for (int i = 0; i < 4; ++ i) {
				int xx = x + dx[i];
				int yy = y + dy[i];
				if (xx < 0 || xx >= n || yy < 0 || yy >= m) {
					continue;
				}
				int newCost =cost;
				if (g[x][y] != g[xx][yy]) {
					newCost += g[x][y];
				}
				upd(xx, yy, newCost, mask);
			}
		}
	}
}

  

topcoder srm 470 div1

标签:期望   raw   部分   character   bre   update   4.0   oid   max   

原文地址:http://www.cnblogs.com/jianglangcaijin/p/7789094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!