SRM 721 DIV2

  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;
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值