当前位置:   article > 正文

蓝桥杯考前复习二

蓝桥杯考前复习二

1.快速幂

  1. public static long qmi(long a, long b, long p) {
  2. long r = 1;
  3. while (b != 0) {
  4. if ((b & 1) == 1) {
  5. r = (r * a) % p;
  6. }
  7. b >>= 1;
  8. a = a * a % p;
  9. }
  10. return r;
  11. }

2.Java日期类

日期问题暂更

3.日期问题模板

考前更新

4.状态机DP

1.松散子序列 - 蓝桥云课 (lanqiao.cn)

  1. import java.util.Scanner;
  2. public class 松散子序列 {
  3. static int N=1000010;
  4. static int[][]f=new int[N][2];
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. String s = sc.next();
  8. int n=s.length();
  9. s=' '+s;
  10. for (int i = 1; i <=n ; i++) {
  11. f[i][1]=f[i-1][0]+s.charAt(i)-96;
  12. f[i][0]=Math.max(f[i-1][0],f[i-1][1]);
  13. }
  14. System.out.println(Math.max(f[n][0],f[n][1]));
  15. }
  16. }

5.多重背包

1371. 货币系统 - AcWing题库

  1. import java.util.Scanner;
  2. public class Main {
  3. //f[i][j]=f[i-1][j]+f[i-1][j-v[i]]+f[i-1][j-2*v[i]]+...
  4. //f[i][j-v[i]=f[i-1][j-v[i]]
  5. static int N = 30, M = 10010;
  6. static int n, m;
  7. static long[][] f = new long[N][M];
  8. static long[] v = new long[N];
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11. int n = sc.nextInt();
  12. int m = sc.nextInt();
  13. for (int i = 1; i <= n; i++) {
  14. v[i] = sc.nextLong();
  15. }
  16. for (int i = 0; i <= n; i++)
  17. f[i][0] = 1;
  18. for (int i = 1; i <= n; i++) {
  19. for (int j = 0; j <= m; j++) {
  20. f[i][j]=f[i-1][j];
  21. if(j>=v[i]) {
  22. f[i][j]+=f[i][(int) (j-v[i])];
  23. }
  24. }
  25. }
  26. System.out.println(f[n][m]);
  27. }
  28. }

重点掌握多重背包的优化;

6.Floyd算法

  1. void floyd()
  2. {
  3. for (int k = 1; k <= n; k ++ )
  4. for (int i = 1; i <= n; i ++ )
  5. for (int j = 1; j <= n; j ++ )
  6. d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  7. }

7.区间合并

给定 n 个区间 [li,ri]],要求合并所有有交集的区间。

注意如果在端点处相交,也算有交集。

输出合并完成后的区间个数。

例如:[1,3] 和 [2,6] 可以合并为一个区间 [1,6]。

输入格式

第一行包含整数 n。

接下来 n 行,每行包含两个整数 l 和 r。

输出格式

共一行,包含一个整数,表示合并区间完成后的区间个数。

  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. public class Main {
  4. static int N=100010;
  5. static Node[]A=new Node[N];
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8. int n=sc.nextInt();
  9. for(int i=0;i<n;i++) {
  10. int l=sc.nextInt();
  11. int r=sc.nextInt();
  12. A[i]=new Node(l,r);
  13. }
  14. Arrays.sort(A,0,n);
  15. int start=A[0].l,end=A[0].r;
  16. int res=1;
  17. for(int i=1;i<n;i++) {
  18. if(A[i].l<=end) {
  19. end=Math.max(end, A[i].r);
  20. }else {
  21. res++;
  22. start=A[i].l;
  23. end=A[i].r;
  24. }
  25. }
  26. System.out.println(res);
  27. }
  28. static class Node implements Comparable<Node>{
  29. int l;
  30. int r;
  31. public Node(int l, int r) {
  32. super();
  33. this.l = l;
  34. this.r = r;
  35. }
  36. @Override
  37. public int compareTo(Node o) {
  38. // TODO Auto-generated method stub
  39. return this.l-o.l;
  40. }
  41. }
  42. }

 8.优先队列贪心问题

