java写入加速

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

1package my_acm; 2 3import java.io.*; 4import java.util.StringTokenizer; 5import java.math.BigInteger; 6 7public class Main { 8 public static void main(String[] args) { 9 InputStream inputStream = System.in;//InputStream是表示字节输入流的所有类的超类 10 OutputStream outputStream = System.out; 11 //InputStream与System 没有关系.System.in是System 这个类的静态变量,只是in是InputStream类型的 12 13 InputReader sc = new InputReader(inputStream); 14 PrintWriter out = new PrintWriter(outputStream); 15 16 17 Task solver = new Task(); 18 solver.solve(sc, out);//这里当作原来的Main函数,输入输出都在里面解决 19 20 out.close();//关闭输出流 21 } 22 23 static class Task { 24 25 public void solve(InputReader scan, PrintWriter out) { 26 double ans=0; 27 int t=scan.nextInt(); 28 while(t!=0) { 29 t--; 30 /* 31 * t组数据输入内容 32 */ 33 } 34 35 while(scan.hasNext()) { 36 /* 37 * 多组数据输入内容 38 */ 39 } 40 41 out.println(ans);//输出答案 42 43 } 44 45 } 46 47 48 //自己写出Scanner原本的输入语法,封装在InputReader类里 49 static class InputReader { 50 public BufferedReader reader; 51 public StringTokenizer tokenizer; 52 53 public InputReader(InputStream stream) { 54 reader = new BufferedReader(new InputStreamReader(stream), 32768); 55 //32768是输入缓冲区大小,随便设的 56 tokenizer = null; 57 } 58 59 public String next() { 60 while (tokenizer == null || !tokenizer.hasMoreTokens()) { 61 try { 62 tokenizer = new StringTokenizer(reader.readLine()); 63 } catch (IOException e) { 64 throw new RuntimeException(e); 65 } 66 } 67 return tokenizer.nextToken(); 68 } 69 70 71 72 73 public int nextInt() { 74 return Integer.parseInt(next()); 75 } 76 77 public long nextLong() { 78 return Long.parseLong(next()); 79 } 80 81 public double nextDouble() { 82 return Double.parseDouble(next()); 83 } 84 85 public boolean hasNext() { 86 try { 87 String string = reader.readLine(); 88 if (string == null) { 89 return false; 90 } 91 tokenizer = new StringTokenizer(string); 92 return tokenizer.hasMoreTokens(); 93 } catch (IOException e) { 94 return false; 95 } 96 } 97 98 public BigInteger nextBigInteger() {//大数 99 return new BigInteger(next()); 100 } 101 102 } 103}

读写挂模板

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

思路:矩阵前缀和+二分

1import java.io.*; 2import java.util.StringTokenizer; 3import java.math.BigInteger; 4 5public class Main { 6 public static void main(String[] args) { 7 InputStream inputStream = System.in;//InputStream是表示字节输入流的所有类的超类 8 OutputStream outputStream = System.out; 9 //InputStream与System 没有关系.System.in是System 这个类的静态变量,只是in是InputStream类型的 10 11 InputReader sc = new InputReader(inputStream); 12 PrintWriter out = new PrintWriter(outputStream); 13 14 15 Task solver = new Task(); 16 solver.solve(sc, out); 17 18 out.close(); 19 } 20 21 static class Task { 22 public boolean check(long[][] dp,int m,long max,int n) { 23 for(int i=1;n-i+1>=m;i++) { 24 for(int j=1;n-j+1>=m;j++) { 25 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) 26 return true; 27 } 28 } 29 return false; 30 } 31 32 public void solve(InputReader sc, PrintWriter out) { 33 int n=sc.nextInt(); 34 long k=sc.nextLong(); 35 36 long[][] dp=new long[n+1][n+1]; 37 long[][] v=new long[n+1][n+1]; 38 39 for(int i=1;i<=n;i++) { 40 for(int j=1;j<=n;j++) 41 v[i][j]=sc.nextLong(); 42 } 43 for(int i=1;i<=n;i++)//矩阵前缀和 44 for(int j=1;j<=n;j++) 45 dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+v[i][j]; 46 int ans=-1; 47 int l=1; 48 int r=n; 49 while(l<=r) {//二分 50 int mid=(l+r)/2; 51 if(check(dp,mid,k,n)) { 52 ans=mid; 53 r=mid-1; 54 } 55 else 56 l=mid+1; 57 } 58 if(ans==-1) { 59 out.println("I'm a Gold Chef!"); 60 return ; 61 } 62 out.println(ans); 63 64 } 65 66 } 67 68 69 //自己写出Scanner原本的输入语法,封装在InputReader类里 70 static class InputReader { 71 public BufferedReader reader; 72 public StringTokenizer tokenizer; 73 74 public InputReader(InputStream stream) { 75 reader = new BufferedReader(new InputStreamReader(stream), 32768); 76 tokenizer = null; 77 } 78 79 public String next() { 80 while (tokenizer == null || !tokenizer.hasMoreTokens()) { 81 try { 82 tokenizer = new StringTokenizer(reader.readLine()); 83 } catch (IOException e) { 84 throw new RuntimeException(e); 85 } 86 } 87 return tokenizer.nextToken(); 88 } 89 90 91 92 93 public int nextInt() { 94 return Integer.parseInt(next()); 95 } 96 97 public long nextLong() { 98 return Long.parseLong(next()); 99 } 100 101 public double nextDouble() { 102 return Double.parseDouble(next()); 103 } 104 105 public boolean hasNext() { 106 try { 107 String string = reader.readLine(); 108 if (string == null) { 109 return false; 110 } 111 tokenizer = new StringTokenizer(string); 112 return tokenizer.hasMoreTokens(); 113 } catch (IOException e) { 114 return false; 115 } 116 } 117 118 119 public BigInteger nextBigInteger() { 120 return new BigInteger(next()); 121 } 122 123 } 124} 125 126L

View Code

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

思路:贪心

1package my_acm; 2 3import java.io.*; 4import java.util.Arrays; 5import java.util.StringTokenizer; 6import java.math.BigInteger; 7 8public class Main { 9 public static void main(String[] args) { 10 InputStream inputStream = System.in;//InputStream是表示字节输入流的所有类的超类 11 OutputStream outputStream = System.out; 12 //InputStream与System 没有关系.System.in是System 这个类的静态变量,只是in是InputStream类型的 13 14 InputReader scan = new InputReader(inputStream); 15 PrintWriter out = new PrintWriter(outputStream); 16 17 18 Task solver = new Task(); 19 solver.solve(scan, out);//这里当作原来的Main函数,输入输出都在里面解决 20 21 out.close();//关闭输出流 22 } 23 24 static class Task { 25 26 public void solve(InputReader scan, PrintWriter out) { 27 while(scan.hasNext()) { 28 long m; 29 int n=scan.nextInt(); 30 m=scan.nextLong(); 31 long [] c=new long[n+1]; 32 long sumb=0,suma=0; 33 for(int i=0;i<n;i++) { 34 long a,b; 35 a=scan.nextLong(); 36 b=scan.nextLong(); 37 c[i]=a-b; 38 sumb+=b; 39 suma+=a; 40 } 41 int ans=0;//压缩数量 42 if(sumb>m) 43 ans=-1; 44 else{ 45 46 Arrays.sort(c,0,n); 47 for(int i=n-1;i>=0;i--) { 48 if(suma<=m) { 49 break; 50 } 51 suma-=c[i]; 52 ans++; 53 } 54 } 55 out.println(ans); 56 57 } 58 59 } 60} 61 62 //自己写出Scanner原本的输入语法,封装在InputReader类里 63 static class InputReader { 64 public BufferedReader reader; 65 public StringTokenizer tokenizer; 66 67 public InputReader(InputStream stream) { 68 reader = new BufferedReader(new InputStreamReader(stream), 32768); 69 //32768是输入缓冲区大小,随便设的 70 tokenizer = null; 71 } 72 73 public String next() { 74 while (tokenizer == null || !tokenizer.hasMoreTokens()) { 75 try { 76 tokenizer = new StringTokenizer(reader.readLine()); 77 } catch (IOException e) { 78 throw new RuntimeException(e); 79 } 80 } 81 return tokenizer.nextToken(); 82 } 83 84 85 86 87 public int nextInt() { 88 return Integer.parseInt(next()); 89 } 90 91 public long nextLong() { 92 return Long.parseLong(next()); 93 } 94 95 public double nextDouble() { 96 return Double.parseDouble(next()); 97 } 98 99 public boolean hasNext() { 100 try { 101 String string = reader.readLine(); 102 if (string == null) { 103 return false; 104 } 105 tokenizer = new StringTokenizer(string); 106 return tokenizer.hasMoreTokens(); 107 } catch (IOException e) { 108 return false; 109 } 110 } 111 112 public BigInteger nextBigInteger() {//大数 113 return new BigInteger(next()); 114 } 115 116 } 117}

View Code

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Android So动态加载 优雅实现与原理分析

背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我