不知道这几题有没有问题,大佬进来帮忙看看。
第二题,直接找最长的0子串。
// 最长连续子串 public class pdd2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; int basecount = 0; for(int i=0; i<n; i++) { a[i] = sc.nextInt(); if (a[i] == 1) { basecount ++; } } System.out.println(basecount + answer(a)); } public static int answer(int[] a){ int max = Integer.MIN_VALUE; int current = 0; for(int i=0; i<a.length; i++) { if (a[i] == 0) { current++; } else { current = 0; } if (current > max) { max = current; } } return max; } }
// 冰冻青蛙 public class pdd4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for(int i=0; i<t; i++) { Integer n = scanner.nextInt(); Long m = scanner.nextLong(); Long k = scanner.nextLong(); ArrayList<Long> a= new ArrayList<>(); for(int j=0; j<n; j++) { a.add(scanner.nextLong()); } System.out.println(answer(n,m,k,a)); } } public static Long answer(Integer n, Long m, Long k, ArrayList<Long> a) { Long[] dp = new Long[n]; int pos = 0; for(int i=0; i<n; i++) { dp[i] = a.get(i); } dp[1] = dp[1]-k; for(int i=2; i<=m; i++) { if (pos-1 < 0) { dp[pos+1] = dp[pos+1]-k; pos++; continue; } if (pos+1 >= n) { dp[pos-1] = dp[pos-1]-k; pos--; continue; } if (dp[pos+1] < dp[pos-1] ) { dp[pos-1] = dp[pos-1] - k; pos --; } else { dp[pos+1] = dp[pos+1] - k; pos ++; } } Long min = Long.MAX_VALUE; for (int j=0; j<n; j++) { if (dp[j] < min) { min = dp[j]; } } return min; } }
全部评论
(13) 回帖