蓝桥算法训练营—普及组—2023年2.5日(Java)

接金币

排序 + 模拟

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {
    
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(in);
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static void main(String[] args) throws Exception{
    	
    	int t = Integer.parseInt(in.readLine());

    	while (t-- > 0) {
    		String s = in.readLine();
            // 测试用例中会有空行,加一下判断
    		if (s.equals("")) s = in.readLine();
    		int n = Integer.parseInt(s);
    		
    		int[][] arr = new int[n][2];
    		for (int i = 0; i < n; i++) {
    			String[] ss = in.readLine().split(" ");
    			arr[i][0] = Integer.parseInt(ss[0]);
    			arr[i][1] = Integer.parseInt(ss[1]);
    		}
    		Arrays.sort(arr, (a, b) -> (a[1] - b[1]));
    		int cur = 0, time = 0;
    		boolean flag = false;
    		for (int i = 0; i < n; i++) {
    			if (Math.abs(cur - arr[i][0]) > arr[i][1] - time) {
    				flag = true;
    				break;
    			}
    			cur = arr[i][0];
    			time = arr[i][1];
    		}
    		if (flag) {
    			out.println("Notabletocatch");
    		} else {
    			out.println("Abletocatch");
    		}
    	}

        out.flush();
        in.close();
    }  
}

又是毕业季I

数论题,详细看洛谷题解

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {
    
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(in);
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static void main(String[] args) throws Exception{
    	
    	String[] ss = in.readLine().split(" ");
    	
    	out.println(Integer.parseInt(ss[0]) / Integer.parseInt(ss[1]));

        out.flush();
        in.close();
    }  
}

删数问题

正难则反,将删除 k 位转换成保留 n - k 位

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {
    
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(in);
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static void main(String[] args) throws Exception{
    	
    	char[] ch = in.readLine().toCharArray();
    	int k = Integer.parseInt(in.readLine());
    	int n = ch.length;
    	StringBuilder sb = new StringBuilder();
    	int st = -1;
    	for (int i = 0; i < n - k; i++) {
    		char c = '9';
    		for (int j = st + 1; j <= k + i; j++) {
    			if (ch[j] < c) {
    				c = ch[j];
    				st = j;
    			}
    		}
    		sb.append(c);
    	}
    	int idx = 0, len = sb.length();
    	// 去除前导0
    	while (idx < len && sb.charAt(idx) == '0') idx++;
    	String ans = sb.substring(idx);
    	// 判断去除前导0后为空字符串的情况
    	if (!ans.equals(""))out.println(ans);
    	else out.print("0");
    	
    	out.flush();
    	in.close();
    }  
}

谈判

贪心

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {
    
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(in);
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static void main(String[] args) throws Exception{
    	
    	int n = Integer.parseInt(in.readLine());
    	
    	PriorityQueue<Long> pq = new PriorityQueue<>();
    	
    	for (int i = 0; i < n; i++) {
    		st.nextToken();
    		pq.offer((long)st.nval);
    	}
    	long ans = 0L;
    	while (pq.size() > 1) {
    		long a = pq.poll(), b = pq.poll();
    		ans += a + b;
    		pq.offer(a + b);
    	}
    	
    	out.println(ans);
    	
    	out.flush();
    	in.close();
    }  
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值