牛客笔试,牛客.四个选项(dfs巨难)牛客.接雨水动态规划单调栈解法牛客.栈和排序牛客.加减

目录

牛客.四个选项(dfs巨难)

牛客.接雨水

动态规划

单调栈解法

牛客.栈和排序

牛客.加减


牛客.四个选项(dfs巨难)

 

刚开始我是想着用数学,Cxx去解决,但是他的还有其余条件,就没有办法解决,所以就枚举 ,递归的数据量不大时候,是推荐使用的

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
   static int []a;
  static int[]path=new int[13];//记录当前路径填写了哪些选项
   static int sum;
   static boolean[][]same=new boolean[13][13];
   public static void  dfs(int pos){
   if(pos>12){
//12个填写满是1种,我们需要写完12种
    sum++;
    return ;
   }
   for(int i=1;i<=4;i++){
   if(a[i]==0){
    //剪枝,看看有没有剩余的次数
continue;}
   if(!isSame(pos,i))continue; //判断是否需要相同的题目,填写了相同的选项
   //在pos位置填写了i选项,当我们path[pos]如果我们下次循环,会覆盖掉这次的经历。
     path[pos]=i;
     a[i]--;
     dfs(pos+1);
     a[i]++;
     }
   } 
   //pos位置,填写cur的时候
  static boolean isSame(int pos,int cur){
    for(int i=1;i<pos;i++){
        //pos和i必须要是相同的,path和cur要填的不同是不合法的
        if(same[pos][i]==true&&path[i]!=cur){
         return false;
        }
    }
    //当前位置填写cur合法
     return true;
   }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        a=new int[6];
        for(int i=1;i<=5;i++){
         a[i]=in.nextInt();
        }
       while(a[5]!=0){
        int x=in.nextInt();
        int y=in.nextInt();
        same[x][y]=same[y][x]=true;
        a[5]--;
       }
        dfs(1); 
        System.out.println(sum);
    }
}

牛客.接雨水

动态规划

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * max water
     * @param arr int整型一维数组 the array
     * @return long长整型
     */
    public long maxWater (int[] arr) {
        int n=arr.length;
        long sum=0;
      
        int[]left=new int[n];
        int[]right=new int[n];
        left[0]=arr[0];
        //0-i
        for(int i=1;i<n;i++){
            left[i]=Math.max(left[i-1],arr[i]);
        }
        //预处理右侧 i到 n
        right[n-1]=arr[n-1];
        for(int i=n-2;i>=0;i--){
            //后面位置的最大值,和当前位置的最大值
            right[i]=Math.max(right[i+1],arr[i]);
        }

       
        for(int i=1;i<n-1;i++){
           sum+=Math.min(left[i],right[i])-arr[i];
        }
        
        return sum;
    }
}

单调栈解法

class Solution {
    public int trap(int[] height) {
        Stack<Integer> stack = new Stack<>();
        int res = 0;
        // 遍历每个柱体
        for (int i = 0; i < height.length; i++) {
           while (!stack.isEmpty() && height[stack.peek()] < height[i]) {
                int mid = stack.pop();
                // 如果栈顶元素一直相等,那么全都pop出去,只留第一个。
                while (!stack.isEmpty() && height[stack.peek()] == height[mid]) {
                    stack.pop();
                }
                if (!stack.isEmpty()) {
                    // stack.peek()是此次接住的雨水的左边界的位置,右边界是当前的柱体,即i。
                    // Math.min(height[stack.peek()], height[i]) 是左右柱子高度的min,减去height[mid]就是雨水的高度。
                    // i - stack.peek() - 1 是雨水的宽度。
                    res += (Math.min(height[stack.peek()], height[i]) - height[mid]) * (i - stack.peek() - 1);
                }
            }
            stack.push(i);
        }
        return res;
    }
}

牛客.栈和排序

好想不好写,蛮考验代码功力的

然后有一些,还在栈里面,就等栈空不空,接着搞

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 栈排序
     * @param a int整型一维数组 描述入栈顺序
     * @return int整型一维数组
     */
    public int[] solve (int[] a) {
        int n = a.length;
        Stack<Integer>stack = new Stack<>();
        ArrayList<Integer>ret = new  ArrayList<>();
        int count = n;
        int []hash = new int[n + 1];
        for (int i = 0; i < n; i++) {
            stack.add(a[i]);
            hash[a[i]]++;
            if (a[i] == count && (stack.isEmpty() || stack.peek() < a[i])) {
                stack.pop();
                ret.add(a[i]);
                hash[count]++;
                count--;
            }
            while (hash[count] != 0) {
                count--;
            }
            while (!stack.isEmpty() && stack.peek() > count) {
                ret.add(stack.pop());
            }
        }

        while (!stack.isEmpty()) {
            ret.add(stack.pop());
        }
        int []p = new int[n];
        for (int i = 0; i < n; i++) {
            p[i] = ret.get(i);
        }
        return p;
    }
    }

牛客.加减

如何求一个区间的最小代价

加入选择a2与a3比较

a3的花费是 整段+a2——a4

a2的花费是 整段+a2——a4+a3-a2所以最中间的点有优势

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        long k = in.nextLong();
        long []a = new long[n+1];
        long []sum = new long[n+1];
        int count=0;
        for (int i = 1; i <= n; i++) {
            a[i] = in.nextLong();
        }
        Arrays.sort(a);
        for (int i = 1; i <= n; i++) {
            sum[i]=sum[i-1]+a[i];
        }
            long cost=0;
            int left = 1;
            int right = 1; 
             while(right<=n){      
            int mid=(left+right)/2;    
           cost=(mid-left)*a[mid]-(sum[mid-1]-sum[left-1])+(sum[right]-sum[mid])-(right-mid)*a[mid];     
             while(cost>k){
             left++;
             mid=(left+right)/2; 
             cost=(mid-left)*a[mid]-(sum[mid-1]-sum[left-1])+(sum[right]-sum[mid])-(right-mid)*a[mid];     
             }
             
                 count=Math.max(count,right-left+1);
                 right++;
            }
            
            System.out.print(count);


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

狗哥不是甜妹

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

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

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

打赏作者

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

抵扣说明:

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

余额充值