modify code

master
algorithmzuo 2 years ago
parent 47becb5b25
commit a662fd4c38

@ -1,56 +1,35 @@
package 01.mca_08_dp;
//有m个同样的苹果认为苹果之间无差别
//有n个同样的盘子认为盘子之间也无差别
//还有比如5个苹果如果放进3个盘子
//那么1、3、1和1、1、3和3、1、1的放置方法也认为是一种方法
//如上的设定下,返回有多少种放置方法
//测试链接 : https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
//提交以下的code提交时请把类名改成"Main"
// 有m个同样的苹果认为苹果之间无差别
// 有n个同样的盘子认为盘子之间也无差别
// 还有比如5个苹果如果放进3个盘子
// 那么1、3、1和1、1、3和3、1、1的放置方法也认为是一种方法
// 如上的设定下,返回有多少种放置方法
// 测试链接 : https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 提交以下的code提交时请把类名改成"Main"
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Scanner;
public class Code09_SplitApples {
public static int test(int apples, int plates) {
int[][] dp = new int[apples + 1][plates + 1];
for (int i = 0; i <= apples; i++) {
for (int j = 0; j <= plates; j++) {
dp[i][j] = -1;
}
}
return f(apples, plates, dp);
}
public static int f(int apples, int plates, int[][] dp) {
if (dp[apples][plates] != -1) {
return dp[apples][plates];
}
int ans = 0;
if (apples == 0) {
ans = 1;
} else if (plates == 0) {
ans = 0;
} else {
if (plates > apples) {
ans = f(apples, apples, dp);
} else { // apples >= plates;
ans = f(apples, plates - 1, dp) + f(apples - plates, plates, dp);
}
}
dp[apples][plates] = ans;
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int m = sc.nextInt();
int n = sc.nextInt();
int ways = ways3(m, n);
System.out.println(ways);
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int m = (int) in.nval;
in.nextToken();
int n = (int) in.nval;
out.println(ways3(m, n));
out.flush();
}
sc.close();
}
// 思路来自于分裂数问题

@ -0,0 +1,81 @@
package 02.mca_01;
// 牛客的测试链接:
// https://www.nowcoder.com/questionTerminal/8ecfe02124674e908b2aae65aad4efdf
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 把如下的全部代码拷贝进java编辑器
// 把文件大类名字改成Main可以直接通过
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Code01_CherryPickup {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int N = (int) in.nval;
in.nextToken();
int M = (int) in.nval;
int[][] matrix = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
in.nextToken();
matrix[i][j] = (int) in.nval;
}
}
out.println(cherryPickup(matrix));
out.flush();
}
}
public static int cherryPickup(int[][] grid) {
int N = grid.length;
int M = grid[0].length;
int[][][] dp = new int[N][M][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
for (int k = 0; k < N; k++) {
dp[i][j][k] = Integer.MIN_VALUE;
}
}
}
int ans = process(grid, 0, 0, 0, dp);
return ans < 0 ? 0 : ans;
}
public static int process(int[][] grid, int x1, int y1, int x2, int[][][] dp) {
if (x1 == grid.length || y1 == grid[0].length || x2 == grid.length || x1 + y1 - x2 == grid[0].length) {
return Integer.MIN_VALUE;
}
if (dp[x1][y1][x2] != Integer.MIN_VALUE) {
return dp[x1][y1][x2];
}
if (x1 == grid.length - 1 && y1 == grid[0].length - 1) {
dp[x1][y1][x2] = grid[x1][y1];
return dp[x1][y1][x2];
}
int next = Integer.MIN_VALUE;
next = Math.max(next, process(grid, x1 + 1, y1, x2 + 1, dp));
next = Math.max(next, process(grid, x1 + 1, y1, x2, dp));
next = Math.max(next, process(grid, x1, y1 + 1, x2, dp));
next = Math.max(next, process(grid, x1, y1 + 1, x2 + 1, dp));
if (grid[x1][y1] == -1 || grid[x2][x1 + y1 - x2] == -1 || next == -1) {
dp[x1][y1][x2] = -1;
return dp[x1][y1][x2];
}
if (x1 == x2) {
dp[x1][y1][x2] = grid[x1][y1] + next;
return dp[x1][y1][x2];
}
dp[x1][y1][x2] = grid[x1][y1] + grid[x2][x1 + y1 - x2] + next;
return dp[x1][y1][x2];
}
}

@ -0,0 +1,106 @@
package 02.mca_01;
// 牛客的测试链接:
// https://www.nowcoder.com/practice/7201cacf73e7495aa5f88b223bbbf6d1
// 不要提交包信息把import底下的类名改成Main提交下面的代码可以直接通过
// 因为测试平台会卡空间所以把set换成了动态加和减的结构
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Comparator;
import java.util.HashSet;
import java.util.PriorityQueue;
public class Code02_TopKSumCrossTwoArrays {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int N = (int) in.nval;
in.nextToken();
int K = (int) in.nval;
int[] arr1 = new int[N];
int[] arr2 = new int[N];
for (int i = 0; i < N; i++) {
in.nextToken();
arr1[i] = (int) in.nval;
}
for (int i = 0; i < N; i++) {
in.nextToken();
arr2[i] = (int) in.nval;
}
int[] topK = topKSum(arr1, arr2, K);
for (int i = 0; i < K; i++) {
out.print(topK[i] + " ");
}
out.println();
out.flush();
}
}
// 放入大根堆中的结构
public static class Node {
public int index1;// arr1中的位置
public int index2;// arr2中的位置
public int sum;// arr1[index1] + arr2[index2]的值
public Node(int i1, int i2, int s) {
index1 = i1;
index2 = i2;
sum = s;
}
}
// 生成大根堆的比较器
public static class MaxHeapComp implements Comparator<Node> {
@Override
public int compare(Node o1, Node o2) {
return o2.sum - o1.sum;
}
}
public static int[] topKSum(int[] arr1, int[] arr2, int topK) {
if (arr1 == null || arr2 == null || topK < 1) {
return null;
}
int N = arr1.length;
int M = arr2.length;
topK = Math.min(topK, N * M);
int[] res = new int[topK];
int resIndex = 0;
PriorityQueue<Node> maxHeap = new PriorityQueue<>(new MaxHeapComp());
HashSet<Long> set = new HashSet<>();
int i1 = N - 1;
int i2 = M - 1;
maxHeap.add(new Node(i1, i2, arr1[i1] + arr2[i2]));
set.add(x(i1, i2, M));
while (resIndex != topK) {
Node curNode = maxHeap.poll();
res[resIndex++] = curNode.sum;
i1 = curNode.index1;
i2 = curNode.index2;
set.remove(x(i1, i2, M));
if (i1 - 1 >= 0 && !set.contains(x(i1 - 1, i2, M))) {
set.add(x(i1 - 1, i2, M));
maxHeap.add(new Node(i1 - 1, i2, arr1[i1 - 1] + arr2[i2]));
}
if (i2 - 1 >= 0 && !set.contains(x(i1, i2 - 1, M))) {
set.add(x(i1, i2 - 1, M));
maxHeap.add(new Node(i1, i2 - 1, arr1[i1] + arr2[i2 - 1]));
}
}
return res;
}
public static long x(int i1, int i2, int M) {
return (long) i1 * (long) M + (long) i2;
}
}

