首页 > 代码库 > PAT 1029

PAT 1029

感觉已经没啥心情用JAVA和PAT斗智斗勇了,会内存超限,是不是读进来就内存超限了?

二分做的方法有点bug,又写了一个merge sort的,想着百万数量级应该不会超时吧这都超时也太蛋疼了,结果居然内存爆了...

 

 1 import java.util.*; 2 import java.io.*; 3  4 class FastReader{ 5     BufferedReader reader; 6     StringTokenizer tokenizer; 7      8     public FastReader(InputStream stream){ 9         reader = new BufferedReader(new InputStreamReader(stream), 1 << 10);10         tokenizer = null;11     }12     13     public String next(){14         while (tokenizer == null || !tokenizer.hasMoreTokens()){15             try{16                 tokenizer = new StringTokenizer(reader.readLine());17             } catch (Exception e){18                 throw new RuntimeException(e);19             }20         }21         22         return tokenizer.nextToken();23     }24     25     public long next_long(){26         return Long.parseLong(next());27     }28     29     public int next_int(){30         return Integer.parseInt(next());31     }32 }33 34 public class Main{35     static long[] seq_a;36     static long[] seq_b;37     38     public static void main(String[] args){39         FastReader reader = new FastReader(System.in);40         int N;41         42         N = reader.next_int();43         seq_a = new long[(int) N];44         for (int i = 0; i < N; i++)45             seq_a[i] = reader.next_long();46         47         N = reader.next_int();48         seq_b = new long[(int) N];49         for (int i = 0; i < N; i++)50             seq_b[i] = reader.next_long();51         52         int total = seq_a.length + seq_b.length;53         int target = ((total & 0x1) != 0) ? total / 2 + 1 : total / 2;54         55         int pa = 0, pb = 0;56         int cnt = 1;57         while (cnt < target){58             if (seq_a[pa] < seq_b[pb])59                 pa++;60             else61                 pb++;62             63             cnt++;64         }65         66         long ans = Math.min(seq_a[pa], seq_b[pb]);67         System.out.println(ans);68     }69 }

 

PAT,想说爱你不容易... 既然这么不待见JAVA为什么还给JAVA这么严苛的条件,给C++倒是很宽嘛,不用二分做都能过的时间... 

PAT 1029