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

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

题目1:数列求值

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 {
    	
    	long a = 1, b = 1, c = 1;
    	long ans = 0;
    	for (int i = 4; i <= 20190324; i++) {
    		ans = (a + b + c) % 10000;
    		a = b;
    		b = c;
    		c = ans;
    	}
    	out.println(ans);

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

题目2:质数

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)));
    
    static boolean isPrime(int x) {
    	if (x <= 1) return false;
    	int end = (int)Math.sqrt(x);
    	for (int i = 2; i <= end; i++) {
    		if (x % i == 0) return false;
    	}
    	return true;
    }

    public static void main(String[] args) throws Exception {
    	
    	int cnt = 0;
    	for (int i = 0; i < 100000; i++) {
    		if (isPrime(i)) cnt++;
    		if (cnt == 2019) {
    			out.println(i);
    			break;
    		}
    	}

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

题目3:饮料换购

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()), ans = 0;
		// n 为剩余的数量,剩余数量大于等于3,一定可以换购
		// ans 为喝掉的数量
    	while (n >= 3) {
    		ans += n / 3 * 3;
    		n = n / 3 + n % 3;
    	}
    	out.println(ans + n);

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

题目4:巧克力

前置知识:优先队列、自定义排序

这个解法可能不是最优解

假设我们按照价格进行排序,优先选择价格比较低的,那么很可能最后一天可能没有巧克力可以吃(价格低的保质期可能很长,价格高的可能保质期很短,并且价格低的数量可能很少)。

比如:如果现在需要吃 7 天,如果每次我们考虑价格低的,那么最后 3 天就没有巧克力可以选择了。

价格保质期数量
1103
10045

思路:

贪心的考虑,如果在第 n 天吃的巧克力的保质期是大于等于 n 的,那么可以保证每一天都有巧克力可以吃到,只需要找出满足条件价格最低的一种吃掉就可以。除非数量不够或者是没有一种巧克力的保质期大于等于 n。比如:当前是第 11 天,虽然数量足够,但是都过期了。

价格保质期数量
1103
10049

根据思路:我们按照保质期、价格、数量进行排序,保质期高的在前边,低的在后边;保质期相同,则按照价格降序排序,价格相同再按照升序排序。

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

class Node {
    // p:价格price	d:保质期days	c:数量count
	public int p, d, c;
	public Node(int p, int d, int c) {
		this.p = p;
		this.d = d;
		this.c = c;
	}
}
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 x, n;
    // 优先队列1:存放所有种类的巧克力,按照上述排序方式排序
    static PriorityQueue<Node> pq = new PriorityQueue<>(new Comparator<Node>() {

		@Override
		public int compare(Node o1, Node o2) {
			if (o1.d < o2.d) {
				return 1;
			} else if (o1.d > o2.d) {
				return -1;
			} else {
				if (o1.p > o2.p) {
					return 1;
				} else if (o1.p < o2.p) {
					return -1;
				} else {
					if (o1.c < o2.c) {
						return 1;
					} else if (o1.c > o2.c) {
						return -1;
					}
				}
			}
			return 0;
		}
    	
	});
    // 优先队列2:存放符合要求的巧克力,天数已经符合了,按照价格排序即可
    static PriorityQueue<Node> pq2 = new PriorityQueue<>(new Comparator<Node>() {

		@Override
		public int compare(Node o1, Node o2) {
			if (o1.p > o2.p) {
				return 1;
			} else if (o1.p < o2.p) {
				return -1;
			} else {
				if (o1.c < o2.c) {
					return 1;
				} else if (o1.c > o2.c) {
					return -1;
				}
			}
			return 0;
		}
    	
	});

    public static long helper() {
    	long ans = 0L;
        // 从最后一天开始枚举
    	for (int i = x; i >= 1; i--) {
            // 如果保质期满足要求就把他添加到队列2中
    		while (!pq.isEmpty() && pq.peek().d >= i) {
    			pq2.offer(pq.poll());
    		}
            // 没有发现一种满足条件的,直接返回
    		if (pq2.isEmpty()) return -1;
            // 从队列2中取出价格最低的,把它的价格加到答案里边
    		Node t = pq2.poll();
    		ans += t.p;
            // 数量减一
    		t.c -= 1;
            // 如果还有剩余,再把它加回队列中
    		if (t.c > 0) pq2.offer(t);	
    	}
    	return ans;
    }

    public static void main(String[] args) throws Exception {
        // input...
    	String[] xn = in.readLine().split(" ");
    	x = Integer.parseInt(xn[0]);
    	n = Integer.parseInt(xn[1]);
    	for (int i = 0; i < n; i++) {
    		String[] s = in.readLine().split(" ");
    		int a = Integer.parseInt(s[0]), b = Integer.parseInt(s[1]), c = Integer.parseInt(s[2]);
    		pq.offer(new Node(a, b, c));
    	}
    	
    	out.println(helper());

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

数组解法

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

class Node {
	public int p, d, c;
	public Node(int p, int d, int c) {
		this.p = p;
		this.d = d;
		this.c = c;
	}
}
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, x, n;
    static Node[] arr = new Node[N];

    static PriorityQueue<Node> pq = new PriorityQueue<>(new Comparator<Node>() {

		@Override
		public int compare(Node o1, Node o2) {
			if (o1.p > o2.p) {
				return 1;
			} else if (o1.p < o2.p) {
				return -1;
			} else {
				if (o1.c < o2.c) {
					return 1;
				} else if (o1.c > o2.c) {
					return -1;
				}
			}
			return 0;
		}
    	
	});

    public static long helper() {
    	long ans = 0L;
    	int idx = 0;
    	
    	for (int i = x; i >= 1; i--) {
    		while (idx < n && arr[idx].d >= i) {
    			pq.offer(arr[idx]);
    			idx++;
    		}
    		
    		if (pq.isEmpty()) return -1;
    		
    		Node t = pq.poll();
    		ans += t.p;
    		t.c -= 1;
    		if (t.c > 0) pq.offer(t);
    	}
    	return ans;
    }

    public static void main(String[] args) throws Exception {
    	String[] xn = in.readLine().split(" ");
    	x = Integer.parseInt(xn[0]);
    	n = Integer.parseInt(xn[1]);
    	for (int i = 0; i < n; i++) {
    		String[] s = in.readLine().split(" ");
    		int a = Integer.parseInt(s[0]), b = Integer.parseInt(s[1]), c = Integer.parseInt(s[2]);
    		arr[i] = new Node(a, b, c);
    	}
    	Arrays.sort(arr, 0, n, new Comparator<Node>(){

			@Override
			public int compare(Node o1, Node o2) {
				if (o1.d < o2.d) {
					return 1;
				} else if (o1.d > o2.d) {
					return -1;
				}
				return 0;
			}
		});
    	out.println(helper());

        out.flush();
        in.close();
    }  
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值