USACOW: The Aftermath. PotW Solution Scanner s = new Scanner(System.in); int n = s.nextInt(), k = s.nextInt(), x = 0; for (int i = 0; i < n; i++) x ^=

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

1 Various Methods of Populating Arrays Randomly generated integers.
1 Parallel Parentheses Matching Plus Some Applications.
(())()(()) ((())()(())()(()))
This title is orange because of Halloween. It is totally not because it’s always orange.
I couldn’t think of anything to put here. So have a cow. Or two.
3.12 ≠ π What an inspiring title. PotW Solution - Bovinekiin int stall = 0; for (int i = 0; i calcGreedy(best)) { best = new ArrayList (cur); // update.
Vector Machines Model for Parallel Computation Bryan Petzinger Adv. Theory of Computing.
CSC1016 Coursework Clarification Derek Mortimer March 2010.
Building Java Programs
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
CSC 110 – Intro to Computing Lecture 14: Midterm Review.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Number Systems Lecture 02.
- ABHRA DASGUPTA Solving Adhoc and Math related problems.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Moooooo Or: How I learned to stop worrying and love USACO.
Suppose you have a problem involving N data points. Recursive solution of such problem is a follows: If the problem can be solved directly for N points.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Houyang 3/25/13. USACO March Contest Congrats to Johnny Ho for scoring over 900 points in the Gold Division o 7th place in US Kudos to Jonathan Uesato.
Binary Search From solving a problem to verifying an answer.
Sorting Fun1 Chapter 4: Sorting     29  9.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
Happy Birthday Julia Courier New. USACO December Contest - Congratulations to Jon+Julia+Andy for promoting to gold And Johnny - 2th place in gold among.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Public void main What do you call something that’s not static?
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Symbol Tables and Search Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
Digital Logic Design (CSNB163)
Huffman Codes Juan A. Rodriguez CS 326 5/13/2003.
1 Chapter 7 Skip Lists and Hashing Part 2: Hashing.
Click to add title Click to add lame programming pun.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Not a Math Honor Society meeting. Or: How I learned to do math again, this time with cows.
Where’s the title? You gotta search for it!. PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev,
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
 MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions.
The SECOND Meeting Or: How I learned exciting new things in CS Club.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
The material in this lecture should be review. If you have not seen it before, you should be able to learn it quickly – and on your own. So we are going.
Interval Trees Marco Gallotta. Problem ● Given a collection of items i, each with value V i ● Want to answer many queries of the form: How many items.
Advanced Sorting 7 2  9 4   2   4   7
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
NUMBER SYSTEMS.
Data Structures: Disjoint Sets, Segment Trees, Fenwick Trees
Chapter 2 Scanning – Part 1 June 10, 2018 Prof. Abdelaziz Khamis.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
TN 221: DIGITAL ELECTRONICS 1
Computer Programming Methodology Input and While Loop
Algorithm design and Analysis
int [] scores = new int [10];
Defining methods and more arrays
Recitation Outline C++ STL associative containers Examples
Fundamentals of Data Representation
Building Java Programs
Primitive Types and Expressions
Bitwise operators.
Week 1 - Thursday COMP 4100.
Presentation transcript:

USACOW: The Aftermath

PotW Solution Scanner s = new Scanner(System.in); int n = s.nextInt(), k = s.nextInt(), x = 0; for (int i = 0; i < n; i++) x ^= s.nextInt() % (k + 1); System.out.println(x == 0 ? "John" : "Bessie");

USACO 2011 November Contest Officially over now (hopefully everyone took it) Preliminary results can be found at: o Yes, we can see everyone's scores! Problems in all divisions and their test data are also available. o Yay, more practice problems to do!

Bronze Problem: digits A number, N, is converted to base 2 and base 3, but one digit is written incorrectly in each case. Given these two (incorrect) results, find N. N < 1,000,000,000 Test all possible changes to the base 2 number and see if resulting number can be reached with one digit change to the base 3 number o If difference between resulting number and base 3 input is 1 or 2 times power of 3

Bronze Solution BufferedReader f = new BufferedReader(new FileReader("digits.in")); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("digits.out"))); String base2Str = f.readLine(); String base3Str = f.readLine(); int base2 = 0, base3 = 0; for (int i = base2Str.length() - 1, digit = 1; i >= 0; digit *= 2, i--) base2 += digit * (base2Str.charAt(i) - '0'); for (int i = base3Str.length() - 1, digit = 1; i >= 0; digit *= 3, i--) base3 += digit * (base3Str.charAt(i) - '0'); for (int powerOf2 = 1, digitIndex = base2Str.length() - 1; digitIndex >= 0; powerOf2 *= 2, digitIndex--) { int possibleN = (base2Str.charAt(digitIndex) == '1') ? base2 - powerOf2 : base2 + powerOf2; int diff = Math.abs(possibleN - base3); if (isPowerOf3(diff)) { out.println(possibleN); out.close(); System.exit(0); } private static boolean isPowerOf3(int num) { while (num % 3 == 0) num /= 3; return num == 1 || num == 2; }

Silver Problem: lineup N cows are standing at various positions along a line. Each cow has an integer position and an integer “breed ID” Given each cow’s position and breed number, determine the minimum size (range of positions) of a photograph capturing at least one of each breed of cow.

Silver Problem (cont.) class Pair implements Comparable { int x, id; Pair(int x_,int id_){x = x_; id = id_;} public int compareTo(Pair o) { if(x != o.x) return (x < o.x) ? -1 : 1; return (id < o.id) ? -1 : 1; } } Utility class for sorting pairs of numbers Implement comparable Sort by first, then by second Very often useful

Silver Solution Scanner s = new Scanner(System.in); int num = s.nextInt(), idc = 0; // idc is number of unique ids ArrayList list = new ArrayList (); HashMap map = new HashMap (); for(int i = 0; i < num; i++) { int x = s.nextInt(), id = s.nextInt(); // scan in x coordinates and ids if(!map.containsKey(id)) // compress ids using HashSet map.put(id,idc++); list.add(new Pair(x,map.get(id))); } Collections.sort(list); // sort cows by x coordinate int[] cnt = new int[idc]; // bins cows by id int existc = 0, left = 0, right = 0, best = ; while(left < num && right < num) { while(right < num && existc < idc) { // until all ids are present int cur = list.get(right++).id; // sweep right, adding cows if(cnt[cur]++ == 0) existc++; } if(existc == idc) { // if this is valid, modify best accordingly int d = list.get(right-1).x - list.get(left).x; if(d < best) best = d; } int cur = list.get(left++).id; // increment left, erase cow if(cnt[cur]-- == 1) existc--; } System.out.println(best);

Gold Problem: median Problem: Given n≤100,000 numbers, count how many continuous subsequences have median≥k, where the median's index is rounded up if the subsequence has even length. Obsrv. #1: O(n 2 ) is too slow for full credit Obsrv. #2: This problem is equivalent to counting how many subsequences have at least half their numbers greater than k. Obsrv. #3: Note that if you replace k with 0, you can then replace every number in the list ≥k with 1, and <k with -1.

Gold Problem (cont.) Obsrv. #4: Take prefix sums of the modified list - e.g. {-1,1,-1,-1,1} turns into {-1,0,-1,-2,-1} Obsrv. #5: Now you just have to determine, for each number in the prefix sum list, how many numbers to the left of it are ≤ it. The problem has been greatly simplified. o Note that this answer is essentially the reverse of the number of inversions of the prefix sum list From this point on, use either: o Mergesort - used to count # of inversions o Binary Indexed or Fenwick Tree - take advantage of each prefix sum lying within [-n,n]

Gold Solution bit : stores the Binary Indexed Tree j & -j isolates the last significant digit - this operation forms the core of BITs sum : prefix sum - artificially shifted by num + 1 to become nonnegative sum++ or sum-- depending on s.nextInt() ans : stores final answer - queries from the BIT are added to it Scanner s = new Scanner(System.in); int num = s.nextInt(), thresh = s.nextInt(), sum = num + 1; int[] bit = new int[2 * num + 2]; long ans = 0; for (int i = 0; i = thresh) sum++; else sum--; for (int j = sum; j > 0; j -= (j & -j)) ans += bit[j]; } System.out.println(ans);

PotW: Cow Whitewashing One day, at school, Bessie wrote a string of matching '('s and ')'s. As soon as she got home, however, she noticed that she had definitely made some mistakes! She plans on covering this up by simply erasing some of the parentheses on the left and right, while aiming for the longest substring. The substring she obtains must be a valid sequence of parentheses - each '(' must match to a ')' to its right, and vice versa. Tell Bessie the length of the longest valid substring. Sample Input - strings of parentheses ()) ()() )(()))(()()) Sample Output - length of longest valid substring For 15 pts: length < 100 For 25 pts: length < 1,000 For 35 pts: length < 1,000,000