@ -2,11 +2,16 @@ package class25;
// 测试链接 : https://www.nowcoder.com/practice/2a2c00e7a88a498693568cef63a4b7bb
// 如果在牛客上做题,可以用如下的方式来做
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 提交如下的代码,并把主类名改成"Main"
// 可以直接通过
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Code01_MonotonousStackForNowcoder {
@ -15,7 +20,7 @@ public class Code01_MonotonousStackForNowcoder {
// stack1 : 相等值的位置也放
// stack2 : 只放不相等值的最后一个位置
// 比如 : arr = { 3, 3, 3, 4, 4, 6, 6, 6}
// 位置 0 1 2 3 4 5 6 7
// 位置 0 1 2 3 4 5 6 7
// 如果位置依次压栈,
// stack1中的记录是位置 : 0 1 2 3 4 5 6 7
// stack1中的记录是位置 : 2 4 7
@ -24,17 +29,20 @@ public class Code01_MonotonousStackForNowcoder {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] strs = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(strs[i]);
}
getNearLess(n);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < n; i++) {
builder.append(ans[i][0] + " " + ans[i][1] + "\n");
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) in.nval;
for (int i = 0; i < n; i++) {
in.nextToken();
arr[i] = (int) in.nval;
}
getNearLess(n);
for (int i = 0; i < n; i++) {
out.println(ans[i][0] + " " + ans[i][1]);
}
out.flush();
}
System.out.println(builder.toString());
}
public static void getNearLess(int n) {

@ -6,25 +6,38 @@ package class39;
// 但是用的分治的方法
// 这是牛客的测试链接:
// https://www.nowcoder.com/questionTerminal/d94bb2fa461d42bcb4c0f2b94f5d4281
// 把如下的全部代码拷贝进编辑器java
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 提交如下的代码,并把主类名改成"Main"
// 可以直接通过
import java.util.Map.Entry;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.TreeMap;
public class Code02_SnacksWaysMain1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int bag = sc.nextInt();
int[] arr = new int[N];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) in.nval;
in.nextToken();
int bag = (int) in.nval;
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
in.nextToken();
arr[i] = (int) in.nval;
}
long ways = ways(arr, bag);
out.println(ways);
out.flush();
}
long ways = ways(arr, bag);
System.out.println(ways);
sc.close();
}
public static long ways(int[] arr, int bag) {
@ -57,14 +70,11 @@ public class Code02_SnacksWaysMain1 {
return ways + 1;
}
// arr 30
// func(arr, 0, 14, 0, bag, map)
// func(arr, 15, 29, 0, bag, map)
// 从index出发到end结束
// 之前的选择已经形成的累加和sum
// 零食[index....end]自由选择出来的所有累加和不能超过bag每一种累加和对应的方法数填在map里
@ -75,13 +85,13 @@ public class Code02_SnacksWaysMain1 {
// - - - $ 3 -> 0 : 1(3, 1)
// - - $ - 3 -> 0 : 1(3, 2)
public static long func(int[] arr, int index, int end, long sum, long bag, TreeMap<Long, Long> map) {
if(sum > bag) {
if (sum > bag) {
return 0;
}
// sum <= bag
if(index > end) { // 所有商品自由选择完了!
if (index > end) { // 所有商品自由选择完了!
// sum
if(sum != 0) {
if (sum != 0) {
if (!map.containsKey(sum)) {
map.put(sum, 1L);
} else {
@ -90,12 +100,12 @@ public class Code02_SnacksWaysMain1 {
return 1;
} else {
return 0;
}
}
}
// sum <= bag 并且 index <= end(还有货)
// 1) 不要当前index位置的货
long ways = func(arr, index + 1, end, sum, bag, map);
// 2) 要当前index位置的货
ways += func(arr, index + 1, end, sum + arr[index], bag, map);
return ways;

@ -1,26 +1,23 @@
// 不要拷贝包信息的内容
package class39;
//优化版本
// 优化版本
// 这是牛客的测试链接:
// https://www.nowcoder.com/questionTerminal/d94bb2fa461d42bcb4c0f2b94f5d4281
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 提交如下的代码,并把主类名改成"Main"
// 可以直接通过
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Scanner;
public class Code02_SnacksWaysMain2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
size = sc.nextInt();
long w = (long) sc.nextInt();
for (int i = 0; i < size; i++) {
arr[i] = (long) sc.nextInt();
}
long ways = ways(w);
System.out.println(ways);
}
sc.close();
}
// 用来收集所有输入的数字
public static long[] arr = new long[31];
public static int size = 0;
@ -33,6 +30,24 @@ public class Code02_SnacksWaysMain2 {
// 准备的数组可能用不完左部分生成了多少累加和用leftSize表示
public static int rightSize = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
size = (int) in.nval;
in.nextToken();
int bag = (int) in.nval;
for (int i = 0; i < size; i++) {
in.nextToken();
arr[i] = (int) in.nval;
}
long ways = ways(bag);
out.println(ways);
out.flush();
}
}
public static long ways(long w) {
if (size == 0) {
return 0;

@ -4,16 +4,56 @@
// 把如下代码粘贴进网页所提供的java编译器环境中
// 不需要修改任何内容可以直接通过
// 请看网页上的题目描述并结合main函数的写法去了解这个模板的用法
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
package class47;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class Code03_DinicAlgorithm {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int cases = (int) in.nval;
for (int i = 1; i <= cases; i++) {
in.nextToken();
int n = (int) in.nval;
in.nextToken();
int s = (int) in.nval;
in.nextToken();
int t = (int) in.nval;
in.nextToken();
int m = (int) in.nval;
Dinic dinic = new Dinic(n);
for (int j = 0; j < m; j++) {
in.nextToken();
int from = (int) in.nval;
in.nextToken();
int to = (int) in.nval;
in.nextToken();
int weight = (int) in.nval;
dinic.addEdge(from, to, weight);
dinic.addEdge(to, from, weight);
}
int ans = dinic.maxFlow(s, t);
out.println("Case " + i + ": " + ans);
out.flush();
}
}
}
public static class Edge {
public int from;
public int to;
@ -114,26 +154,4 @@ public class Code03_DinicAlgorithm {
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int cases = cin.nextInt();
for (int i = 1; i <= cases; i++) {
int n = cin.nextInt();
int s = cin.nextInt();
int t = cin.nextInt();
int m = cin.nextInt();
Dinic dinic = new Dinic(n);
for (int j = 0; j < m; j++) {
int from = cin.nextInt();
int to = cin.nextInt();
int weight = cin.nextInt();
dinic.addEdge(from, to, weight);
dinic.addEdge(to, from, weight);
}
int ans = dinic.maxFlow(s, t);
System.out.println("Case " + i + ": " + ans);
}
cin.close();
}
}

@ -1,28 +1,43 @@
package class14;
// 本题测试链接 : https://www.nowcoder.com/practice/e13bceaca5b14860b83cbcc4912c5d4a
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 提交以下的代码并把主类名改成Main
// 可以直接通过
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Code03_BiggestBSTTopologyInTree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int h = sc.nextInt();
int[][] tree = new int[n + 1][3];
for (int i = 1; i <= n; i++) {
int c = sc.nextInt();
int l = sc.nextInt();
int r = sc.nextInt();
tree[l][0] = c;
tree[r][0] = c;
tree[c][1] = l;
tree[c][2] = r;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) in.nval;
in.nextToken();
int h = (int) in.nval;
int[][] tree = new int[n + 1][3];
for (int i = 1; i <= n; i++) {
in.nextToken();
int c = (int) in.nval;
in.nextToken();
int l = (int) in.nval;
in.nextToken();
int r = (int) in.nval;
tree[l][0] = c;
tree[r][0] = c;
tree[c][1] = l;
tree[c][2] = r;
}
out.println(maxBSTTopology(h, tree, new int[n + 1]));
out.flush();
}
System.out.println(maxBSTTopology(h, tree, new int[n + 1]));
sc.close();
}
// h: 代表当前的头节点

@ -2,26 +2,37 @@ package class18;
// 牛客的测试链接:
// https://www.nowcoder.com/questionTerminal/8ecfe02124674e908b2aae65aad4efdf
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 把如下的全部代码拷贝进java编辑器
// 把文件大类名字改成Main可以直接通过
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Code03_CherryPickup {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
int[][] matrix = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
matrix[i][j] = sc.nextInt();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int N = (int) in.nval;
in.nextToken();
int M = (int) in.nval;
int[][] matrix = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
in.nextToken();
matrix[i][j] = (int) in.nval;
}
}
out.println(cherryPickup(matrix));
out.flush();
}
int ans = cherryPickup(matrix);
System.out.println(ans);
sc.close();
}
public static int cherryPickup(int[][] grid) {

@ -4,32 +4,45 @@ package class18;
// https://www.nowcoder.com/practice/7201cacf73e7495aa5f88b223bbbf6d1
// 不要提交包信息把import底下的类名改成Main提交下面的代码可以直接通过
// 因为测试平台会卡空间所以把set换成了动态加和减的结构
import java.util.Scanner;
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Comparator;
import java.util.HashSet;
import java.util.PriorityQueue;
public class Code04_TopKSumCrossTwoArrays {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int K = sc.nextInt();
int[] arr1 = new int[N];
int[] arr2 = new int[N];
for (int i = 0; i < N; i++) {
arr1[i] = sc.nextInt();
}
for (int i = 0; i < N; i++) {
arr2[i] = sc.nextInt();
}
int[] topK = topKSum(arr1, arr2, K);
for (int i = 0; i < K; i++) {
System.out.print(topK[i] + " ");
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int N = (int) in.nval;
in.nextToken();
int K = (int) in.nval;
int[] arr1 = new int[N];
int[] arr2 = new int[N];
for (int i = 0; i < N; i++) {
in.nextToken();
arr1[i] = (int) in.nval;
}
for (int i = 0; i < N; i++) {
in.nextToken();
arr2[i] = (int) in.nval;
}
int[] topK = topKSum(arr1, arr2, K);
for (int i = 0; i < K; i++) {
out.print(topK[i] + " ");
}
out.println();
out.flush();
}
System.out.println();
sc.close();
}
// 放入大根堆中的结构

@ -1,26 +1,35 @@
package class_2021_12_4_week;
//有m个同样的苹果认为苹果之间无差别
//有n个同样的盘子认为盘子之间也无差别
//还有比如5个苹果如果放进3个盘子
//那么1、3、1和1、1、3和3、1、1的放置方法也认为是一种方法
//如上的设定下,返回有多少种放置方法
//测试链接 : https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
//提交以下的code提交时请把类名改成"Main"
// 有m个同样的苹果认为苹果之间无差别
// 有n个同样的盘子认为盘子之间也无差别
// 还有比如5个苹果如果放进3个盘子
// 那么1、3、1和1、1、3和3、1、1的放置方法也认为是一种方法
// 如上的设定下,返回有多少种放置方法
// 测试链接 : https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 提交以下的code提交时请把类名改成"Main"
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Scanner;
public class Code05_SplitApples {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int m = sc.nextInt();
int n = sc.nextInt();
int ways = ways3(m, n);
System.out.println(ways);
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int m = (int) in.nval;
in.nextToken();
int n = (int) in.nval;
out.println(ways3(m, n));
out.flush();
}
sc.close();
}
// 思路来自于分裂数问题

@ -12,98 +12,45 @@ package class_2022_01_4_week;
// 主商品和附属商品的层级最多有2层
// 给定二维数组things、钱数money返回整体花费不超过money的情况下最大的收益总和
// 测试链接 : https://www.nowcoder.com/practice/f9c6f980eeec43ef85be20755ddbeaf4
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 请把如下的代码的主类名改为"Main", 可以直接通过
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Scanner;
public class Code01_BuyThingsAboutCollocation {
// // index.....货自由选择剩余的钱数是rest
// // 返回:在不花超的情况下,返回最大的价值
// public static int f(int[] prices, int[] values, int index, int rest) {
// if(rest < 0) {
// return -1;
// }
// // rest >= 0
// if(index == prices.length) { // 没货了!
// return 0;
// }
// // rest >=0 有货!
// // 可能1 index.... 自由选择不要index位置的货
// int p1 = f(prices, values, index + 1, rest);
// // 可能性2index.... 自由选择要index位置的货
// int p2 = -1;
// int next = f(prices, values, index + 1, rest - prices[index]);
// if(next != -1) {
// p2 = values[index] + next;
// }
// return Math.max(p1, p2);
// }
//
// // 商品组 主商品重要度3价格9 附件重要度6价格7
// // 0 : [ [3,9], [6,7], [4, 3] ]
// // 1 : [ [4,7] ] 没有附件,只有主商品
// // 2 : [ [5,100] , [2,1] ]
//
// public static int p(int[][][] matrix, int index, int rest) {
// if(rest < 0) {
// return -1;
// }
// if(index == matrix.length) {
// return 0;
// }
// // 有商品组!也有钱>=0;
// int[][] team = matrix[index];
// if(team.length == 1) { // 要 、不要
// int p1 = p(matrix, index + 1, rest);
// int p2 = -1;
// int next = p(matrix, index + 1, rest - team[0][1]);
// if(next != -1) {
// p2 = team[0][0] * team[0][1] + next;
// }
// return Math.max(p1, p2);
// }else if(team.length == 2) { // a b 不要 a ab
// int[] a = team[0];
// int[] b = team[1];
// int p1 = p(matrix, index + 1, rest);
//
// int p2 = -1;// 只要a
// int next2 = p(matrix, index + 1, rest - a[1]);
// if(next2 != -1) {
// p2 = a[0] * a[1] + next2;
// }
//
// int p3 = -1;// 要 ab
// int next3= p(matrix, index + 1, rest - a[1] - b[1]);
// if(next3 != -1) {
// p3 = a[0] * a[1] + b[0] * b[1] + next3;
// }
// return Math.max(p1, Math.max(p2, p3));
// }else { // a : b c 不要 a ab ac abc
//
// }
//
//
//
// }
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int money = sc.nextInt();
int size = sc.nextInt();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int money = (int) in.nval;
in.nextToken();
int size = (int) in.nval;
ArrayList<ArrayList<int[]>> things = new ArrayList<>();
things.add(new ArrayList<>());
for (int i = 0; i < size; i++) {
ArrayList<int[]> cur = new ArrayList<>();
cur.add(new int[] { sc.nextInt(), sc.nextInt(), sc.nextInt() });
in.nextToken();
int a = (int) in.nval;
in.nextToken();
int b = (int) in.nval;
in.nextToken();
int c = (int) in.nval;
cur.add(new int[] { a, b, c });
things.add(cur);
}
int n = clean(things, size);
int ans = maxScore(things, n, money);
System.out.println(ans);
out.println(ans);
out.flush();
}
sc.close();
}
public static int clean(ArrayList<ArrayList<int[]>> things, int size) {

@ -12,50 +12,64 @@ package class_2022_03_1_week;
// 测试链接 : http://poj.org/problem?id=1236
// 注册一下 -> 页面上点击"submit" -> 语言选择java
// 然后把如下代码粘贴进去, 把主类名改成"Main", 可以直接通过
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Scanner;
public class Code02_NetworkOfSchools {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) in.nval;
ArrayList<ArrayList<Integer>> edges = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i <= n; i++) {
edges.add(new ArrayList<Integer>());
}
for (int from = 1; from <= n; from++) {
int to = 0;
while ((to = sc.nextInt()) != 0) {
edges.get(from).add(to);
}
do {
in.nextToken();
int to = (int) in.nval;
if (to == 0) {
break;
} else {
edges.get(from).add(to);
}
} while (true);
}
StronglyConnectedComponents scc = new StronglyConnectedComponents(edges);
int sccn = scc.getSccn();
int[] in = new int[sccn + 1];
int[] out = new int[sccn + 1];
int[] inDegrees = new int[sccn + 1];
int[] outDegrees = new int[sccn + 1];
ArrayList<ArrayList<Integer>> dag = scc.getShortGraph();
for (int i = 1; i <= sccn; i++) {
for (int j : dag.get(i)) {
out[i]++;
in[j]++;
outDegrees[i]++;
inDegrees[j]++;
}
}
int zeroIn = 0;
int zeroOut = 0;
for (int i = 1; i <= sccn; i++) {
if (in[i] == 0) {
if (inDegrees[i] == 0) {
zeroIn++;
}
if (out[i] == 0) {
if (outDegrees[i] == 0) {
zeroOut++;
}
}
System.out.println(zeroIn);
System.out.println(sccn == 1 ? 0 : Math.max(zeroIn, zeroOut));
out.println(zeroIn);
out.println(sccn == 1 ? 0 : Math.max(zeroIn, zeroOut));
out.flush();
}
sc.close();
}
public static class StronglyConnectedComponents {

@ -10,29 +10,42 @@ package class_2022_03_1_week;
// 测试链接 : http://poj.org/problem?id=2186
// 注册一下 -> 页面上点击"submit" -> 语言选择java
// 然后把如下代码粘贴进去, 把主类名改成"Main", 可以直接通过
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Scanner;
public class Code03_PopularCows {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int m = sc.nextInt();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) in.nval;
in.nextToken();
int m = (int) in.nval;
ArrayList<ArrayList<Integer>> edges = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i <= n; i++) {
edges.add(new ArrayList<Integer>());
}
for (int i = 0; i < m; i++) {
int from = sc.nextInt();
int to = sc.nextInt();
in.nextToken();
int from = (int) in.nval;
in.nextToken();
int to = (int) in.nval;
edges.get(from).add(to);
}
StronglyConnectedComponents connectedComponents = new StronglyConnectedComponents(edges);
int sccn = connectedComponents.getSccn();
int ans = 0;
if (sccn == 1) {
System.out.println(n);
ans = n;
} else {
ArrayList<ArrayList<Integer>> dag = connectedComponents.getShortGraph();
int zeroOut = 0;
@ -44,20 +57,19 @@ public class Code03_PopularCows {
}
}
if (zeroOut > 1) {
System.out.println(0);
ans = 0;
} else {
int[] scc = connectedComponents.getScc();
int ans = 0;
for (int i = 1; i <= n; i++) {
if (scc[i] == outScc) {
ans++;
}
}
System.out.println(ans);
}
}
out.println(ans);
out.flush();
}
sc.close();
}
public static class StronglyConnectedComponents {
@ -147,4 +159,4 @@ public class Code03_PopularCows {
}
}
}

