首页 > 8.22 美团 笔试
头像
zhangzhenallen
编辑于 2020-08-22 18:03
+ 关注

8.22 美团 笔试

4+1道编程题

  1. 没用正则表达式, 暴力法, ac。
  2. 很费解,不知道哪里错了,一直都是54%,,,求大佬指点。

```java
public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    HashMap<Integer,Integer> map = new HashMap<Integer, Integer>();

    for(int i= 1;i<=n;i++){
        int v = sc.nextInt();
        int w = sc.nextInt();
        int price = v+w*2;
        map.put(i,price);
    }

    PriorityQueue<Integer> queue = new PriorityQueue<Integer>((a,b)->{

        if(map.get(a) != map.get(b))  return  map.get(a)- map.get(b);
        return b-a;

    });

    for(int key:map.keySet()){
        queue.offer(key);
        if(queue.size()>m) queue.poll();
    }

    StringBuilder res = new StringBuilder();
    List<Integer> list = new ArrayList<Integer>();
    while(!queue.isEmpty()){
        list.add(queue.poll());
    }
    Collections.sort(list);

    for(int a :list){
        res.append(a+" ");
    }
    System.out.println(res.substring(0, res.length() - 1));

}

}

  1. 题都没读明白, 是这个堆是怎么分呐, 是子堆的两堆,还是一共只有两堆。。。样例给我的感觉是,只有两堆,按这个做的,只a了18%。。。不知道该咋分。。求大佬们指点一下

全部评论

(10) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