Google KickStart 2019 A

Java代码写算法是真的臃肿!!!
549. 训练

import java.util.*;

class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int T = in.nextInt();

        for (int i = 0; i < T; i++) {
            int N = in.nextInt();
            int P = in.nextInt();
            int[] skills = new int[N+1];
            for (int j = 1; j <= N; j++)
                skills[j] = in.nextInt();
            Arrays.sort(skills);

            int[] accumulation = new int[N+1];
            accumulation[1] = skills[1];
            for (int k = 2; k <= N; k++)
                accumulation[k] = accumulation[k - 1] + skills[k];  //前缀和数组
            int ans = Integer.MAX_VALUE;
            for (int k = P; k <= N ; k++) {
                    ans = Math.min(ans, P * skills[k] - (accumulation[k] - accumulation[k - P]));
            }

            System.out.println("Case #"+(i+1)+": "+ans);
        }

    }
}

550. 包裹

import java.util.*;

public class Main {

    static class Coordinates {
        int x, y;

        public Coordinates(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    public static int[][] dist;

    public static boolean isValid(int i, int j) {
        int m = dist.length, n = dist[0].length;
        return i >= 0 && i < m && j >= 0 && j < n && dist[i][j] == -1;
    }

    public static boolean check(int len, char[][] grid) {

        int m = grid.length, n = grid[0].length;
        Queue<Coordinates> que = new LinkedList<>();
        //初始化dist距离矩阵,将格网中值为'1'的坐标添加进队列,O(m*n)
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1') {
                    dist[i][j] = 0;
                    que.add(new Coordinates(i, j));
                } else
                    dist[i][j] = -1;
            }
        }

        //将路径长度为len以内的格点坐标添加进队列,O(m*n)
        while (!que.isEmpty()) {
            Coordinates tmp = que.poll();
            int x = tmp.x, y = tmp.y;
            if (dist[x][y] == len)
                continue;
            for (int dx = -1; dx <= 1; dx++) {
                for (int dy = -1; dy <= 1; dy++) {
                    if (Math.abs(dx + dy) == 1 && isValid(x + dx, y + dy)) {
                        dist[x + dx][y + dy] = dist[x][y] + 1;
                        que.add(new Coordinates(x + dx, y + dy));
                    }
                }
            }
        }

        //对于没有在队列中的格点坐标得到他们的外包正方形,O(m*n)
        int minSum = Integer.MAX_VALUE, maxSum = Integer.MIN_VALUE, minSub = Integer.MAX_VALUE, maxSub = Integer.MIN_VALUE;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) 
            {
                if (dist[i][j] == '0') 
                {
                    minSum = Math.min(minSum, i + j);
                    minSub = Math.min(minSub, i - j);
                    maxSum = Math.max(maxSum, i + j);
                    maxSub = Math.max(maxSub, i - j);
                }
            }
        }

        if (minSum == Integer.MAX_VALUE)
            return true;

        //寻找合适的坐标使得上面的格点都在len距离以内
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '0') 
                {
                    int sum = Math.max(Math.abs(i + j - minSum), Math.abs(i + j - maxSum));
                    int sub = Math.max(Math.abs(i - j - minSub), Math.abs(i - j - maxSub));
                    if(Math.max(sum,sub)<=len)
                    {
                        return true;
                    }
                        
                }
            }
        }
        return false;

    }

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int T = in.nextInt();

        for (int n = 1; n <= T; n++) 
        {
            int rows = in.nextInt();
            int cols = in.nextInt();
            char[][] grid = new char[rows][cols];
            dist = new int[rows][cols];
            for (int i = 0; i < rows; i++) 
            {
                grid[i] = in.next().toCharArray();
            }
            int l = 0, r = rows + cols;
            while (l < r) 
            {
                int mid = (r - l) / 2 + l;
                if (check(mid,grid))
                    r = mid;
                else
                    l = mid + 1;
            }
            System.out.println("Case #"+n+": "+l);
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值