笔试狂刷--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);
    }
}
  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值