蓝桥杯练习题(六)

📑前言

本文主要是【算法】------蓝桥杯练习题(六)的文章,如果有什么需要改进的地方还请大佬指出⛺️

🎬作者简介:大家好,我是听风与他🥇

☁️博客首页:CSDN主页听风与他

🌄每日一句:狠狠沉淀,顶峰相见

目录

172.递增三元组

java 复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;

public class Main {
/*
3
1 1 1
2 2 2
3 3 3
27
 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		sc.nextToken();
		int n = (int)sc.nval;
		int a[] = new int[n];
		int b[] = new int[n];
		int c[] = new int[n];
		for(int i=0;i<n;i++) {
			sc.nextToken();
			a[i] = (int)sc.nval;
		}
		for(int i=0;i<n;i++) {
			sc.nextToken();
			b[i] = (int)sc.nval;
		}
		for(int i=0;i<n;i++) {
			sc.nextToken();
			c[i] = (int)sc.nval;
		}
		Arrays.sort(a);
		Arrays.sort(b);
		Arrays.sort(c);
		long sum = 0;
		int i=0,k=0;
		for(int j=0;j<n;j++) {
			while(i<n&&a[i]<b[j]) i++;
			while(k<n&&c[k]<=b[j]) k++;
			sum+=(long)i*(n-k);
		}
		System.out.println(sum);
	}

}

99.分巧克力

java 复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Main {

	static int[][] a;
	static int k;
	static int n;
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		sc.nextToken();
		n = (int)sc.nval;
		sc.nextToken();
		k = (int)sc.nval;
		a = new int[n][2];
		for(int i=0;i<n;i++) {
			sc.nextToken();
			a[i][0]=(int)sc.nval;
			sc.nextToken();
			a[i][1]=(int)sc.nval;
		}
		int l=1,r=(int)1e5;
		while(l<r) {
			int mid = (l+r+1)/2;
			if(check(mid)) {
				l = mid;
			}else {
				r = mid - 1;
			}
		}
		System.out.println(l);
	}
	
	public static boolean check(int x) {
		int sum=0;
		for(int i=0;i<n;i++) {
			sum+=(a[i][0]/x)*(a[i][1]/x);
		}
		if(sum<k) {
			return false;
		}
		return true;
	}
	
}

1443.卡片

java 复制代码
public class Main {

	static int a[];
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		a =new int[10];
		for(int i=0;i<10;i++) {
			a[i] = 2021;
		}
		int ans = 1;
		while(f(ans)) {
			ans++;
		}
		System.out.println(ans-1);
	}

	public static boolean f(int n) {
		while(n>0) {
			a[n%10]--;
			if(a[n%10]<0) {
				return false;
			}
			n/=10;
		}
		return true;
	}
}

2191.卡牌

java 复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Scanner;

public class Main {

	static long a[];
	static long b[];
	static int n;
	static long m;
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		sc.nextToken();
		n = (int)sc.nval;
		sc.nextToken();
		m = (long)sc.nval;
		a = new long[ n];
		b = new long[ n];
		for(int i=0;i<n;i++) {
			sc.nextToken();
			a[i]=(long)sc.nval;
		}
		for(int i=0;i<n;i++) {
			sc.nextToken();
			b[i]=(long)sc.nval;
		}
		int l=0,r=(int)4e5;
		while(l<r) {
			//满足要求最大的
			int mid = (l+r+1)/2;
			if(check(mid)) {
				l = mid;
			}else {
				r = mid - 1;
			}
		}
		System.out.println(l);
	}

	public static boolean check(int k) {
		int t=0;
		for(int i=0;i<n;i++) {
			if(a[i]<k) {
				if(k-a[i]>b[i]) {
					return false;
				}else {
					t+=k-a[i];
				}
			}
		}
		if(t>m) {
			return false;
		}
		return true;
	}
}

1372.美丽的区间

java 复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Main {
	/*
5 6
1 2 3 4 5
2
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		sc.nextToken();
		int n = (int)sc.nval;
		sc.nextToken();
		int s = (int)sc.nval;
		int a[] = new int[n+1];
		for(int i=1;i<=n;i++) {
			sc.nextToken();
			a[i] = (int)sc.nval;
		}
		long sum[] = new long[n+1];
		for(int i=1;i<=n;i++) {
			sum[i] = a[i]+sum[i-1];
		}
		int min = Integer.MAX_VALUE;
		for(int i=1;i<=n;i++) {//固定左边界,二分右边界
			int l = i-1,r=n;
			while(l<r) {
				int mid = (l+r)/2;
				if(sum[mid]-sum[i-1]>=s) {
					r = mid;
				}else {
					l = mid + 1;
				}
			}
			if(sum[r]-sum[i-1]>=s) {
				min = Math.min(min, r-i+1);
			}
		}
		if(min==Integer.MAX_VALUE) {
			System.out.println(0);
		}else {
			System.out.println(min);
		}
	}

}

📑文章末尾

相关推荐
Code apprenticeship几秒前
Java面试题(2)
java·开发语言
憨子周1 小时前
2M的带宽怎么怎么设置tcp滑动窗口以及连接池
java·网络·网络协议·tcp/ip
passer__jw7672 小时前
【LeetCode】【算法】3. 无重复字符的最长子串
算法·leetcode
passer__jw7672 小时前
【LeetCode】【算法】21. 合并两个有序链表
算法·leetcode·链表
sweetheart7-72 小时前
LeetCode22. 括号生成(2024冬季每日一题 2)
算法·深度优先·力扣·dfs·左右括号匹配
霖雨3 小时前
使用Visual Studio Code 快速新建Net项目
java·ide·windows·vscode·编辑器
SRY122404193 小时前
javaSE面试题
java·开发语言·面试
Fiercezm3 小时前
JUC学习
java
无尽的大道3 小时前
Java 泛型详解:参数化类型的强大之处
java·开发语言
ZIM学编程3 小时前
Java基础Day-Sixteen
java·开发语言·windows