kilobyte136's blog

By kilobyte136, history, 2 hours ago, In English

Hello Codeforces,

Although i did not participate in Educational Codeforces round 190. I have tried to solve its first problem, https://mirror.codeforces.com/contest/2230/problem/A .

The core of the problem says that:

There are $$$n$$$ students whom have access to keys of cost $$$a$$$ and $$$b$$$ , which are individual keys and group keys for an online course. For which, a group can be up to three. Our task is to minimize spending for all $$$n$$$ students, getting each one a key.

Now, if all $$$n$$$ kids need to have keys, then we can work by using the group keys for groups of 3 (max). Then as per the remainder, we can add along either $$$a$$$ or $$$b$$$ depending which is smaller.

first part:

check whether buying individually for 3 or group wise for 3 (max) is more optimal, spend it on $$$floor(n/3)$$$ kids (as we are buying in groups of 3 or 3 individually for max). mathematically : $$$min(3a, b)*floor(n/3)$$$

last part:

after optimally buying maximum keys in less price, we check how much more is needed (remainder) and check whether buying individually for 1 or group wise for 1 (min) is better. mathematically : $$$n \bmod 3 * min(a,b)$$$

combined: $$$min(3a, b)*floor(n/3) + n \bmod 3 * min(a,b)$$$

Heres the code:

#include <bits/stdc++.h>
using namespace std;

void solve(){
    int n,a,b; cin >>n >>a >>b;
    int price = 0;
    if(n<3){
        price = min(a*n, b);
    }
    else{
        price = min(a*3, b)*(n/3)+(n%3)*(min(a,b));
    }
    cout << price << endl;
}

int main() {
        ios::sync_with_stdio(false);
        cin.tie(NULL);
	int t; cin >> t;
	while(t--) solve();
}

Edit: I have not talked about my $$$if(n \lt 3)$$$ yet, sorry for that. the assumed exceptions are $$${1, 2}$$$ as first part would become $$$0$$$ and the last part would be $$$n*min(a,b)$$$ , which causes hindrance if $$$b \lt a$$$ , as b is group based up to 3, we would not need more than one group key, yet still we are calculating $$$n*b$$$ . Using $$$min(a*n, b)$$$ is more reliable as it does not guarantee that the result must be individual.

its currently in queue (queueforces), so i would like to know if my code is correct, any edges or errors.

  • Vote: I like it
  • -4
  • Vote: I do not like it

»
113 minutes ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

interger overflow , your conditions of if else are wrong

»
108 minutes ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by kilobyte136 (previous revision, new revision, compare).

»
107 minutes ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by kilobyte136 (previous revision, new revision, compare).