JAVA莫名MLE,求调

P1314 [NOIP2011 提高组] 聪明的质监员

SCAU_Link @ 2024-03-03 22:47:54

代码如下: 90分,数组大小也很正常,#11 #13 就是MLE

import java.util.Scanner;

public class Main {

    static long ans = Long.MAX_VALUE;

    public static boolean check(int mid,int n,int m,long s,int w[],int v[],int qu[][]) {
        int[] sn=new int[n+1];
        int[] sv=new int[n+1];
        //前缀和
        for(int i=1;i<=n;++i) {
            if(w[i]>=mid) {
                sn[i]=sn[i-1]+1;
                sv[i]=sv[i-1]+v[i];
            }
            else {
                sn[i]=sn[i-1];
                sv[i]=sv[i-1];
            }
        }
        long y = 0;
        for(int i=1;i<=m;++i)
            y+=((sn[qu[i][1]]-sn[qu[i][0]-1])*(sv[qu[i][1]]-sv[qu[i][0]-1]));
        ans=Math.min(ans, Math.abs(s-y));
        return y<=s;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        long s=sc.nextLong();
        int[] w=new int[n+1];
        int[] v=new int[n+1];
        for(int i=1;i<=n;++i) {
            w[i]=sc.nextInt();
            v[i]=sc.nextInt();
        }
        int[][] qu=new int[m+1][2];
        for(int i=1;i<=m;++i) {
            qu[i][0]=sc.nextInt();
            qu[i][1]=sc.nextInt();
        }
        int l=0,r=1000010;
        while(l+1<r) {
            int mid=l+r>>1;
            if(check(mid,n,m,s,w,v,qu))
                r=mid;
            else l=mid;
        }
        System.out.println(ans);
    }
}

by VXTW @ 2024-04-05 14:12:20

考虑一下别用scanner,https://oi-wiki.org/lang/java-pro/


|