4.19小红书第二题
import java.util.*;
public class Main {
public static long ans = 0;
public static List<Long> 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<Long, Long> map = new HashMap<>();
List<Long> 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<Long> 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<Long, Long> map = new HashMap<>();
List<Long> 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 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啊,看不出哪里的问题
相关推荐
点赞 评论 收藏
分享
09-11 17:25
浙江工商大学 游戏测试 点赞 评论 收藏
分享
10-17 09:06
门头沟学院 Java LZStarV:(这居然能又刷到你)感觉真的就是岗位少了吧,项目很多27届这个点都差不多这个水准,只能说有点不赶趟了
先慢慢投吧兄弟,啥都投一下总有机会的
点赞 评论 收藏
分享
万物DP:你这写的太多了,我看了看其实很多也没亮点
点赞 评论 收藏
分享