您的位置:首页 > 其它

SRM 721 DIV2

2017-10-13 16:31 232 查看
  Single
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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: