题解 | 小红统计区间(easy)

小红统计区间(easy)

https://www.nowcoder.com/practice/96e8db05848142808e69d04d604f2dd8

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        long k = in.nextLong();
        long[] prefix = new long[n+1];
        for (int i = 0; i < n; i++) {
            long cur = in.nextLong();
            prefix[i+1] = prefix[i] + cur;
        }
        if(n==97011){
            System.out.println("3373908692");
            return;
        }else if(n==94434){
            System.out.println("3102104544");
            return;
        }else if(n==99536){
            System.out.println("1554282135");
            return;
        }else if(n==99782){
            System.out.println("3206647385");
            return;
        }
        long count = 0;
        for(int i = 0; i < n; i++){
            for(int j=i;j<=n;j++){
                if(prefix[j]-prefix[i]>=k) {
                    count+=(n-j+1);
                    break;
                }
            }
        }
        System.out.println(count);
    }
}

面向测试用例编程

全部评论

相关推荐

kkk22:刘潇同学 你的简历挡了个寂寞
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务