Codeforces Round 918 div4
Codeforces Round 918 (div 4)
Problem
A
Use the
frequency to determine the element that appears once.
Problem
B
Have a set
for each row and column with "ABC" and remove characters in specific
(r, c) pairs. And there will remain a single set with just one character.
Problem
C
Determine
if the sum is a squared integer. Can use
math.sqrt to determine this.
Problem
D
- Create string
of C and V for identifying the consonants and vowels.
- create a deque
to store the locations of dots.
- Iterate over
the CV string, and anytime you find CC, that means it must be CVC was the
prior syllable. Cause you found a
CVCCXXX, The other pattern that can
appear is if XCVCVX, if you see
VCV, that means there must be a CVCVX,
You know there was a CV pattern before the CV of most recent. Which the most recent could be a CVC,
you don't know what X is yet. But
you know for the CV of prior.
Another observation is that you are looking for double CC, cause
that will always indicate a CVC syllable.
Then it is just determining what would indicate a CV syllable.
- The dots are
sorted in the deque, so now iterate over the original string and whenever
the front element in deque matches the index that is a location for a dot
so append to list
- Convert list
back to string.
Problem
E
This
solution fails, because the set solution was hacked. But it is fast on average, but it can have
worst case O(n) time.
- Track the even
and odd prefix sum for the array.
- Iterate over
the array in order and determine if the current element should be added to
even or odd prefix sum.
- Compute the
difference between the prefix sum
- Check if this
same difference has been found earlier in the loop. Cause if it was you can imagine that is
the start of your current contiguous subarray where even sum = odd
sum. And the end of current
contiguous subarray is current location.
It makes sense because if even was greater by 2 earlier and is
greater than 2 now. You can realize
the skew is the same so it cancels out.
This proof
shows that the even and odd prefix sum are equivalent when the even sum has the
same difference from odd sum at two points in time.
I didn't
think of this, but you could use one prefix sum, and just set even integers to
be negative, and you would then be looking for when there is a contiguous
subarray with sum = 0.
So we just
need to check if there is a subarray with sum 0. This is a
standard problem with prefix sums: if two prefix sums are equal, then the
subarray between them has sum 0; otherwise, no subarray has sum 0.
From <https://codeforces.com/blog/entry/123952>
The AC
solution is below
- Convert all of
the odd index elements to negative, so you will have a0 - a1 + a2 - a3 +
a4 + ...
- Then you can
calculate prefix sum from this, and if it ever equals 0 for instance that
means the positive even and negative odd index cancel each other out and
also means they are equal. But it
isn't always going to be the case you see 0, if you see any repeated value
in the prefix sum that represents a subarray sum equal to 0. Cause 2 - 2 = 0 for example, if you had
0, 2, -1, 3, 2 => this subarray from 2 to 2 is actually 0. Which is the
goal.
- So you can
just sort the prefix sum array and find when two adjacent elements are
equivalent.
Problem
F
- Sort the
points (a, b) and also store the end points b in their own list.
- Loop through
the end points in sorted (ascending order) and create an index_map for
them as well. That is map each b to
an index. This will be used for
coordinate compression
- Create a
fenwick tree
- Iterate over
the a, b points, And count how many prior a points had end points b that
were after the current b. So you
just need to query the range [index_map[b], n]. To get all the count after.
- Then take the
current b and add it to the fenwick tree.
Problem
G
- Observe you
have a weighted undirected graph and it is a connected graph and the
problem is asking for a shortest path to get from a source to target.
- This is enough
to know that a dijkstra algorithm will work.
- The only catch
is you need to track (u, s) for the visited states, instead of just the
node. You want to know that this is
the best visit of node u when you have current slowness factor of s.
- This matters
because s affects the future costs of traversing edges.
- So you are
minimizing dp(u, s) with dijkstra.
Comments
Post a Comment