4月10滴滴的笔经

1、计算操作系统的调度算法时间。给你n行数字,每行两个数字,第一个表示当前任务的等待时间,第二个是任务执行的时间。让你计算程序执行的最短时间。

其实是一个贪心算法。直接对输入的数组进行排序(按照等待时间的短进行排序,然后如果等待时间相等的时候,我们就比较执行时间的长度)。然后再扫描一次数组就行了。
n=2
5 1
2 4
输出结果:7 (先执行任务2,最后的时间是6,但是这个时候任务1又可以执行了,然后直接执行,6+1=7)

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Node[] a = new Node[n];
        for(int i = 0 ;i<n;i++){
            a[i] = new Node();
            a[i].waittime = sc.nextLong();
            a[i].dotime = sc.nextInt();
        }
        Arrays.sort(a,(A,B)->{
            if(A.waittime<B.waittime){
                return -1;
            }else if(A.waittime==B.waittime){
                if(A.dotime<B.dotime){
                    return -1;
                }
            }else if(A.waittime>B.waittime){
                return 1;
            }
            return 0;
        });
        long time=0;
        long cutterntime = a[0].waittime;
        for(int i = 0;i<n;i++){
            if(cutterntime<a[i].waittime){
                cutterntime = a[i].waittime;
            }
            time = cutterntime+a[i].dotime;
            cutterntime = cutterntime+a[i].dotime;
        }
        System.out.println(time);
    }
    static class Node{
        public long waittime;
        public long dotime;

        public Node() {

        }

        @Override
        public String toString() {
            return "Node{" +
                    "waittime=" + waittime +
                    ", dotime=" + dotime +
                    '}';
        }
    }
}

2、种树问题,给你两个数,一个n表示要种的数数量,另一个数x是树之间的差值,然后再给出n棵树的高度,你要使用魔法,将这些树变成等差数列。前提是要使用的魔法次数最少。比如

5 2
1 3 1 3 5
结果为3(改变最后三棵树就行了)
这里有一个问题,x有可能是负数,并且树的高度不能小于1。

import java.util.Scanner;

public class Main2 {
    public static void main(String[] args){
        Scanner sc  = new Scanner(System.in);
        int n = sc.nextInt();
        int x = sc.nextInt();
        long[] a = new long[n];
        for(int i = 0 ;i<n;i++){
            a[i] = sc.nextLong();
        }
        //寻找最长连续等差数列。
        int[] dp = new int[n];
        for(int i = 0 ;i<n;i++){
            dp[i] = 1;
        }
        for(int i =1;i<n;i++){
            if(a[i]-a[i-1]==x){
                dp[i] = dp[i-1]+1;
            }
        }
        //如果开始的下标不一定相等的话就要进行一定的处理。
        //以谁结尾的开头是什么,然后进行相应的判断就行了
        int count = Integer.MAX_VALUE;
        if(x>0) {    //差值为正数的时候
            for (int i = 0; i < n; i++) {
                if (a[i] - (((i + 1) - dp[i]) * x) >= 1 + (i + 1 - dp[i]) * x) {
                    count = Math.min(n - dp[i], count);
                }
            }
        }
        else{     //差值为负数的时候
            for(int i= 0 ;i<n;i++ ){
                if(a[i]>=(1+((-x)*(n-i-1)))){    //
                    count = Math.min(n-dp[i],count);
                }
            }
        }
        if(count==Integer.MAX_VALUE){
            System.out.println(0);
        }else {
            System.out.println(count);
        }
    }
}
//后面这个不知道对不对的。
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务