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 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啊,看不出哪里的问题
点赞 回复 分享
发布于 2024-04-19 21:05 吉林

相关推荐

05-22 09:23
门头沟学院 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-27 15:19
简历上能写3个月吗?
码农索隆:大胆写,主要你能把实习经历包装好,可以看一下我这篇帖子https://www.nowcoder.com/share/jump/4888395581180798063
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务