📑前言
本文主要是【算法】——蓝桥杯练习题(六)的文章,如果有什么需要改进的地方还请大佬指出⛺️
🎬作者简介:大家好,我是听风与他🥇
☁️博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见
目录
- 📑前言
- 172.递增三元组
- 99.分巧克力
- 1443.卡片
- 2191.卡牌
- 1372.美丽的区间
- 📑文章末尾
172.递增三元组
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.分巧克力
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.卡片
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.卡牌
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.美丽的区间
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);
}
}
}