SRM 721 DIV2
2017-10-13 16:31
232 查看
Single
Round Match 721 Round 1 - Division II, Level One
代码如下:
Single
Round Match 721 Round 1 - Division II, Level Two
代码如下:
Single
Round Match 721 Round 1 - Division II, Level Three
import java.util.*;
public class ApocalypseEasy {
private static List<Integer>[] adjList;
private static Set<Integer> set = new HashSet<>();
private static Set<Integer> tokenSet = new HashSet<>();
private static boolean flag = false;
public int maximalSurvival(int[] p, int[] position, int t)
{
int n = p.length;
adjList = new List[n + 1];
for (int i = 0; i < n + 1; i++) {
adjList[i] = new ArrayList<Integer>();
tokenSet.add(i);
}
for (int i : position)
{
tokenSet.remove(i);
}
set.clear();
for (int i : position)
{
set.add(i);
}
for (int i = 0; i < n; i++)
{
adjList[p[i]].add(i + 1);
adjList[i + 1].add(p[i]);
}
int ans = 0;
for (int u : position)
{
flag = false;
boolean success = dfs(u, -1, t);
if (success) ans++;
}
return ans;
}
private boolean dfs(int u, int p, int t)
{
if (t <= 0) {
if (tokenSet.contains(u)) return true;
return false;
}
for (Integer v : adjList[u]) {
if (v == p) continue;
if (set.contains(v)) {
if(dfs(v, u, --t)) return true;
} else if (tokenSet.contains(v)) {
if (dfs(v, u, --t)) {
if (!flag){
tokenSet.remove(v);
flag = true;
}
return true;
}
}
}
if (tokenSet.contains(u)) return true;
return false;
}
}
Round Match 721 Round 1 - Division II, Level One
代码如下:
public class FlightDataRecorder { public double getDistance(int[] heading, int[] distance) { double startx = 0, starty = 0; double dstx = 0, dsty = 0; for (int i = 0; i < heading.length; i++) { double radian = Math.PI * heading[i] / 180; dstx += distance[i] * Math.cos(radian); dsty += distance[i] * Math.sin(radian); } return Math.sqrt(Math.pow(startx - dstx, 2) + Math.pow(starty - dsty, 2)); } }
Single
Round Match 721 Round 1 - Division II, Level Two
代码如下:
public class RememberWordsEasy { public String isPossible(int d1, int d2, int w1, int w2) { int mod = w1 % d1; int leftMin = w1 / d1; if (mod != 0) leftMin++; int leftMax = 0; if (w1 >=d1) leftMax = (2 * w1 / d1 + d1 - 1) / 2; else leftMax = (w1 + 1) / 2; int rightMin = (2 * w2 / d2 + 1 - d2) / 2; if (rightMin < 0) rightMin = 0; int rightMax = w2 / d2; //System.out.println("leftMin:" + leftMin + " leftMax:" + leftMax + " rightMin:" + rightMin + " rigthMax:" + rightMax); if ((rightMin>= leftMin && rightMin <= leftMax) || (rightMax>= leftMin && rightMax <= leftMax) || (rightMin - leftMin >= 0 && rightMin - leftMin <= 1) || (rightMax - leftMin >= 0 && rightMax - leftMin <= 1) || (rightMax - leftMax >= 0 && rightMax - leftMax <= 1) || (rightMin - leftMax >= 0 && rightMin - leftMax <= 1)) return "Possible"; return "Impossible"; } }
Single
Round Match 721 Round 1 - Division II, Level Three
import java.util.*;
public class ApocalypseEasy {
private static List<Integer>[] adjList;
private static Set<Integer> set = new HashSet<>();
private static Set<Integer> tokenSet = new HashSet<>();
private static boolean flag = false;
public int maximalSurvival(int[] p, int[] position, int t)
{
int n = p.length;
adjList = new List[n + 1];
for (int i = 0; i < n + 1; i++) {
adjList[i] = new ArrayList<Integer>();
tokenSet.add(i);
}
for (int i : position)
{
tokenSet.remove(i);
}
set.clear();
for (int i : position)
{
set.add(i);
}
for (int i = 0; i < n; i++)
{
adjList[p[i]].add(i + 1);
adjList[i + 1].add(p[i]);
}
int ans = 0;
for (int u : position)
{
flag = false;
boolean success = dfs(u, -1, t);
if (success) ans++;
}
return ans;
}
private boolean dfs(int u, int p, int t)
{
if (t <= 0) {
if (tokenSet.contains(u)) return true;
return false;
}
for (Integer v : adjList[u]) {
if (v == p) continue;
if (set.contains(v)) {
if(dfs(v, u, --t)) return true;
} else if (tokenSet.contains(v)) {
if (dfs(v, u, --t)) {
if (!flag){
tokenSet.remove(v);
flag = true;
}
return true;
}
}
}
if (tokenSet.contains(u)) return true;
return false;
}
}
相关文章推荐
- SRM 450 div1(practice)
- SRM 676 div1 hard
- SRM 475 DIV1 900
- topcoder srm 315 div1
- SRM 588 DIV1
- Topcoder SRM 660 DIV1 500 Privateparty(数学)
- topcoder SRM 513 DIV2
- 【TopCoder】SRM 680 DIV 2
- TopCoder——SRM 519 DIV 2
- TopCoder SRM 590 Div2 第3题
- SRM 387 DIV2 [250]
- SRM 510 DIV2 1000 TheLuckyBasesDivTwo
- SRM 400 DIV2 [500]
- QuickSums - SRM 197 Div 2 dp+矩阵+暴力
- TopCoder SRM 664 Div2 Level Three
- SRM 533 DIV 2
- Topcoder SRM 452 div 2 level 3 HamiltonPath
- SRM 423 div1 500p
- Topcoder SRM 597 DIV 1
- Topcoder SRM536 div2 1000pt