codeforces上的一个简单问题在第7个测试时出错的原因

发布于 2022-09-05 19:28:45 字数 2039 浏览 10 评论 0

我在codeforces上面做了一道很简单的题目用做热身,但是测试跑到第7个的时候出现了错误,本来是可以看到测试用例的,但是这条测试数据太长了,到后面我只能看到省略号……T^T

题目:
Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1 ≤ i ≤ n) he makes ai money. Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence ai. Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.

Help Kefa cope with this task!

Input
The first line contains integer n (1 ≤ n ≤ 105).

The second line contains n integers a1,  a2,  ...,  an (1 ≤ ai ≤ 109).

Output
Print a single integer — the length of the maximum non-decreasing subsegment of sequence a.

大致意思就是给我一个数列,需要找出该数列里最长非减子数列

代码:

import java.util.Scanner;

/**
 * 
 * A. Kefa and First Steps
 * http://codeforces.com/problemset/problem/580/A
 */
public class A580 {

    
    public static void main(String[] args) {
        
        codeforces();
        
    }
    
    public static void codeforces() {
        
        Scanner in = new Scanner(System.in);
        int cnt = in.nextInt();
        
        int res = 1;
        int lastOne = 0;
        int len = 0;
        
        for(int i=0; i<cnt; i++) {
            int tmp = in.nextInt();
            
            if(lastOne <= tmp) {
                len++;
                
            } else {
                if(res < len) {
                    res = len;
                    len = 1;
                }
            }
            
            lastOne = tmp;
        }
        
        if(res < len) {
            res = len;
        }
        
        System.out.println(res);
        
    
    }
    
    
}

下面是第7个测试的用例和结果

clipboard.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半葬歌 2022-09-12 19:28:45

最后用随机数测试发现了问题,len不应该在if里面重置为1
跑通了前6个测试真是“凑巧”

烦人精 2022-09-12 19:28:45

出错的原因当然是写错啦,你可以选择自己思考下,当然也可以选择看下别人的代码,最后CF是看不了全部数据的
https://www.quora.com/How-do-...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文