4.19小红书第二题
import java.util.*;
public class Main {
public static long ans = 0;
public static List list = new ArrayList<>();
public static void main(String args[]) {
//4 2
//1 2 3 4
//3 4 2 1
Scanner in = new Scanner(System.in);
long n = in.nextLong();
long k = in.nextLong();
Map map = new HashMap<>();
List mark = new ArrayList<>();
for (int i = 0; i < n; i++) {
long x1 = in.nextLong();
mark.add(x1);
}
for (int i = 0; i < n; i++) {
long x2 = in.nextLong();
map.put(mark.get(i), x2);
}
boolean[] visited = new boolean[(int) n];
dfs(k, map, mark, visited, n,-1);
System.out.println(ans);
}
public class Main {
public static long ans = 0;
public static List
public static void main(String args[]) {
//4 2
//1 2 3 4
//3 4 2 1
Scanner in = new Scanner(System.in);
long n = in.nextLong();
long k = in.nextLong();
Map
List
for (int i = 0; i < n; i++) {
long x1 = in.nextLong();
mark.add(x1);
}
for (int i = 0; i < n; i++) {
long x2 = in.nextLong();
map.put(mark.get(i), x2);
}
boolean[] visited = new boolean[(int) n];
dfs(k, map, mark, visited, n,-1);
System.out.println(ans);
}
全部评论
public static void dfs(long k, Map<Long, Long> map, List<Long> mark, boolean[] visited, long n,long now) {
if (list.size() == k) {
long sum = 0, value = Integer.MAX_VALUE;
for (int i = 0; i < k; i++) {
sum += list.get(i);
value = Math.min(value, map.get(list.get(i)));
}
ans = Math.max(ans, sum * value);
// System.out.println(list);
}
for (int i = 0; i < n; i++) {
if (visited[i]||now>=i) continue;
if (!visited[i]) {
list.add(mark.get(i));
visited[i] = true;
dfs(k, map, mark, visited, n,i);
if (!list.isEmpty()) list.remove(list.size()-1);
visited[i] = false;
}
}
}
}
接着上面的,有字数限制,回溯为什么只过了35啊,看不出哪里的问题
相关推荐
11-19 12:10
湖南财政经济学院 Java 点赞 评论 收藏
分享