蓝桥杯练习题(六)

📑前言

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

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

☁️博客首页: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);
		}
	}

}

📑文章末尾

相关推荐
xlsw_2 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹3 小时前
基于java的改良版超级玛丽小游戏
java
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭4 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
XH华4 小时前
初识C语言之二维数组(下)
c语言·算法
暮湫4 小时前
泛型(2)
java
超爱吃士力架4 小时前
邀请逻辑
java·linux·后端
南宫生4 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石4 小时前
12/21java基础
java
不想当程序猿_4 小时前
【蓝桥杯每日一题】求和——前缀和
算法·前缀和·蓝桥杯
李小白664 小时前
Spring MVC(上)
java·spring·mvc