[At Coder] Solve the ABC183 D problem with Ruby

The brown coder tried to solve the ABC183 D problem in Ruby.

problem

AtCoder Beginner Contest 183

D - Water Heater

There is a $ 1 $ water heater that can supply $ W $ liters of hot water per minute. There are $ N $ people. The $ i $ th person plans to use $ P_i $ liters of water boiled in this water heater every minute between time $ S_i $ and $ T_i $ (excluding time $ T_i $). The hot water cools quickly and cannot be stored. Is it possible to supply hot water to everyone as planned?

Constraint

1 \leq N \leq 2 \times 10^5 0 \leq S_i \leq 2 \times 10^5 1 \leq W, P_i \leq 10^9 All inputs are integers

input

N W S_1 T_1 P_1S_N T_N P_N

Answer

nw = gets.chomp.split.map(&:to_i)
n = nw[0]
w = nw[1]

#An array that stores the amount of water used per hour
a = Array.new(200005, 0)

n.times do
    stp = gets.chomp.split.map(&:to_i)
    s = stp[0]
    t = stp[1]
    p = stp[2]

    #Preparation for "array to store cumulative sum"
    a[s] += p
    a[t] -= p
end

result = true

#Store cumulative sum
200001.times do |i|
    if a[i] > w
        result = false
        break
    end
    a[i+1] += a[i]
end

puts result ? "Yes" : "No"

Way of thinking

Use the cumulative sum. However, keep in mind that you will do it later, not from the beginning.

  1. Prepare an array to store the amount of water used per hour
  2. Increase or decrease the amount of water used only at the beginning and end of use
  3. Calculate the cumulative sum of water consumption for each hour

Prepare an array to store the amount of water used per hour

Since it is $ T_i \ leq 2 \ times 10 ^ 5 $, prepare an array with $ 200001 $ or more elements.

Increase or decrease the amount of water used only at the beginning and end of use

As mentioned above, the point is not to calculate the cumulative sum at first. If you ask for it from the beginning, you will run out of execution time.

First of all, when entering information for $ N $ people, only increase or decrease the amount of water used at the beginning and end of use. Specifically, do as follows.

--Increase the amount of water used when you start using --Reduce the amount of water used at the end of use

Calculate the cumulative sum of water consumption for each hour

After that, we will calculate the cumulative sum as usual. It is OK if you judge whether it exceeds $ W $.

Impressions

I went to ask for the cumulative sum from the beginning, but I couldn't AC because the execution time was not enough. Crying Increase the amount of water used only at the beginning of use, and decrease it only at the end of use. I see, I feel like I had that hand.

Recommended Posts

[At Coder] Solve the ABC183 D problem with Ruby
[At Coder] Solve the ABC182 D problem with Ruby
[Competition Pro] Solve the knapsack problem with Ruby
AtCoder ABC127 D hash to solve with Ruby 2.7.1
[Beginner] Let's solve AtCoder problem with Ruby while looking at the article!
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
I tried to solve the problem of "multi-stage selection" with Ruby
[At Coder] Introducing Competitive Pro with Ruby
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
Solving with Ruby and Crystal AtCoder ABC 129 D
atcoder ABC70 D problem
Solving with Ruby and Java AtCoder ABC129 D 2D array
Solve the Discrete Logarithm Problem with an arbitrary mod
[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with Ruby!
Solve Google problems with Ruby
Let's solve the FizzBuzz problem!
[Ruby] problem with if statement
Programming with ruby (on the way)
Solve ARC104 D Multiset Mean with Scala, Java, C ++, Ruby, Perl, Elixir
Solving the knapsack problem with dynamic programming
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
Ruby problem ⑦
[Ruby] Define the hierarchy at the same time as Hash initialization with tap method
Publish the app made with ruby on rails
Manage the version of Ruby itself with rbenv
[Ruby] Exclude duplicate elements with the uniq method.
A quick look at the Monty Hall problem
Determine the current page with Ruby on Rails
To get Ruby Silver at the last minute
[Java] Try to solve the Fizz Buzz problem
Finding pi with the Monte Carlo method? (Ruby)
I checked the number of taxis with Ruby