You are not logged in. Please login at www.codechef.com to post your questions!

×

DWNLD - Editorial

Problem Link:

Contest
Practice

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;
    }
}

Author's and Tester's Solutions

Setter
Tester

This question is marked "community wiki".

asked 20 Feb '17, 14:06

kingofnumbers's gravatar image

6★kingofnumbers ♦
194824
accept rate: 12%

edited 20 Feb '17, 15:46

admin's gravatar image

0★admin ♦♦
19.6k349497539


    for(i=0;i<n;i++)
    {
        cin>>t>>d;   
        if(t<=k)  //if time(mins) is less than or equal to the remaining free data mins given then no data charges 
        {
            k=k-t;
            t=0;
        }
        else     //if time is greater than remaining free data mins then charges will apply for the difference mins
        {
            t=t-k;
            k=0;
        }
        charge+=(t*d);
    }
link

answered 20 Feb '17, 20:00

vipulmehta's gravatar image

2★vipulmehta
311
accept rate: 0%

edited 20 Feb '17, 20:00

for _ in range(int(input())) :
n , k = map(int, input().split() )
cost = 0
for session in range(n) :
    iT , iD = map(int, input().split() )
    if ( iT <= k ) :
        k -= iT
        #print("Cost = {}, k = {} iT = {} iD = {}".format(cost, k , iT, iD))
    else : 
        k = 0 if k < 0 else k
        cost += ( ( iT - k )  * iD )  
        k -= iT
        #print("Cost = {}, k = {} iT = {} iD = {}".format(cost, k , iT, iD))
print ( cost )

This is my solution from Python

link

answered 05 Dec '17, 22:43

sarath127's gravatar image

2★sarath127
1
accept rate: 0%

I am not able to figure what is wrong in my solution, I have used a different approach

include<iostream>

using namespace std;

int main(){ int t,n,k,sum,i; cin>>t; while(t--){ cin>>n>>k; sum=0; int arr[n][2];

    for(i=0;i<n;i++){
        cin>>arr[i][0]>>arr[i][1];
    }
    for(i=0;i<n;i++){
        if(k-arr[i][0]>=0){
            k=k-arr[i][0];
        }
        else{
            arr[i][0]=arr[i][0]-k;
            sum+=arr[i][0]*arr[i][1];
        }
    }
    cout<<sum<<endl;
}

}

link

answered 10 Aug, 22:24

bahl24's gravatar image

0★bahl24
1
accept rate: 0%

This isn't the first time a question is badly worded and not explained in proper detail.

"The internet provider charges the Chef 1 dollar for every 1 MB data used, except for the first K minutes, when the internet data is free as part of the plan provided to Chef."

Question never mentions how the cost is calculated if suppose you have values like N=2,K=1 , T1=3,D1=5, T2=3, D2=1.

You get K=1 minute of free usage so how many MBs do you consume in the 2 minutes left in T1, can you consume multiple MBs in 1 minute ? Is the speed fixed at 1 minute = 1 MB ?

Then there is the problem in the explanation of the test cases. If the problem clearly says 1MB = $1 then why is the cost being calculated like " 1 * 2 + 2 * 4 + 10 * 10 = 110 " ??? Minutes shouldn't even be a factor after K. Sorry but this question makes no sense.

link

answered 17 Oct, 17:02

coderotaster's gravatar image

0★coderotaster
11
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,126
×1,556
×84
×9

question asked: 20 Feb '17, 14:06

question was seen: 1,514 times

last updated: 17 Oct, 17:02