najimCseJu

USACO 2015 December Contest, Bronze Problem 2. Speeding Tic

Oct 5th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.39 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  
  5. public class Main {
  6.     private static int Max = 111;
  7.     public static void main(String[] args) throws Exception {
  8.         //Reader reader = new Reader().InitConsoleReading();
  9.         Reader reader = new Reader().InitFileReading("speeding.in");
  10.         //Reader reader = new Reader().InitFileReading("D:\\Work\\SourceTree\\Algorithm\\io\\in.txt");
  11.         //Writer writer = new Writer().InitConsoleWriting();
  12.         Writer writer = new Writer().InitFileWriting("speeding.out");
  13.         //Writer writer = new Writer().InitFileWriting("D:\\Work\\SourceTree\\Algorithm\\io\\out.txt");
  14.         int[] limitsD = new int[Max];
  15.         int[] limitsV = new int[Max];
  16.         int[] drivesD = new int[Max];
  17.         int[] drivesV = new int[Max];
  18.         int n, m;
  19.         n = reader.nextInt();
  20.         m = reader.nextInt();
  21.         for (int i = 0; i < n; i++) {
  22.             limitsD[i] = reader.nextInt();
  23.             limitsV[i] = reader.nextInt();
  24.         }
  25.         for (int i = 0; i < m; i++) {
  26.             drivesD[i] = reader.nextInt();
  27.             drivesV[i] = reader.nextInt();
  28.         }
  29.         for (int i = 1; i < n; i++) {
  30.             drivesD[i] += drivesD[i - 1];
  31.         }
  32.  
  33.         int px = 0,max = 0;
  34.         int lmtSt = 1;
  35.         int lmtEd = limitsD[0];
  36.         for (int i = 0; i < m; i++) {
  37.             int st = i == 0 ? 0 : drivesD[i - 1] + 1;
  38.             int ed = drivesD[i]; //segment to be covered
  39.             while (true) {
  40.                 if (overlaps(lmtSt, lmtEd, st, ed)) {
  41.                     max = Math.max(max, drivesV[i] - limitsV[px]);
  42.                     if (partiallyCovered(lmtSt, lmtEd, st, ed)) {
  43.                         px++;
  44.                         lmtSt = lmtEd + 1;
  45.                         lmtEd += limitsD[px];
  46.                     } else break;
  47.                 } else if (isLeft(lmtSt, lmtEd, st, ed)) {
  48.                     px++;
  49.                     lmtSt = lmtEd + 1;
  50.                     lmtEd += limitsD[px];
  51.                 } else {
  52.                     break;
  53.                 }
  54.             }
  55.         }
  56.         writer.WriteInt(max).WriteNewLine().Dispose();
  57.     }
  58.  
  59.     private static boolean partiallyCovered(int lmtSt, int lmtEd, int st, int ed) {
  60.         return ed >= lmtEd;
  61.     }
  62.  
  63.     private static boolean isLeft(int lmtSt, int lmtEd, int st, int ed) {
  64.         return lmtEd < st;
  65.     }
  66.  
  67.     private static boolean overlaps(int lmtSt, int lmtEd, int st, int ed) {
  68.         if (lmtSt <= st && st <= lmtEd) return true;
  69.         if (st <= lmtSt && lmtSt <= ed) return true;
  70.         return false;
  71.     }
  72.  
  73.     private static int swap(int a, int b) {
  74.         return a;
  75.     }
  76.  
  77.     static boolean next_permutation(int[] p) {
  78.         for (int a = p.length - 2; a >= 0; --a)
  79.             if (p[a] < p[a + 1])
  80.                 for (int b = p.length - 1; ; --b)
  81.                     if (p[b] > p[a]) {
  82.                         int t = p[a];
  83.                         p[a] = p[b];
  84.                         p[b] = t;
  85.                         for (++a, b = p.length - 1; a < b; ++a, --b) {
  86.                             t = p[a];
  87.                             p[a] = p[b];
  88.                             p[b] = t;
  89.                         }
  90.                         return true;
  91.                     }
  92.         return false;
  93.     }
  94.  
  95.     static int gcd(int a, int b) {
  96.         if (a < 0) a *= -1;
  97.         if (b < 0) b *= -1;
  98.         return b == 0 ? a : gcd(b, a % b);
  99.     }
  100.  
  101.     static class Reader {
  102.         public BufferedReader reader;
  103.         public StringTokenizer tokenizer;
  104.  
  105.         public Reader() {
  106.         }
  107.  
  108.         public Reader InitConsoleReading() {
  109.             reader = new BufferedReader(new InputStreamReader(System.in), 32768);
  110.             tokenizer = null;
  111.             return this;
  112.         }
  113.  
  114.         public Reader InitFileReading(String filePath) throws FileNotFoundException {
  115.             reader = new BufferedReader(new FileReader(filePath), 32768);
  116.             tokenizer = null;
  117.             return this;
  118.         }
  119.  
  120.         public String next() {
  121.             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  122.                 try {
  123.                     tokenizer = new StringTokenizer(reader.readLine());
  124.                 } catch (IOException e) {
  125.                     throw new RuntimeException(e);
  126.                 }
  127.             }
  128.             return tokenizer.nextToken();
  129.         }
  130.  
  131.         public String nextLine() throws IOException {
  132.             return reader.readLine();
  133.         }
  134.  
  135.         public int nextInt() {
  136.             return Integer.parseInt(next());
  137.         }
  138.  
  139.         public long nextLong() {
  140.             return Long.parseLong(next());
  141.         }
  142.     }
  143.  
  144.     static class Writer {
  145.         private BufferedWriter log;
  146.  
  147.         public Writer InitConsoleWriting() {
  148.             log = new BufferedWriter(new OutputStreamWriter(System.out));
  149.             return this;
  150.         }
  151.  
  152.         public Writer InitFileWriting(String filePath) throws IOException {
  153.             log = new BufferedWriter(new FileWriter(filePath));
  154.             return this;
  155.         }
  156.  
  157.         public Writer WriteInt(int inp) throws IOException {
  158.             this.log.write(String.valueOf(inp));
  159.             return this;
  160.         }
  161.  
  162.         public Writer WriteChar(char inp) throws IOException {
  163.             this.log.write(inp);
  164.             return this;
  165.         }
  166.  
  167.         public Writer WriteNewLine() throws IOException {
  168.             return this.WriteChar('\n');
  169.         }
  170.  
  171.         public Writer WriteStr(String str) throws IOException {
  172.             this.log.write(str);
  173.             return this;
  174.         }
  175.  
  176.         public Writer WriteIntArray(int inp[], char delimChar) throws IOException {
  177.             int len = inp.length;
  178.             if (len > 0) {
  179.                 log.write(String.valueOf(inp[0]));
  180.                 for (int i = 1; i < len; i++) {
  181.                     log.write(String.format("%c%s", delimChar, String.valueOf(inp[i])));
  182.                 }
  183.             }
  184.             return this;
  185.         }
  186.  
  187.         public Writer WriteCharArray(char inp[], char delimChar) throws IOException {
  188.             int len = inp.length;
  189.             if (len > 0) {
  190.                 log.write(String.valueOf(inp[0]));
  191.                 for (int i = 1; i < len; i++) {
  192.                     log.write(String.format("%c%s", delimChar, String.valueOf(inp[i])));
  193.                 }
  194.             }
  195.             return this;
  196.         }
  197.         //
  198.  
  199.         public Writer WriteStrArray(String inp[], char delimChar) throws IOException {
  200.             int len = inp.length;
  201.             if (len > 0) {
  202.                 log.write(inp[0]);
  203.                 for (int i = 1; i < len; i++) {
  204.                     log.write(String.format("%c%s", delimChar, inp[i]));
  205.                 }
  206.             }
  207.             return this;
  208.         }
  209.  
  210.         public Writer WriteCase(String prefix, int caseNo, String suffix) throws IOException {
  211.             return this.WriteStr(String.format("%s%d%s", prefix, caseNo, suffix));
  212.         }
  213.  
  214.         public Writer Dispose() throws IOException {
  215.             this.log.flush();
  216.             this.log.close();
  217.             return this;
  218.         }
  219.  
  220.         public Writer WriteSpace() throws IOException {
  221.             return this.WriteChar(' ');
  222.         }
  223.  
  224.         public Writer WriteDouble(double v) throws IOException {
  225.             this.log.write(String.format("%.2lf", v));
  226.             return this;
  227.         }
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment