Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Main {
- private static int Max = 111;
- public static void main(String[] args) throws Exception {
- //Reader reader = new Reader().InitConsoleReading();
- Reader reader = new Reader().InitFileReading("speeding.in");
- //Reader reader = new Reader().InitFileReading("D:\\Work\\SourceTree\\Algorithm\\io\\in.txt");
- //Writer writer = new Writer().InitConsoleWriting();
- Writer writer = new Writer().InitFileWriting("speeding.out");
- //Writer writer = new Writer().InitFileWriting("D:\\Work\\SourceTree\\Algorithm\\io\\out.txt");
- int[] limitsD = new int[Max];
- int[] limitsV = new int[Max];
- int[] drivesD = new int[Max];
- int[] drivesV = new int[Max];
- int n, m;
- n = reader.nextInt();
- m = reader.nextInt();
- for (int i = 0; i < n; i++) {
- limitsD[i] = reader.nextInt();
- limitsV[i] = reader.nextInt();
- }
- for (int i = 0; i < m; i++) {
- drivesD[i] = reader.nextInt();
- drivesV[i] = reader.nextInt();
- }
- for (int i = 1; i < n; i++) {
- drivesD[i] += drivesD[i - 1];
- }
- int px = 0,max = 0;
- int lmtSt = 1;
- int lmtEd = limitsD[0];
- for (int i = 0; i < m; i++) {
- int st = i == 0 ? 0 : drivesD[i - 1] + 1;
- int ed = drivesD[i]; //segment to be covered
- while (true) {
- if (overlaps(lmtSt, lmtEd, st, ed)) {
- max = Math.max(max, drivesV[i] - limitsV[px]);
- if (partiallyCovered(lmtSt, lmtEd, st, ed)) {
- px++;
- lmtSt = lmtEd + 1;
- lmtEd += limitsD[px];
- } else break;
- } else if (isLeft(lmtSt, lmtEd, st, ed)) {
- px++;
- lmtSt = lmtEd + 1;
- lmtEd += limitsD[px];
- } else {
- break;
- }
- }
- }
- writer.WriteInt(max).WriteNewLine().Dispose();
- }
- private static boolean partiallyCovered(int lmtSt, int lmtEd, int st, int ed) {
- return ed >= lmtEd;
- }
- private static boolean isLeft(int lmtSt, int lmtEd, int st, int ed) {
- return lmtEd < st;
- }
- private static boolean overlaps(int lmtSt, int lmtEd, int st, int ed) {
- if (lmtSt <= st && st <= lmtEd) return true;
- if (st <= lmtSt && lmtSt <= ed) return true;
- return false;
- }
- private static int swap(int a, int b) {
- return a;
- }
- static boolean next_permutation(int[] p) {
- for (int a = p.length - 2; a >= 0; --a)
- if (p[a] < p[a + 1])
- for (int b = p.length - 1; ; --b)
- if (p[b] > p[a]) {
- int t = p[a];
- p[a] = p[b];
- p[b] = t;
- for (++a, b = p.length - 1; a < b; ++a, --b) {
- t = p[a];
- p[a] = p[b];
- p[b] = t;
- }
- return true;
- }
- return false;
- }
- static int gcd(int a, int b) {
- if (a < 0) a *= -1;
- if (b < 0) b *= -1;
- return b == 0 ? a : gcd(b, a % b);
- }
- static class Reader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public Reader() {
- }
- public Reader InitConsoleReading() {
- reader = new BufferedReader(new InputStreamReader(System.in), 32768);
- tokenizer = null;
- return this;
- }
- public Reader InitFileReading(String filePath) throws FileNotFoundException {
- reader = new BufferedReader(new FileReader(filePath), 32768);
- tokenizer = null;
- return this;
- }
- 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 String nextLine() throws IOException {
- return reader.readLine();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- }
- static class Writer {
- private BufferedWriter log;
- public Writer InitConsoleWriting() {
- log = new BufferedWriter(new OutputStreamWriter(System.out));
- return this;
- }
- public Writer InitFileWriting(String filePath) throws IOException {
- log = new BufferedWriter(new FileWriter(filePath));
- return this;
- }
- public Writer WriteInt(int inp) throws IOException {
- this.log.write(String.valueOf(inp));
- return this;
- }
- public Writer WriteChar(char inp) throws IOException {
- this.log.write(inp);
- return this;
- }
- public Writer WriteNewLine() throws IOException {
- return this.WriteChar('\n');
- }
- public Writer WriteStr(String str) throws IOException {
- this.log.write(str);
- return this;
- }
- public Writer WriteIntArray(int inp[], char delimChar) throws IOException {
- int len = inp.length;
- if (len > 0) {
- log.write(String.valueOf(inp[0]));
- for (int i = 1; i < len; i++) {
- log.write(String.format("%c%s", delimChar, String.valueOf(inp[i])));
- }
- }
- return this;
- }
- public Writer WriteCharArray(char inp[], char delimChar) throws IOException {
- int len = inp.length;
- if (len > 0) {
- log.write(String.valueOf(inp[0]));
- for (int i = 1; i < len; i++) {
- log.write(String.format("%c%s", delimChar, String.valueOf(inp[i])));
- }
- }
- return this;
- }
- //
- public Writer WriteStrArray(String inp[], char delimChar) throws IOException {
- int len = inp.length;
- if (len > 0) {
- log.write(inp[0]);
- for (int i = 1; i < len; i++) {
- log.write(String.format("%c%s", delimChar, inp[i]));
- }
- }
- return this;
- }
- public Writer WriteCase(String prefix, int caseNo, String suffix) throws IOException {
- return this.WriteStr(String.format("%s%d%s", prefix, caseNo, suffix));
- }
- public Writer Dispose() throws IOException {
- this.log.flush();
- this.log.close();
- return this;
- }
- public Writer WriteSpace() throws IOException {
- return this.WriteChar(' ');
- }
- public Writer WriteDouble(double v) throws IOException {
- this.log.write(String.format("%.2lf", v));
- return this;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment