You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Codeforces 1921-C单测例正常多测例出错问题求助

Codeforces 1921-C 多测试用例运行异常问题

我在做Codeforces 1921-C题时遇到一个问题:单个测试用例跑结果正确,但把多个测试用例一起输入时结果就错了。

针对这个测试用例:

12 621526648 2585904 3566299
51789 61859 71998 73401 247675 298086 606959 663464 735972 806043 806459 919683

正确输出应该是“Yes”,但当它和其他测试用例一起运行时,输出变成了“No”。我的代码如下:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int test = sc.nextInt();

        for (int t_ = 0; t_ < test; t_++) {
            long n = sc.nextLong();
            long f = sc.nextLong();
            long a = sc.nextLong();
            long b = sc.nextLong();
            long now = 0L;

            for (int i = 0; i < n; i++){
                long m = sc.nextLong();
                long min = Math.min(a*(m - now), b);
                f = f - min;
                if(f <= 0) {
                    System.out.println("No");
                    break;
                }
                now = m;
            }
            if(f > 0) {
                System.out.println("Yes");
            }
        }
    }
}

问题原因

当某个测试用例中f <= 0时,直接break跳出内部循环,但当前测试用例剩下的数字还没被读取,这些未读的输入会被下一个测试用例当成自己的参数读取,导致后续测试用例的输入完全混乱,最终输出错误结果。

解决方法

在输出“No”并break后,必须把当前测试用例剩下的所有数字全部读完,确保输入指针走到下一个测试用例的起始位置。

修改后的代码(写法一):

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int test = sc.nextInt();

        for (int t_ = 0; t_ < test; t_++) {
            long n = sc.nextLong();
            long f = sc.nextLong();
            long a = sc.nextLong();
            long b = sc.nextLong();
            long now = 0L;
            boolean possible = true;

            for (int i = 0; i < n; i++){
                long m = sc.nextLong();
                if (!possible) {
                    continue; // 已判定不可行,继续读取剩余数字跳过
                }
                long min = Math.min(a*(m - now), b);
                f -= min;
                if(f <= 0) {
                    possible = false;
                }
                now = m;
            }
            System.out.println(possible ? "Yes" : "No");
        }
    }
}

修改后的代码(写法二):

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int test = sc.nextInt();

        for (int t_ = 0; t_ < test; t_++) {
            long n = sc.nextLong();
            long f = sc.nextLong();
            long a = sc.nextLong();
            long b = sc.nextLong();
            long now = 0L;
            boolean ok = true;

            for (int i = 0; i < n; i++){
                long m = sc.nextLong();
                long min = Math.min(a*(m - now), b);
                f -= min;
                if(f <= 0) {
                    ok = false;
                    // 主动读取剩余的所有数字
                    for(int j = i+1; j < n; j++){
                        sc.nextLong();
                    }
                    break;
                }
                now = m;
            }
            System.out.println(ok ? "Yes" : "No");
        }
    }
}

内容的提问来源于stack exchange,提问作者sheafifier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 15:22:45