牛客小白月赛101题解(A-E)(Java)

A:tb的区间问题

        一开始想的是贪心的扔掉头和尾的较小值,直接wa五发,后来想到了正解,用滑动窗口+前缀和。

        

// package 小白月赛101;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import static java.lang.Math.max;

public class Main {

    static BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    public static void main(String[] args) throws Exception
    {
        int n,k;
        String[] sin = cin.readLine().split(" ");
        n = Integer.parseInt(sin[0]);
        k = Integer.parseInt(sin[1]);
        int[] a = new int[n+2];
        sin = cin.readLine().split(" ");
        for (int i = 0; i < n; i++)
        {
            a[i] = Integer.parseInt(sin[i]);
        }
        int l = n - k;
        long[] sum = new long[n+1];
        long ans = -1;
        for (int i = 0; i < l; i++) {
            sum[0] += a[i];
            ans = max(ans,sum[0]);
        }
        for (int i = l; i < n; i++) {
            sum[i - l + 1] = sum[i - l] + a[i] - a[i - l];
            ans = max(ans,sum[i - l + 1]);
        }
        out.printf("%d",ans);
        out.flush();
    }
}

B.tb的字符串问题

        个人认为是全场最简单的题,基本就是经典的“括号匹配”问题的翻版,经典的栈的应用

// package 小白月赛101;

// import 牛客周赛60.C;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Stack;

public class Main {

    static BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    public static void main(String[] args) throws Exception
    {
        int n = Integer.parseInt(cin.readLine());
        String s = cin.readLine();
        long ans = 0;
        Stack<Character>st = new Stack<>();
        for (int i = 0; i < s.length(); i++)
        {
            if(s.charAt(i) == 'f' || s.charAt(i) == 't')
                st.add(s.charAt(i));
            else if(s.charAt(i) == 'b')
            {
                if (st.empty())
                    continue;;
                if(st.peek() == 't')
                {
                    ans ++;
                    st.pop();
                }
                else
                    st.clear();
            }
            else if(s.charAt(i) == 'c')
            {
                if (st.empty())
                    continue;
                if(st.peek() == 'f')
                {
                    ans ++;
                    st.pop();
                }
                else
                    st.clear();
            }
            else
                st.clear();
        }
        out.printf("%d",n - ans * 2 );
        out.flush();
    }
}

C.tb的路径问题

结论题,观察可以发现,任何一个偶数n,除2以外,n和n-2的最大公约数一定是2,而离原点最近的2就是(2,2),所以任何一个偶数都可以先走到(2,2),然后走到(n,n-2),最后走到(n,n),即最多为4步,而任何一个奇数都可以先走到(n-1,n-1),即最多六步

然后特判一下1,2,3即可

// package 小白月赛101;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {

    static BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
    public static void main(String[] args) throws Exception {
        int n = Integer.parseInt(cin.readLine());
        if(n % 2 == 0)
        {
            if(n == 2)
                out.print(2);
            else
                out.print(4);
        }
        else
        {
            if(n == 1)out.print(0);
            else if(n == 3)
            {
                out.print(4);
            }
            else
                out.print(6);
        }
        out.flush();
    }
}

D:tb的平方问题

观察到n很小,而查询次数q非常大,一般可以用预处理的思路。

用ans[i]表示第i个数的答案,sum[i][j]表示从i开始长度为j的数组的和,若sum[i][j]为完全平方数,那么说明从i到i+j-1都有一个新的子数组满足结果,把ans[i]到ans[i+j-1]都加一。

// package 小白月赛101;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import static java.lang.Math.sqrt;

public class Main {

    static BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
    public static boolean is(int n)
    {
        int x = (int)sqrt(n);
        return  x*x == n;
    }
    public static void main(String[] args) throws Exception
     {
         String[] s = cin.readLine().split(" ");
         int n = Integer.parseInt(s[0]),q = Integer.parseInt(s[1]);
         int[] a = new int[n+2],sums[] = new int[n+2][n+2];
         s = cin.readLine().split(" ");
         for (int i = 1; i <= n; i++) {
             a[i] = Integer.parseInt(s[i - 1]);
             sums[i][1] = a[i];
         }
         int ans[] = new int[n+2];
         for(int i = 1 ;i <= n;i ++)
         {
             for(int j = 1;j <= n - i + 1;j ++)
             {
                 sums[i][j] = sums[i][j-1] + a[i + j - 1];
                 if(is(sums[i][j]))
                 {
                     for (int k = 0; k <= j - 1; k++) {
                         ans[i+k] ++;
                     }
                 }
             }
         }
        while(q > 0)
        {
            int x = Integer.parseInt(cin.readLine());
            out.printf("%d\n",ans[x]);

            q --;
        }

        out.flush();
    }
}

E:tb的数数问题

观察到,坏数的倍数一定是坏数,(坏数即为不是好数的数),而好数一定在a集合中,那么可以用类似埃式筛求质数的方法筛除坏数。

// package 小白月赛101;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static java.lang.Integer.MIN_VALUE;
import static java.lang.Integer.max;

public class Main{

    static BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    public static void main(String[] args) throws Exception {

        int n = Integer.parseInt(cin.readLine());
        String[] s = cin.readLine().split(" ");
        Set<Integer> st = new HashSet<>();
        int maxn = MIN_VALUE;
        for (int i = 0; i < n; i++) {
            int x = Integer.parseInt(s[i]);
            st.add(x);
            maxn = max(maxn,x);
        }
        int ans = 0;
        boolean[] sieve = new boolean[maxn + 1];
        for (int i = 1; i <= maxn; i++) {
            if(sieve[i])continue;
            if(!st.contains(i)) {
                for (int j = i; j <= maxn; j += i) {
                    sieve[j] = true;
                }
            }
            if (!sieve[i] && st.contains(i))ans  ++;
        }
        out.print(ans);
        out.flush();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值