codeforces上的一个简单问题在第7个测试时出错的原因
我在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个测试的用例和结果
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后用随机数测试发现了问题,len不应该在if里面重置为1
跑通了前6个测试真是“凑巧”
出错的原因当然是写错啦,你可以选择自己思考下,当然也可以选择看下别人的代码,最后CF是看不了全部数据的
https://www.quora.com/How-do-...