滴滴9月17第二题ak代码,差分数组

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(); int p=sc.nextInt(); int q=sc.nextInt();
        sc.nextLine();
        int[][] a=new int[n][3];
        a[0]=Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        a[1]=Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        a[2]=Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        Map<Integer,int[]> map=new HashMap<>();
        for(int i=0;i<n;i++){
            int[] tmp1=map.getOrDefault(a[0][i]-1, new int[2]);
            int[] tmp2=map.getOrDefault(a[1][i], new int[2]);
            tmp1[a[2][i]-1]--;
            tmp2[a[2][i]-1]++;
            map.put(a[0][i]-1, tmp1);
            map.put(a[1][i], tmp2);
        }
        List<Map.Entry<Integer,int[]>> list=new ArrayList<>();
        for(Map.Entry<Integer,int[]> v:map.entrySet()){
            list.add(v);
        }
        Collections.sort(list,(x, y)->{
            return y.getKey()-x.getKey();
        });
        // System.out.println(list.size());
        int res=0;
        int curp=0, curq=0;
        int lastk=-1;
        for(Map.Entry<Integer,int[]> v : list){
            int tmpk=v.getKey();
            //System.out.println(tmpk);
            int[] tmpv=v.getValue();
            if(lastk==-1){
                curp+=tmpv[0];
                curq+=tmpv[1];
                lastk=tmpk;
            }else{
                if(curp>=p&&curq>=q){
                    res+=lastk-tmpk;
                }
                curp+=tmpv[0];
                curq+=tmpv[1];
                lastk=tmpk;
            }
        }
        System.out.println(res);
    }
}

全部评论
差分离散化😖忘记离散化了
点赞 回复 分享
发布于 2022-09-17 16:52 陕西

相关推荐

wu970:标准北漂配置,怎么看着装修风格有点像自如的😭
点赞 评论 收藏
分享
996的工作制还是没能硬啃下去,快要面试怂了,取消了
牛客80700350...:很正常,不是所有人都能接受这种强度的。不叫怯战,这叫明智
点赞 评论 收藏
分享
评论
2
3
分享

创作者周榜

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