java ACM竞赛IO优化Petr模板

相信ACMer 用java的时候不少遇到超时的现象吧,今天做CF的时候就遇到了,$10^5$ Int 就超1s了,根本不能忍,这里留下一个petr 大佬的模板,果然速度提高10倍

  • 输入
  • 输出
  • 例子

输入

class InputReader{
    private final static int BUF_SZ = 65536;
    BufferedReader in;
    StringTokenizer tokenizer;
    public InputReader(InputStream in) {
        super();
        this.in = new BufferedReader(new InputStreamReader(in),BUF_SZ);
        tokenizer = new StringTokenizer("");
    }
    private String next() {
        while (!tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(in.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }
    public int nextInt() {
        return Integer.parseInt(next());
    }
}

StringTokenizer
BufferedReader

输出

输出比较简单直接使用 PrintWriter


    public static InputReader in = new InputReader(System.in);
    public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

例子

CF 980 C. Posterized

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;




public class Main {
    public static final int INF = 0x3f3f3f3f;
    public static InputReader in = new InputReader(System.in);
    public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) {
        int n = in.nextInt();
        int k = in.nextInt();
        int[] p = new int[n];
        for(int i=0 ; inew Solution(n, k, p);
        solver.solve();
        for(int i=0 ; i" ");
        out.flush();
        out.close();
    }
}

class Solution{
    int n,k;
    int[] p;
    int[] ans;
    public Solution(int n, int k, int[] p) {
        super();
        this.n = n;
        this.k = k;
        this.p = p;
        ans = new int[256];
        Arrays.fill(ans, -1);
    }

    public void solve() {
        ans[0] =0;
        for(int i=0 ; iint ret =0;
            if(ans[p[i]]!=-1)continue;
            int lower = Math.max(-1, p[i]-k);
            for(ret = p[i] ; ret >lower ; --ret){
                if(ans[ret] !=-1){
                    if(ans[ret] > lower){
                        ret = ans[ret];
                    }else ret++;
                    break;
                }
            }
            ret = Math.max(ret, lower+1);
            for(int j=ret ; j<=p[i] ; ++j)ans[j] = ret;
        }
    }
}

class InputReader{
    private final static int BUF_SZ = 65536;
    BufferedReader in;
    StringTokenizer tokenizer;
    public InputReader(InputStream in) {
        super();
        this.in = new BufferedReader(new InputStreamReader(in),BUF_SZ);
        tokenizer = new StringTokenizer("");
    }
    private String next() {
        while (!tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(in.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }
    public int nextInt() {
        return Integer.parseInt(next());
    }
}

你可能感兴趣的:(misc)