Problem Link:
Author: Hasan Jaddouh
Testers: Kamil Debowski
Editorialist: Hasan Jaddouh
Difficulty:
cakewalk
Pre-requisites:
none
Problem Statement:
Given a list internet speed during different times, the internet service provider charges 1 dollar per 1 MB downloaded, except for the first K minutes it was free, calculate the total cost that should be paid.
Explanation
We will describe the logic of the solution and the implementation details will be in C++
One input file contains multiple test-cases, we should process each test-case alone, so first thing we need a variable to read the number of test-cases then we make a loop to iterator over test-cases, inside it we will solve the problem for a single test-case, it’s fine to output the result of one test-case before reading the rest of test-cases.
so far our code should look like this:
#include <iostream>
using namespace std;
int Tc;
int main(){
cin>>Tc;
for(int j=0;j<Tc;j++){
// process a single test-case here
}
}
for single test-case, we should read N and K, so we need two variables for them we also need a variable to store the answer (Let’s name it sol) initially it has value 0. after that we should a make a loop to iterate over lists of durations and speeds, in every step in this loop we should read the duration and speed so we also need variables for them, thus so far our code is like this:
#include <iostream>
using namespace std;
int Tc;
int main(){
cin>>Tc;
for(int j=0;j<Tc;j++){
// process a single test-case here
int N,K,sol=0;
cin>>N>>K;
for(int j=0;j<N;j++){
int T,D;
cin>>T>>D;
}
}
}
Now, let’s use the variable K as how much time remaining for free period so if T is less than K then the whole T duration will be free but K should decrease by T, otherwise if T is greater or equal to K then only first K minutes will be free so we will pay for the rest (T-K) minutes and the amount to pay will be (T-K)*D so we increase sol by it, after that we should decrease K to 0 because free period is ended
by the end of the loop we just output sol, so the full solution is:
#include <iostream>
using namespace std;
int Tc;
int main(){
cin>>Tc;
for(int j=0;j<Tc;j++){
// process a single test-case here
int N,K,sol=0;
cin>>N>>K;
for(int j=0;j<N;j++){
int T,D;
cin>>T>>D;
if(T<K){
K= K - T;
} else {
sol += (T-K)*D;
K=0;
}
}
cout<<sol<<endl;
}
}