第十四届蓝桥杯三月真题刷题训练——第 18 天

排列字母

原题链接

排列字母

问题描述

小蓝要把一个字符串中的字母按其在字母表中的顺序排列。
例如,LANQIAO 排列后为 AAILNOQ。
又如,GOODGOODSTUDYDAYDAYUP 排列后为 AADDDDDGGOOOOPSTUUYYY。
请问对于以下字符串,排列之后字符串是什么?WHERETHEREISAWILLTHEREISAWAY

解题思路

排序

参考代码

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

public class Main {

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

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

    	char[] ch = "WHERETHEREISAWILLTHEREISAWAY".toCharArray();
    	Arrays.sort(ch);
    	for (int i = 0; i < ch.length; i++) out.print(ch[i]);

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

GCD

原题链接

GCD

问题描述

todo:这里复制题目描述

解题思路

参考题解
百度百科 - 更相减损术

根据上述定理 gcd(a, b) = gcd(a, b - a) (题目保证 b > a)
可以得到 gcd(a + k, b + k) = gcd(a + k, b + k - (a + k)) = gcd(a + k, b - a)
设 c = b - c 可以得到 gcd(a + k, c)
a + kc 的最大公约数是多少呢?答案是 c
如果 a + k < c ,则最大公约数为 a + k;如果 a + k >= c,则最大公约数为 c
(a + k) % c = 0 时,最大公约数为 c
如果 a % c = 0,则 k = 0,否则 k = (a / c + 1) * c - a

参考代码

import java.io.*;

public class Main {

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static void main(String[] args) throws Exception {
    	String[] s = in.readLine().split(" ");
    	long a = Long.parseLong(s[0]), b = Long.parseLong(s[1]);
    	long c = b - a;
    	if (a % c == 0) {
    		out.println(0);
    	} else {
    		out.println((a / c + 1) * c - a);
    	}

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

选数异或

原题链接

选数异或

问题描述

todo:这里复制题目描述

解题思路

异或运算满足:a ^ a = 0
a ^ b = x 可以转换为 a ^ b ^ b = x ^ b,即 a = x ^ b
在遍历这个数组的时候,当前的遍历到了 arr[i] ,借助哈希表可以很快的找到 x ^ arr[i] 是否在之前出现过(这里哈希表存放的是数 x 最后一次出现的位置,这样可以有更多的区间能覆盖到)

参考代码

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

public class Main {

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    
    static int N = (int)1e5 + 10, n, m, x;
    static int[] a = new int[N], f = new int[N];
    
    public static void main(String[] args) throws Exception {
    	String[] s = in.readLine().split(" ");
    	n = Integer.parseInt(s[0]);
    	m = Integer.parseInt(s[1]);
    	x = Integer.parseInt(s[2]);
    	
    	Map<Integer,Integer> map = new HashMap<>();
    	s = in.readLine().split(" ");
    	for (int i = 1; i <= n; i++) {
    		a[i] = Integer.parseInt(s[i - 1]);
    		f[i] = Math.max(map.getOrDefault(a[i] ^ x, 0), f[i - 1]);
    		map.put(a[i], i);
    	}
    	while (m-- > 0) {
    		s = in.readLine().split(" ");
    		int l = Integer.parseInt(s[0]), r = Integer.parseInt(s[1]);
    		if (f[r] >= l) {
    			out.println("yes");
    		} else {
    			out.println("no");
    		}
    	}

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

背包与魔法

原题链接

背包与魔法

问题描述

todo:这里复制题目描述

解题思路

01背包变形

参考代码

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

public class Main {

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    
    static int N = 2010, M = 10010, n, m, k;
    static int[] v = new int[N], w = new int[N];
    static int[][] f = new int[M][2];
    
    public static int max(int... nums) {
    	int ans = nums[0];
    	for (int num: nums) ans = Math.max(ans, num);
    	return ans;
    }
    
    public static void main(String[] args) throws Exception {
    	String[] s = in.readLine().split(" ");
    	n = Integer.parseInt(s[0]);
    	m = Integer.parseInt(s[1]);
    	k = Integer.parseInt(s[2]);
    	
    	for (int i = 1; i <= n; i++) {
    		s = in.readLine().split(" ");
    		v[i] = Integer.parseInt(s[0]);
    		w[i] = Integer.parseInt(s[1]);
    	}
    	
    	for (int i = 1; i <= n; i++) {
    		for (int j = m; j >= v[i]; j--) {
    			// 不使用魔法
    			f[j][0] = max(f[j][0], f[j - v[i]][0] + w[i]);
    			// 使用魔法
    			if (v[i] + k <= j) {
    				f[j][1] = max(f[j][1], f[j - v[i]][1] + w[i], f[j - v[i] - k][0] + w[i] * 2);
    			}
    		}
    	}
    	out.println(Math.max(f[m][0], f[m][1]));
    	
        out.flush();
        in.close();
    }  
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值