1.最大开支 - 蓝桥云课 (lanqiao.cn)

  1. import java.util.PriorityQueue;
  2. import java.util.Scanner;
  3. public class 最大开支 {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. PriorityQueue<Node> pq = new PriorityQueue<Node>();
  7. int n = sc.nextInt();
  8. int m = sc.nextInt();
  9. long sum = 0;
  10. for (int i = 0; i < m; i++) {
  11. int k = sc.nextInt();
  12. int b = sc.nextInt();
  13. if (k + b < 0)
  14. continue;
  15. pq.offer(new Node(k, b, 1, k+b));
  16. }
  17. for (int i = 1; i <= n; i++) {
  18. if (pq.peek().c > 0) {
  19. Node t = pq.poll();
  20. sum += t.c;
  21. int c = 2 * t.k * t.x + t.k + t.b;
  22. int x = t.x + 1;
  23. if (t.c > 0) {
  24. pq.offer(new Node(t.k, t.b, x, c));
  25. }
  26. }
  27. else {
  28. break;
  29. }
  30. }
  31. System.out.println(sum);
  32. }
  33. static class Node implements Comparable<Node> {
  34. int k;
  35. int b;
  36. int x;
  37. int c;
  38. public Node(int k, int b, int x, int c) {
  39. super();
  40. this.k = k;
  41. this.b = b;
  42. this.x = x;
  43. this.c = c;
  44. }
  45. @Override
  46. public int compareTo(Node o) {
  47. // TODO Auto-generated method stub
  48. return o.c - this.c;
  49. }
  50. }
  51. }

9.路径

3.路径 - 蓝桥云课 (lanqiao.cn)

  1. import java.util.Arrays;
  2. public class 路径 {
  3. static int N = 3000,n,m,k,INF = 0x3f3f3f3f;
  4. static int[][] g = new int[N][N];
  5. static int[] dist = new int[N];
  6. static boolean[] st = new boolean[N];
  7. public static void main(String[] args) {
  8. for (int i = 1; i <= 2021; i++) {
  9. for (int j = 1; j <= 2021; j++) {
  10. if (i == j)
  11. g[i][j] = 0;
  12. else if (Math.abs(i - j) > 21)
  13. g[i][j] = INF;
  14. else if (Math.abs(i - j) <= 21)
  15. g[i][j] = lcm(i, j);
  16. }
  17. }
  18. System.out.println(dijkstra());
  19. }
  20. public static int dijkstra() {
  21. n=2021;
  22. Arrays.fill(dist, INF);
  23. dist[1] = 0;
  24. for (int i = 0; i <n; i++) {
  25. int t = -1;
  26. for (int j = 1; j <=n; j++) {
  27. if (!st[j] && ((t == -1) || dist[j] < dist[t])) {
  28. t = j;
  29. }
  30. }
  31. st[t] = true;
  32. for (int j = 1; j <=n; j++) {
  33. dist[j] = Math.min(dist[j], dist[t] + g[t][j]);
  34. }
  35. }
  36. if(dist[n]==INF)return -1;
  37. else return dist[n];
  38. }
  39. public static int gcd(int a, int b) {
  40. return b != 0 ? gcd(b, a % b):a;
  41. }
  42. public static int lcm(int a, int b) {
  43. return a * b / gcd(a, b);
  44. }
  45. }

10.双指针

1.完全二叉树的权值 - 蓝桥云课 (lanqiao.cn)

  1. import java.util.Scanner;
  2. public class 完全二叉树的权值 {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int n = sc.nextInt();
  6. int[] a = new int[n + 1];
  7. for (int i = 1; i <= n; i++) {
  8. a[i] = sc.nextInt();
  9. }
  10. int max = Integer.MIN_VALUE;
  11. int index = 0;
  12. for (int k = 1, i = 1; i <= n; i *= 2, k++) {
  13. long sum = 0;
  14. for (int j = i; j < i+Math.pow(2, k - 1)&&j<=n; j++) {
  15. sum += a[j];
  16. }
  17. if (sum > max) {
  18. max=(int) sum;
  19. index = k;
  20. }
  21. }
  22. System.out.println(index);
  23. }
  24. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/390927
推荐阅读
相关标签
  

闽ICP备14008679号