Contest 154

2019-09-16 17:22:28

总体感受:这次比赛的模版题也太多了吧,两条模版题没有想出来。总的来说,还是自己的刷题量还是严重的不够。

注意点

1)提升刷题量和覆盖率非常重要;

2)在碰到大数处理的时候,还是会出现一些问题,有两个路径吧,一个是使用long来测试一下;二是使用bigInteger。

  • 1191. K-Concatenation Maximum Sum

问题描述:

 

问题求解:

如果没有repeat操作,本题就是一个典型的dp问题,可以在O(n)内解决。事实上,如果进行了repeat依然可以在O(k * n)解决,但是本题的数据规模非常大,如果直接去计算那么必然会TLE。

这种repeat计算最大子数组和有个经典的解法,叫做 Kadens Algorithm 。使用该算法可以在O(n)的时间复杂度完成求解。

这个算法的核心思路是,如果sum of array > 0,那么中间就可以有k - 2个array,最后前后计算一下prefix和suffix即可;如果sum of array <= 0,那么中间必然不存在一个完整的array,那么最后的解就会出现array和横跨两个array的解中。

当然,本题最恶心的地方不只是这个算法本身,这里还有个大数处理的问题,本题采用的策略是使用long来保存数据,因为long的最长能保存10 ^ 18,本题中不会爆掉。

    public int kConcatenationMaxSum(int[] arr, int k) {
        long mod = (long) (1e9 + 7);
        long max_num = helper(arr);
        if (k == 1) return (int)max_num;
        long sum = 0;
        for (int num : arr) sum += num;
        long suffix = calcsuffix(arr);
        long prefix = calcprefix(arr);
        if (sum > 0) {
            return (int) (Math.max((suffix % mod + prefix % mod + (k - 2) * sum % mod) % mod, max_num));
        }
        else {
            return (int) (Math.max((suffix % mod + prefix % mod) % mod, max_num));
        }
    }
    
    private long helper(int[] nums) {
        long mod = (int)(1e9 + 7); 
        long res = nums[0];
        long dp = nums[0];
        for (int i = 1; i < nums.length; i++) {
            dp = Math.max(nums[i], nums[i] + dp);
            res = Math.max(res, dp);
        }
        return res < 0 ? 0 : res % mod;
    }
    
    private long calcsuffix(int[] nums) {
        long res = Integer.MIN_VALUE;
        long curSum = 0;
        for (int num : nums) {
            curSum += num;
            res = Math.max(res, curSum);
        }
        return res;
    }
    
    private long calcprefix(int[] nums) {
        long res = Integer.MIN_VALUE;
        long curSum = 0;
        for (int i = nums.length - 1; i >= 0; i--) {
            curSum += nums[i];
            res = Math.max(res, curSum );
        }
        return res;
    }
  • 1192. Critical Connections in a Network

问题描述:

问题求解:

本题也是一条模版题,是一条经典的求割边的问题,该问题有Tarjan算法,可以在O(n + e)的时间复杂度求解。

Tarjan算法的核心思路是维护两个数组discovery[],low[]。disc[]数组里存放访问节点的时间戳,low[]数组里存放与节点邻接的(不包含父节点)最小时间戳的节点。

每次比较父节点和子节点,如果子节点没有访问到时间戳小的节点,那么这两个节点之间必然有一条割边。

    int timestamp = 1;
    public List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) {
        int[] disc = new int[n];
        int[] low = new int[n];
        List<List<Integer>> res = new ArrayList<>();
        List<Integer>[] graph = new ArrayList[n];
        for (int i = 0; i < n; i++) graph[i] = new ArrayList<>();
        for (int i = 0; i < connections.size(); i++) {
            int u = connections.get(i).get(0);
            int v = connections.get(i).get(1);
            graph[u].add(v);
            graph[v].add(u);
        }
        Arrays.fill(disc, -1);
        helper(graph, 0, disc, low, -1, res);
        return res;
    }
    
    private void helper(List<Integer>[] graph, int node, int[] disc, int[] low, int prev, List<List<Integer>> res) {
        disc[node] = timestamp;
        low[node] = timestamp;
        timestamp += 1;
        for (int u : graph[node]) {
            if (u == prev) continue;
            if (disc[u] == -1) {
                helper(graph, u, disc, low, node, res);
                if (low[u] > disc[node]) {
                    res.add(Arrays.asList(u, node));
                }
            }
            low[node] = Math.min(low[node], low[u]);   
        }
    

  

猜你喜欢

转载自www.cnblogs.com/TIMHY/p/11528460.html
154
今日推荐