@ -2,7 +2,7 @@ package class_2022_03_4_week;
// 来自字节内部训练营
// 某公司游戏平台的夏季特惠开始了你决定入手一些游戏。现在你一共有X元的预算。
// 平台上所有的 n 个游戏均有折扣,标号为 i 的游戏的原价a_i元现价只要b_i元
// 平台上所有的 n 个游戏均有折扣,标号为 i 的游戏的原价a_i元现价只要b_i元
// 也就是说该游戏可以优惠 a_i - b_i并且你购买该游戏能获得快乐值为 w_i
// 由于优惠的存在,你可能做出一些冲动消费导致最终买游戏的总费用超过预算,
// 只要满足 : 获得的总优惠金额不低于超过预算的总金额
@ -11,26 +11,38 @@ package class_2022_03_4_week;
// 测试链接 : https://leetcode-cn.com/problems/tJau2o/
// 提交以下的code将主类名字改成"Main"
// 可以直接通过
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Code02_BuyGoodsHaveDiscount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int money = sc.nextInt();
public class Code02_BuyGoodsHaveDiscount {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) in.nval;
in.nextToken();
int money = (int) in.nval;
int[] costs = new int[n];
long[] values = new long[n];
int size = 0;
long ans = 0;
for (int i = 0; i < n; i++) {
// 打折前
int pre = sc.nextInt();
in.nextToken();
int pre = (int)in.nval;
// 打折后
int pos = sc.nextInt();
in.nextToken();
int pos = (int)in.nval;
// 满足度
int happy = sc.nextInt();
in.nextToken();
int happy = (int)in.nval;
// 节省的钱(save) = 打折前(pre) - 打折后(pos)
int save = pre - pos;
// 带来的好处(well) = 节省的钱 - 打折后(pos)
@ -60,9 +72,9 @@ public class Code02_BuyGoodsHaveDiscount {
}
}
ans += process(costs, values, size, 0, money, dp);
System.out.println(ans);
out.println(ans);
out.flush();
}
sc.close();
}
public static long process(int[] costs, long[] values, int size, int i, int money, long[][] dp) {

@ -6,12 +6,18 @@ package class_2022_03_4_week;
// 这个图中从最初级动物到最顶级捕食者的食物链有几条
// 线上测试链接 : https://www.luogu.com.cn/problem/P4017
// 以下代码都提交,提交时把主类名改成"Main"即可
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 注意洛谷测试平台对java提交非常不友好空间限制可能会卡住C++的提交就完全不会
// 所以提交时如果显示失败,就多提交几次,一定是能成功的
// 这道题本身就是用到拓扑排序,没什么特别的
// 但是为了提交能通过,逼迫我在空间上做的优化值得好好说一下,可以推广到其他题目
import java.util.Arrays;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Code05_HowManyWaysFromBottomToTop {
@ -22,30 +28,31 @@ public class Code05_HowManyWaysFromBottomToTop {
public static int[] queue = new int[5001];
public static int mod = 80112002;
public static int n = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
Arrays.fill(in, 0);
Arrays.fill(out, false);
Arrays.fill(lines, 0);
Arrays.fill(headEdge, 0);
n = sc.nextInt();
int m = sc.nextInt();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer pin = new StreamTokenizer(br);
PrintWriter pout = new PrintWriter(new OutputStreamWriter(System.out));
while (pin.nextToken() != StreamTokenizer.TT_EOF) {
n = (int)pin.nval;
pin.nextToken();
int m = (int)pin.nval;
int[] preEdge = new int[m + 1];
int[] edgesTo = new int[m + 1];
for (int i = 1; i <= m; i++) {
int from = sc.nextInt();
int to = sc.nextInt();
pin.nextToken();
int from = (int)pin.nval;
pin.nextToken();
int to = (int)pin.nval;
edgesTo[i] = to;
preEdge[i] = headEdge[from];
headEdge[from] = i;
out[from] = true;
in[to]++;
}
System.out.println(howManyWays(preEdge, edgesTo));
pout.println(howManyWays(preEdge, edgesTo));
pout.flush();
}
sc.close();
}
public static int howManyWays(int[] preEdge, int[] edgesTo) {

@ -10,27 +10,38 @@ package class_2022_04_2_week;
// 返回这N个小数组中有多少完美对
// 本题测试链接 : https://www.nowcoder.com/practice/f5a3b5ab02ed4202a8b54dfb76ad035e
// 提交如下代码把主类名改成Main
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 可以直接通过
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.Scanner;
public class Code06_PerfectPairNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int m = sc.nextInt();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) in.nval;
in.nextToken();
int m = (int) in.nval;
int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = sc.nextInt();
in.nextToken();
matrix[i][j] = (int)in.nval;
}
}
long ans = perfectPairs(matrix);
System.out.println(ans);
out.println(ans);
out.flush();
}
sc.close();
}
public static long perfectPairs(int[][] matrix) {

@ -3,9 +3,13 @@ package class_2022_07_2_week;
// 本题测试链接 : https://www.luogu.com.cn/problem/P5490
// 提交以下代码,并把主类名改成"Main"
// 可以直接通过
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
@ -21,30 +25,33 @@ public class Code05_LineSweepAlgorithm2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer scanner = new StreamTokenizer(br);
scanner.nextToken();
int n = (int) scanner.nval;
for (int i = 1; i <= n; i++) {
scanner.nextToken();
int x1 = (int) scanner.nval;
scanner.nextToken();
int y1 = (int) scanner.nval;
scanner.nextToken();
int x2 = (int) scanner.nval;
scanner.nextToken();
int y2 = (int) scanner.nval;
orderedY[i] = y1;
orderedY[i + n] = y2;
arr[i][0] = x1;
arr[i][1] = y1;
arr[i][2] = y2;
arr[i][3] = 1;
arr[i + n][0] = x2;
arr[i + n][1] = y1;
arr[i + n][2] = y2;
arr[i + n][3] = -1;
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) in.nval;
for (int i = 1; i <= n; i++) {
in.nextToken();
int x1 = (int) in.nval;
in.nextToken();
int y1 = (int) in.nval;
in.nextToken();
int x2 = (int) in.nval;
in.nextToken();
int y2 = (int) in.nval;
orderedY[i] = y1;
orderedY[i + n] = y2;
arr[i][0] = x1;
arr[i][1] = y1;
arr[i][2] = y2;
arr[i][3] = 1;
arr[i + n][0] = x2;
arr[i + n][1] = y1;
arr[i + n][2] = y2;
arr[i + n][3] = -1;
}
out.println(coverArea(n << 1));
out.flush();
}
System.out.println(coverArea(n << 1));
}
public static long coverArea(int n) {
@ -95,4 +102,4 @@ public class Code05_LineSweepAlgorithm2 {
}
}
}
}
Loading…
Cancel
Save