牛客编程巅峰赛S2第7场 - 青铜&白银&黄金 (升砖二了)

2022-07-26,,,

牛客编程巅峰赛S2第7场 - 青铜&白银&黄金

今天又是拉跨操作, 又是一题选手

第二题踩坑: 子序列 与 子串 ( 二分

战绩

第一题: 牛牛爱喝酒

说什么, 做什么, 模拟就可以了

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 返回牛牛能喝的最多的酒
     * @param m int整型 酒单价
     * @param n int整型 牛牛的现金
     * @return int整型
     */
    public int countWine (int m, int n) {
        // write code here
        int k = n / m;
        int ans = k;
        int a = k, b = k;
        while(a >= 2 || b >= 4){
            int temp = 0;
            temp += a / 2;
            temp += b / 4;
            a = a % 2 + temp;
            b = b % 4 + temp;
            ans += temp;
        }
        
        return ans;
    }
}


第二题: 踩坑题 牛牛的子序列

最小里面找最大, 最大里面找最小 >> 想到二分

  1. 二分查找 (通用的解法)
  2. 双指针(O(N))

二分

二分查找模板

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 
     * @param x string字符串 
     * @return int整型
     */
    public int Maximumlength (String x) {
        // write code here
        int n = x.length();
        int left = 0, right = n, ans = 0;
        
        while(left <= right){
            int mid = (left + right) / 2;
            if(check(mid, x)){
                ans = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        
        return ans * 3;
    }
    
    public boolean check(int mid, String x){
        int n = x.length();
        int a = 0, b = 0, c = 0;
        for(int i = 0; i < n ; i++){
            if(a < mid){
                if(x.charAt(i) == 'a'){
                    a++;
                }
            } else if(b < mid){
                if(x.charAt(i) == 'b'){
                    b++;
                }
            } else if(x.charAt(i) == 'c'){
                c++;
            }
        }
        
        return c >= mid;
    }
    
}


第三题: 分贝壳的游戏

这就是博弈题嘛??

猜结论 ?

抄来的代码

AC Code


import java.util.*;


public class Solution {
    /**
     * 
     * @param n int整型 
     * @param p int整型 
     * @param q int整型 
     * @return int整型
     */
    public int Gameresults (int n, int p, int q) {
        // write code here
        if(p >= n) return 1;
        if(p == q){
            if(n % (p + 1) == 0) return -1;
            else return 1;
        } else {
            if(p > q) return 1;
            else return -1;
        }
    }
}

本文地址:https://blog.csdn.net/qq_43765535/article/details/110886301

《牛客编程巅峰赛S2第7场 - 青铜&白银&黄金 (升砖二了).doc》

下载本文的Word格式文档,以方便收藏与打印。