1、卡牌-二分
1.1、问题描述
1.2、输入格式
1.3、输出格式
1.4、样例输入
4 5
1 2 3 4
5 5 5 5
1.5、样例输出
3
1.6、样例说明
1.7、用例规模与约束
1.8、运行限制
1.9、代码
import java.io.*;
public class Main {
static PrintWriter out = new PrintWriter(System.out);
static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer in = new StreamTokenizer(ins);
static int n;
static long m;
static int[] a;
static int[] b;
public static void main(String[] args) throws IOException {
String[] s1 = ins.readLine().split(" ");
n = Integer.parseInt(s1[0]);
m = Long.parseLong(s1[1]);
a = new int[n];
b = new int[n];
String[] s2 = ins.readLine().split(" ");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(s2[i]);
}
String[] s3 = ins.readLine().split(" ");
for (int i = 0; i < n; i++) {
b[i] = Integer.parseInt(s3[i]);
}
int l = 0,r = 2 * n;
int res = 0;
while (l <= r) {
int mid = (l + r) / 2;
if (check(mid)) {
res = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
out.print(res);
out.flush();
}
private static boolean check(int mid) {
long tmp = m;
for (int i = 0; i < n; i++) {
if (a[i] >= mid) continue;
if (a[i] + b[i] < mid) return false;
if (tmp + a[i] < mid) return false;
if (mid - a[i] <= tmp) {
tmp -= (mid - a[i]);
} else {
return false;
}
}
return true;
}
}
1.10、思路讲解
就是利用二分找到可以达到的最多的套牌数量,利用二分[0,2n]中寻找,如果check(i)满足条件,则可以凑到mid套牌,但是可能存在一个更大的数也满足需要,继续进行二分查找,直到找到满足需要的最大值
二分模板
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)){
ans=mid;
l=mid+1;//继续找到那个最大数量的套牌数,直到不满足条件
}
else{
r=mid+1;
}
}
2、蓝桥杯真题——最大数字
2.1、问题描述
2.2、输入格式
2.3、输出格式
2.4、样例输入
123 1 2
2.5、样例输出
933
2.6、样例说明
2.7、用例规模与约束
2.8、运行限制
2.9、代码
暴力,贪心朴素
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//在此输入您的代码...
String number;
int A,B;
number=in.next();
A=in.nextInt();
B=in.nextInt();
int n=number.length();
int numberindex[]=new int[number.length()];
int index=n-1;
for(int i=0;i<n;i++){
numberindex[i]=(number.charAt(i)-'0');
}
for(int i=0;i<n;i++){
int max=0;
if(numberindex[i]==9){
continue;
}
if(numberindex[i]<B){
if(numberindex[i]+1<9-numberindex[i]||A<B||A<9-numberindex[i]){//利用贪心的策略,优先进行A操作
B-=numberindex[i]+1;
numberindex[i]=9;
}
}
while(numberindex[i]!=9&&A>0){
numberindex[i]++;
A--;
}
}
String ans="";
for(int i=0;i<n;i++){
ans+=numberindex[i];
}
System.out.println(ans);
in.close();
}
}
暴力DFS
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static long a, b, sum;
static String str;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//在此输入您的代码...Scanner sc=new Scanner(System.in);
str = sc.next();//一个正整数
a = sc.nextLong();//1号操作a次
b = sc.nextLong();//2号操作b次
dfs(0, a, b, 0L);
System.out.println(sum);
}
public static void dfs(int i, long a, long b, long v) { //
if (i < str.length()) {
long x = (long) (str.charAt(i) - '0');
long t = Math.min(a, 9 - x);
dfs(i + 1, a - t, b, v * 10L + x + t);
if (b > x) {
dfs(i + 1, a, b - x - 1L, v * 10L + 9);
}
} else {
sum = Math.max(sum, v);
}
}
}