目录
第 1 题:玩具
代码:
第 2 题:滑雪_dfs
代码:
第 3 题:abb_后缀和
代码:
第 4 题:小红取数_dp
代码:
package 第十四届蓝桥杯三月真题刷题训练.day26;
import java.io.*;
import java.util.Arrays;
/**
* @author yx
* @date 2023-03-29 18:37
*/
public class 玩具 {
static PrintWriter out =new PrintWriter(System.out);
static BufferedReader ins=new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer in=new StreamTokenizer(ins);
/**
* 输入
* in.nextToken()
* int a= (int)in.nval;
*
* 输出
* out.print();
* out.flush();
*
* 读文件:
* BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\yx\\Desktop\\primes.txt")));
* String s = br.readLine();s读取每一行数据
* if (s == null)break;读取文件终止的语句
**/
public static void main(String[] args) throws IOException {
in.nextToken();
int N=(int) in.nval;
long[] nums=new long[N];
String[] strings=ins.readLine().split(" ");
for (int i = 0; i < N; i++) {
nums[i]=Long.parseLong(strings[i]);
}
Arrays.sort(nums);
long ans=0;
for (int i = N-1; i >=0 ; i-=2) {
ans+=nums[i];
}
out.println(ans);
out.flush();
}
}
package 第十四届蓝桥杯三月真题刷题训练.day26;
import java.io.*;
/**
* @author yx
* @date 2023-03-29 18:46
*/
public class 滑雪_记忆化搜索 {
static PrintWriter out =new PrintWriter(System.out);
static BufferedReader ins=new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer in=new StreamTokenizer(ins);
static int ans=0;
static boolean[][] isTrue;
static int[] X={0,0,-1,1};
static int[] Y={1,-1,0,0};
static int[][] nums;
static int n;
static int m;
/**
* 输入
* in.nextToken()
* int a= (int)in.nval;
*
* 输出
* out.print();
* out.flush();
*
* 读文件:
* BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\yx\\Desktop\\primes.txt")));
* String s = br.readLine();s读取每一行数据
* if (s == null)break;读取文件终止的语句
**/
public static void main(String[] args) throws IOException {
in.nextToken();
n=(int) in.nval;
in.nextToken();
m=(int) in.nval;
nums=new int[n][m];
isTrue=new boolean[n][m];
String[] strings=new String[n];
for (int i = 0; i < n; i++) {
strings[i]=ins.readLine();
}
for (int i = 0; i < n; i++) {
String[] strings1=strings[i].split(" ");
for (int j = 0; j < m; j++) {
nums[i][j]=Integer.parseInt(strings1[j]);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
dfs(i,j,1);
}
}
out.println(ans);
out.flush();
}
static void dfs(int i,int j,int cnt){
ans=Math.max(ans,cnt);
//表示这个地方搜过了
isTrue[i][j]=true;
for (int k = 0; k < 4; k++) {
int newX=i+X[k];
int newY=j+Y[k];
if(newX>=0&&newY>=0&&newXnums[newX][newY]&&!isTrue[newX][newY]){
dfs(newX,newY,cnt+1);//步数+1
}
}
//回溯
isTrue[i][j]=false;
}
}
package 第十四届蓝桥杯三月真题刷题训练.day26;
import java.io.*;
/**
* @author yx
* @date 2023-03-29 19:16
*/
public class abb_后缀和_long {
static PrintWriter out =new PrintWriter(System.out);
static BufferedReader ins=new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer in=new StreamTokenizer(ins);
/**
* 输入
* in.nextToken()
* int a= (int)in.nval;
*
* 输出
* out.print();
* out.flush();
*
* 读文件:
* BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\yx\\Desktop\\primes.txt")));
* String s = br.readLine();s读取每一行数据
* if (s == null)break;读取文件终止的语句
**/
public static void main(String[] args) throws IOException {
in.nextToken();
int length=(int) in.nval;
char[] arrays=ins.readLine().toCharArray();
int[][] nums=new int[length+1][26];
char temp=' ';
long ans=0;
for (int i = length-1; i >= 0; i--) {
//先对上一组全部备份
for (int j = 0; j < 26; j++) {
nums[i][j]=nums[i+1][j];
}
temp=arrays[i];
//在上一份的基础上进行++
nums[i][temp-'a']++;
}
for (int i = 0; i < length; i++) {
for (int j = 0; j < 26; j++) {
if(j!=arrays[i]-'a'){
ans+=(long) (nums[i][j])*(nums[i][j]-1)/2;
}
}
}
out.println(ans);
out.flush();
}
}
package 第十四届蓝桥杯三月真题刷题训练.day26;
import java.io.*;
import java.util.Arrays;
/**
* @author yx
* @date 2023-03-29 19:50
*/
public class 小红取数_dp {
static PrintWriter out =new PrintWriter(System.out);
static BufferedReader ins=new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer in=new StreamTokenizer(ins);
/**
* 输入
* in.nextToken()
* int a= (int)in.nval;
*
* 输出
* out.print();
* out.flush();
*
* 读文件:
* BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\yx\\Desktop\\primes.txt")));
* String s = br.readLine();s读取每一行数据
* if (s == null)break;读取文件终止的语句
**/
public static void main(String[] args) throws IOException {
in.nextToken();
int n=(int) in.nval;
in.nextToken();
int k=(int) in.nval;
long[][]dp=new long[n+1][k];
long[] nums=new long[n+1];
String[] strings=ins.readLine().split(" ");
for (int i = 0; i <= n; i++) {
Arrays.fill(dp[i],Long.MIN_VALUE);
}
for (int i = 0; i < n; i++) {
nums[i+1]=Long.parseLong(strings[i]);
}
dp[0][0]=0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < k; j++) {
//无敌的状态转移方程
dp[i][(int)((nums[i]+j)%k)]=Math.max(dp[i-1][j]+nums[i],dp[i-1][(int)((nums[i]+j)%k)]);
}
}
// for (int i = 0; i <= n ; i++) {
// for (int j = 0; j < k; j++) {
// System.out.print(dp[i][j]+" ");
// }
// System.out.println();
// }
if(dp[n][0]>0)out.println(dp[n][0]);
else out.println(-1);
out.flush();
}
/*
0 -1 -1 -1 -1
7 7 7 8 7
10 9 9 9 9
18 18 18 18 19
20 19 19 19 19
20 19 19 19 19
20
*/
/*
0 0 0 0 0
8 8 8 8 8
10 10 10 10 10
19 19 19 19 19
20 20 20 20 20
20 20 20 20 20
20
*/
}