DEV Community is a community of 783,060 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Day 9: Uping the problem rating

Disclaimer:

I am a beginner in competitive programming, and this is me documenting my learnings. So, anything I say is just what I think at the moment and should not be taken as hard truths.

Problem 1: Taxi

using vi = vector<int>;

#define all(x) begin(x), end(x)
#define forn(i, n) for(int i = 0; i < n; i++)

main() {
int n;
cin >> n;
vi v(5, 0);
forn(_, n) {
int g;
cin >> g;
v[g]++;
}
// every four will require one car
int four = v;
// every three will require one car
int three = v;
// it can take groups of one too
v -= min(v, v);
// a car can take two groups of two and change
int two = v/2 + v%2;
int rem = v%2;
v -= min(v, rem*2);
int one = v/4 + (v%4 != 0);

int ans = one + two + three + four;
cout << ans;
}

The above solution was the first accepted solution I could come up with.

This one is more cleaner [Greedy Approach]

using vi = vector<int>;

#define forn(i, n) for(int i = 0; i < n; i++)
#define all(v) begin(v), end(v)
#define endl '\n'

main() {
int n;
cin >> n;
vi v(n);
for(auto& e: v) cin >> e;
sort(all(v));
int i = 0, j = n-1;
int ans = 0;
while(i <= j) {
int curr = v[j];
while(i < j && curr+v[i] <= 4) {
curr += v[i++];
}
j--;
ans++;
}
cout << ans;
}

Problem 2: Interesting Drink

I was initially afraid to solve the problem seeing dp in the tags. But then I hid the tags, and went for the
problem. lol.

Just check the number of places whose booze price is less than or equal to the price the man can pay.
Binary Search.

using vi = vector<int>;

#define forn(i, n) for(int i = 0; i < n; i++)
#define all(v) begin(v), end(v)
#define endl '\n'

main() {
int n;
cin >> n;
vi v(n);
for(auto& e: v) cin >> e;
sort(all(v));
int q;
cin >> q;
forn(_, q) {
int p;
cin >> p;
auto it = upper_bound(all(v), p);
cout << distance(begin(v), it) << endl;
}
}

Algorithm

We didn't learn any new algorithm today.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7244364,"math_prob":0.99655986,"size":1865,"snap":"2022-05-2022-21","text_gpt3_token_len":598,"char_repetition_ratio":0.111767866,"word_repetition_ratio":0.22340426,"special_character_ratio":0.39356568,"punctuation_ratio":0.17211328,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9981891,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-01-18T04:46:50Z\",\"WARC-Record-ID\":\"<urn:uuid:f78699eb-c25a-48a0-a396-72ec853f6ddf>\",\"Content-Length\":\"110429\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8cb770b2-fcf2-42a0-966d-bea239c4d249>\",\"WARC-Concurrent-To\":\"<urn:uuid:9bf28a2c-2df5-470f-991d-7ce8362116f2>\",\"WARC-IP-Address\":\"146.75.33.194\",\"WARC-Target-URI\":\"https://practicaldev-herokuapp-com.global.ssl.fastly.net/geekypandey/day-9-uping-the-problem-rating-3jag\",\"WARC-Payload-Digest\":\"sha1:JTCE65SWV3YO2HDSIWU3J6NXCU2CIPMH\",\"WARC-Block-Digest\":\"sha1:HQZJLDVMCLZAMT23SRFCDLWKHSBPRM2D\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-05/CC-MAIN-2022-05_segments_1642320300722.91_warc_CC-MAIN-20220118032342-20220118062342-00357.warc.gz\"}"}