算法进阶指南-基础算法-总结与练习(5~8)

5.防线

题目链接
在这里插入图片描述

import java.util.Scanner;

/**
 * @Auther: jahup
 * @blog: https://blog.csdn.net/jahup
 */
public class Main {
    static way v[] = new way[200010];
    static int n;

    public static void main(String[] args) {
        int t;
        Scanner sc = new Scanner(System.in);

        t = sc.nextInt();

        while (t-- > 0) {
            n = sc.nextInt();
            int a, b, c;
            int l = 0, r = 0;
            for (int i = 1; i <= n; i++) {
                a = sc.nextInt();
                b = sc.nextInt();
                c = sc.nextInt();
                v[i] = new way(a, b, c);
                r = Math.max(r, b);
            }

            while (l < r) {


                int mid = l + r >> 1;
                if ((getall(mid) & 1) > 0) r = mid;
                else l = mid + 1;

            }
            long sum=getall(l)-getall(l-1);
            if((sum%2)>0)
            System.out.println(l+" "+sum);
            else
                System.out.println("There's no weakness.");


        }
    }

    static long getall(int x) {
        long res = 0;
        for (int i = 1; i <= n; i++) {
            if (v[i].s <=x)
                res += (Math.min(v[i].e, x) - v[i].s) / v[i].d+1;


        }
        return res;

    }
}


class way {

    int s, e, d;

    public way(int s, int e, int d) {
        this.s = s;
        this.e = e;
        this.d = d;
    }
}

6.Corral the Cows

题目链接
在这里插入图片描述

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Auther: jahup
 * @blog: https://blog.csdn.net/jahup
 */
public class Main {
    static final int N = 10000 + 10;
    static int n, c;
    static int maps[][] = new int[N][N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        c = sc.nextInt();
        n = sc.nextInt();
        int a, b;
        for (int i = 0; i < N; i++)
            Arrays.fill(maps[i], 0);
        int max_x = 0, max_y = 0;
        while (n-- > 0) {
            a = sc.nextInt();
            b = sc.nextInt();
            maps[a + 1][b + 1]++;
            max_x = Math.max(max_x, a + 1);
            max_y = Math.max(max_y, b + 1);


        }
        int all = Math.max(max_x, max_y);
        for (int i = 1; i <= all; i++)
            for (int j = 1; j <= all; j++) {

                maps[i][j] = maps[i][j] + maps[i][j - 1] + maps[i - 1][j] - maps[i - 1][j - 1];
            }

        int res = 99999;

        for (int i = 1; i <= all; i++)
            for (int j = 1; j <= all && res > 1; j++) {
                for (int k = 1; k <= res; k++) {
                    if (i - k < 0 || j - k < 0)
                        break;
                    if (res == 1)
                        break;
                    if ((maps[i][j] - maps[i - k][j] - maps[i][j - k] + maps[i - k][j - k]) >= c) {
                        res = k;

                    }
                }
            }
        System.out.println(res);
    }


}

7.Soliders

题目链接
在这里插入图片描述

import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// @author:jahup
public class Main {


    public static void main( String args[] ){
        int n;
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        final int N=10000+10;
        int x[]=new int[N];
        int y[]=new int[N];
        for(int i=1;i<=n;i++)
        {
            x[i]=sc.nextInt();
            y[i]=sc.nextInt();

        }
        Arrays.sort(x,1,n+1);
        Arrays.sort(y,1,n+1);
        int cost=0;
        if(n%2==0){
            int mid_y=y[n/2];
            for(int i=1;i<=n;i++)
                cost+=Math.abs(y[i]-mid_y);

            for(int i=1;i<=n;i++)
                x[i]=(x[i]-i);
            Arrays.sort(x,1,n+1);
            int mid_x=x[n/2];
            for(int i=1;i<=n;i++)
                cost+=Math.abs(x[i]-mid_x);


        }
        else {
            int mid_y=y[n/2+1];
            for(int i=1;i<=n;i++)
                cost+=Math.abs(y[i]-mid_y);

            for(int i=1;i<=n;i++)
                x[i]=(x[i]-i);
            Arrays.sort(x,1,n+1);
            int mid_x=x[n/2+1];
            for(int i=1;i<=n;i++)
                cost+=Math.abs(x[i]-mid_x);
        }
        System.out.println(cost);
    }
}

8. 糖果传递

题目链接
在这里插入图片描述

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Auther: jahup
 * @blog: https://blog.csdn.net/jahup
 */
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = 1000000+10;
        long a[]=new long[N];
        int n;
        n=sc.nextInt();
        long all=0;
        for(int i=1;i<=n;i++) {
            a[i] = sc.nextLong();
            all += a[i];
        }
        long mid=all/n;
        for (int i = n; i > 1; i -- )
        {
            a[i] = a[i] - mid + a[i + 1];
        }
        a[1] = 0;
        Arrays.sort(a,1,n+1);
        long res=0;
        for (int i = 1; i <= n; i ++ ) res += Math.abs(a[i] - a[(n + 1) / 2]);
        System.out.println(res);


    }
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值