java写入加速

Java刷题的遇到输入106数据的题目用Scanner直接凉凉,在牛客上看到某位大佬的加速外挂,记录一下模板代码。

java写入加速
package my_acm;

import java.io.*;
import java.util.StringTokenizer;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;//InputStream是表示字节输入流的所有类的超类
        OutputStream outputStream = System.out;
        //InputStream与System 没有关系.System.in是System 这个类的静态变量,只是in是InputStream类型的
        
        InputReader sc = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        
        Task solver = new Task();
        solver.solve(sc, out);//这里当作原来的Main函数,输入输出都在里面解决
        
        out.close();//关闭输出流
    }
 
    static class Task {    
        
        public void solve(InputReader scan, PrintWriter out) {
            double ans=0;
            int t=scan.nextInt();
            while(t!=0) {
                t--;
                /*
                 * t组数据输入内容
                 */
            }
            
            while(scan.hasNext()) {
                /*
                 * 多组数据输入内容
                 */
            }
            
            out.println(ans);//输出答案
             
        }
 
    }
 
    
   //自己写出Scanner原本的输入语法,封装在InputReader类里
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
 
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            //32768是输入缓冲区大小,随便设的
            tokenizer = null;
        }
 
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
 
        
        
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
 
        public long nextLong() {
            return Long.parseLong(next());
        }
        
        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public boolean hasNext() {
            try {
                String string = reader.readLine();
                if (string == null) {
                    return false;
                }
                tokenizer = new StringTokenizer(string);
                return tokenizer.hasMoreTokens();
            } catch (IOException e) {
                return false;
            }
        }
        
        public BigInteger nextBigInteger() {//大数
            return new BigInteger(next());
        }
 
    }
}
读写挂模板

 

https://ac.nowcoder.com/acm/contest/3570/L

思路:矩阵前缀和+二分

java写入加速
import java.io.*;
import java.util.StringTokenizer;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;//InputStream是表示字节输入流的所有类的超类
        OutputStream outputStream = System.out;
        //InputStream与System 没有关系.System.in是System 这个类的静态变量,只是in是InputStream类型的
        
        InputReader sc = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        
        Task solver = new Task();
        solver.solve(sc, out);
        
        out.close();
    }
 
    static class Task {    
        public boolean check(long[][] dp,int m,long max,int n) {
            for(int i=1;n-i+1>=m;i++) {
                for(int j=1;n-j+1>=m;j++) {
                    if(dp[i+m-1][j+m-1]-dp[i+m-1][j-1]-dp[i-1][j+m-1]+dp[i-1][j-1]>=max)
                        return true;
                }
            }
            return false;
        }
         
        public void solve(InputReader sc, PrintWriter out) {
            int n=sc.nextInt();
            long k=sc.nextLong();
            
            long[][] dp=new long[n+1][n+1];
            long[][] v=new long[n+1][n+1];
             
            for(int i=1;i<=n;i++) {
                for(int j=1;j<=n;j++)
                    v[i][j]=sc.nextLong();
            }
            for(int i=1;i<=n;i++)//矩阵前缀和
                for(int j=1;j<=n;j++)
                    dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+v[i][j];
            int ans=-1;
            int l=1;
            int r=n;
            while(l<=r) {//二分
                int mid=(l+r)/2;
                if(check(dp,mid,k,n)) {
                    ans=mid;
                    r=mid-1;
                }
                else
                    l=mid+1;
            }
            if(ans==-1) {
                out.println("I'm a Gold Chef!");
                return ;
            }
            out.println(ans);
             
        }
 
    }
 
    
   //自己写出Scanner原本的输入语法,封装在InputReader类里
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
 
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
 
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
 
        
        
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
 
        public long nextLong() {
            return Long.parseLong(next());
        }
        
        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public boolean hasNext() {
            try {
                String string = reader.readLine();
                if (string == null) {
                    return false;
                }
                tokenizer = new StringTokenizer(string);
                return tokenizer.hasMoreTokens();
            } catch (IOException e) {
                return false;
            }
        }
        
         
        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }
 
    }
}

L
View Code

 

https://ac.nowcoder.com/acm/contest/3402/L

思路:贪心

java写入加速
package my_acm;

import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;//InputStream是表示字节输入流的所有类的超类
        OutputStream outputStream = System.out;
        //InputStream与System 没有关系.System.in是System 这个类的静态变量,只是in是InputStream类型的
        
        InputReader scan = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        
        Task solver = new Task();
        solver.solve(scan, out);//这里当作原来的Main函数,输入输出都在里面解决
        
        out.close();//关闭输出流
    }
 
    static class Task {    
        
        public void solve(InputReader scan, PrintWriter out) {
            while(scan.hasNext()) {
                long m;
                int n=scan.nextInt();
                m=scan.nextLong();
                long [] c=new long[n+1];
                long sumb=0,suma=0;
                for(int i=0;i<n;i++) {
                    long a,b;
                    a=scan.nextLong();
                    b=scan.nextLong();
                    c[i]=a-b;
                    sumb+=b;
                    suma+=a;
                }
                int ans=0;//压缩数量
                if(sumb>m)
                    ans=-1;
                else{
                    
                    Arrays.sort(c,0,n);
                    for(int i=n-1;i>=0;i--) {
                        if(suma<=m) {
                            break;
                        }
                        suma-=c[i];
                        ans++;
                    }
                }
                out.println(ans);
             
        }
 
    }
}
    
   //自己写出Scanner原本的输入语法,封装在InputReader类里
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
 
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            //32768是输入缓冲区大小,随便设的
            tokenizer = null;
        }
 
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
 
        
        
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
 
        public long nextLong() {
            return Long.parseLong(next());
        }
        
        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public boolean hasNext() {
            try {
                String string = reader.readLine();
                if (string == null) {
                    return false;
                }
                tokenizer = new StringTokenizer(string);
                return tokenizer.hasMoreTokens();
            } catch (IOException e) {
                return false;
            }
        }
        
        public BigInteger nextBigInteger() {//大数
            return new BigInteger(next());
        }
 
    }
}
View Code
上一篇:获取请求体中图片的二进制流直接解析成图片


下一篇:20200125——mybatis整合复习