【洛谷OJ】P1567 统计天数

36 篇文章 0 订阅

练习P1567时,由于Scanner的处理效率影响,导致提交后有5个TLE~,优化后采用BufferedReader结合StringTokenizer提供效率,可以全部通过,关于StringTokenizer,源码及批注参见:https://mp.csdn.net/postedit/81813045

目录

StringTokenizer版本:

String结合数组版本

Scanner版本:


StringTokenizer版本:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    private static CustomizedReader cin;

    public static void main(String args[]) throws Exception {
        cin = new CustomizedReader(System.in);
        int n = cin.nextInt();
        if(n<=0 || n>Math.pow(10, 7)) {
            return;
        }
        int historyRecord = 0;
        int newRecord = 0;
        int startTemprature = -1;
        int MAX = (int) Math.pow(10, 9);
        for (int i = 0; i < n; i++) {
            int t = cin.nextInt();
            if(t>=0&&t<=MAX) {
                if(t > startTemprature) {
                    newRecord++;
                    startTemprature = t;
                }else {
                    if(newRecord > historyRecord) {
                        historyRecord = newRecord;
                    }
                    newRecord = 1;
                    startTemprature = t;
                }
            }
        }
        if(newRecord > historyRecord) {
            historyRecord = newRecord;
        }
        System.out.println(historyRecord);
    }
}

class CustomizedReader {
    private BufferedReader reader = null;
    private StringTokenizer tokenizer;
    
    CustomizedReader(InputStream source){
        reader = new BufferedReader(new InputStreamReader(source) );
        tokenizer = new StringTokenizer("");
    }
    
    public String next() {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            try {
                tokenizer = new StringTokenizer(reader.readLine() );
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return tokenizer.nextToken();
        
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public double nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextFloat() {
        return Float.parseFloat(next());
    }
    
}    

 

String结合数组版本

class CustomizedReader {
    private BufferedReader reader = null;
    private String[] list= null;
    private int length = 0;
    private int currentPosition = 0;
    
    CustomizedReader(InputStream source){
        reader = new BufferedReader(new InputStreamReader(source) );
    }
    
    public String next() {
        if(currentPosition >= length) {
            try {
                list = reader.readLine().split("\\s");
                length = list.length;
                currentPosition = 0;
                if(length <= 0) {
                    next();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        return list[currentPosition++];
        
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }

public double nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextFloat() {
        return Float.parseFloat(next());
    }

}


Scanner版本:

import java.util.Scanner;

public class Main {
    private static Scanner cin;

    public static void main(String args[]) throws Exception {
        cin = new Scanner(System.in);
        int n = cin.nextInt();
        if(n<=0 || n>Math.pow(10, 7)) {
            return;
        }
        int historyRecord = 0;
        int newRecord = 0;
        int startTemprature = -1;
        int MAX = (int) Math.pow(10, 9);
        for (int i = 0; i < n; i++) {
            int t = cin.nextInt();
            if(t>=0&&t<=MAX) {
                if(t > startTemprature) {
                    newRecord++;
                    startTemprature = t;
                }else {
                    if(newRecord > historyRecord) {
                        historyRecord = newRecord;
                    }
                    newRecord = 1;
                    startTemprature = t;
                }
            }
        }
        if(newRecord > historyRecord) {
            historyRecord = newRecord;
        }
        System.out.println(historyRecord);
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值