笔试狂刷--Day4(滑动窗口+贪心)

大家好,我是LvZi,今天带来笔试狂刷--Day4
在这里插入图片描述

一.简写单词

1.题目链接:简写单词
2.题目分析:

一个简单的模拟

3.代码实现:

1.先整体读取,再处理

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        String[] str = s.split(" ");// 将字符串按照空格进行分割
        StringBuffer sb = new StringBuffer();
        for(String tmp : str) {
            sb.append(tmp.toUpperCase().charAt(0));
        }

        System.out.print(sb.toString());
    }
}

2.一个字符串一个字符串读取

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while(in.hasNext()) {// 遇到空格就停止
            char ch = in.next().charAt(0);
            if(ch >= 'a' && ch <= 'z') System.out.print((char)(ch - 32));
            else System.out.print(ch);
        }
    }
}

二.dd爱框框(滑动窗⼝)

1.题目链接:dd爱框框(滑动窗⼝)

2.分析:

简单的滑动窗口

3.代码实现:

import java.util.Scanner;
import java.util.*;
import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    
    public static void main(String[] args) throws IOException{
        int n = in.nextInt(), x = in.nextInt();
        int[] a = new int[n + 1];
        for(int i = 1; i <= n; i++) a[i] = in.nextInt();
        
        int sum = 0;
        int[] ret = new int[2];
        ret[1] = 0x3f3f3f3f;
        for(int l = 1, r = 1; r <= n; r++){
            sum += a[r];// 进窗口
            
            while(sum >= x){// 判断
                if(r - l < ret[1] - ret[0]) {
                    ret[0] = l; ret[1] = r;// 更新结果
                }

                sum -= a[l++];// 出窗口
            }
        }
        
        System.out.println(ret[0] +" " + ret[1]);
    }
}

// 本题由于输入数据过多 正常的输入会超时  需要使用快读
class Read {
    StringTokenizer st = new StringTokenizer("");
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    
    String next() throws IOException {
        while(!st.hasMoreTokens()) {
            st = new StringTokenizer(bf.readLine());
        }
        
        return st.nextToken();
    }
    
    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
}

三.除2!(贪⼼ + 堆)

1.题目链接:.除2!
2.题目分析:

使用贪心策略,每次减去偶数的最大值

代码:

import java.util.Scanner;
import java.util.PriorityQueue;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), k = in.nextInt();
        PriorityQueue<Integer> q = new PriorityQueue<>((o1,o2) -> o2 - o1);// 调整为大根堆

        long sum  = 0, x;
        
        // 将所有的偶数添加进队列
        for(int i = 0; i < n; i++) {
            x = in.nextInt();
            sum += x;
            if(x % 2 == 0) q.add((int)x);
        }

        while(!q.isEmpty() && k-- != 0) {
            long t = q.poll() / 2;
            sum -= t;
            if(t % 2 == 0) {// 如果删除的数字/2 仍然是偶数 仍要添加进入队列
                q.add((int)t);
            }
        }
        
        System.out.println(sum);
    }
}

相关推荐

  1. 笔记 - 滑动窗口

    2024-04-23 06:38:01       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-23 06:38:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-23 06:38:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-23 06:38:01       87 阅读
  4. Python语言-面向对象

    2024-04-23 06:38:01       96 阅读

热门阅读

  1. Selenium(一):八大元素定位

    2024-04-23 06:38:01       32 阅读
  2. LeetCode 438.找到字符串中所有字母异位词

    2024-04-23 06:38:01       39 阅读
  3. P4 vscode 插件

    2024-04-23 06:38:01       35 阅读
  4. 开发一款游戏,需要注意哪些问题?

    2024-04-23 06:38:01       45 阅读
  5. DBA搞钱之路

    2024-04-23 06:38:01       40 阅读
  6. Unity实现一个简单的状态机Demo

    2024-04-23 06:38:01       33 阅读
  7. 为什么删除node_modules文件夹很慢

    2024-04-23 06:38:01       32 阅读