-1

I am trying to solve the apartments problem on CSES. VERY new to coding and not looking for a solution.. just help with troubleshooting.

https://cses.fi/problemset/task/1084/.

I want to match n applicants to m apartments based on cost, given a list of each. It works with sample inputs, but many tests still fail. I think the issue is with my vector manipulation in the nested for loop.

Example of a test that fails

n 10 
m 10 
k 10 
n integers  90 41 20 39 49 21 35 31 74 86  
m integers  14 24 24 7 82 85 82 4 60 95 

Expected result for aptsMatched: 6, my result: 3

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

int main() {

    int n, m, k, aptfill, aptsize;
    int aptsMatched = 0;
    vector<int> desiredSize;
    vector<int> apts;
    int b=0;
    cin >> n;
    cin >> m;
    cin >> k;

I'm sure there's a better way to do this.. but I'm brand new and attempted to do this in an intuitive manner. Was able to successfully fill my vectors with the inputs below.

    for (int i = 0; i < n; i++) {
        cin >> aptfill;
        desiredSize.push_back(aptfill);
        sort(desiredSize.begin(), desiredSize.end());
        reverse(desiredSize.begin(), desiredSize.end());

    }

    for (int i = 0; i < m; i++) {
        cin >> aptsize;
        apts.push_back(aptsize);
        sort(apts.begin(), apts.end());
        reverse(apts.begin(), apts.end());

    }

I'm attempting to iterate through the apts vector 3x, each time holding the apts index value constant while iterating through the desiredSize vector indices. Suspect something is wrong with the nested loop - either j<desiredSize.size(); or flawed logic in the if statement... or even with the erase function. My questions are: how can I test what's going wrong here? is there possibly a better container?

    for (int i = 0; i < m; i++) {  

        for (int j = 0; j < desiredSize.size(); j++) {
            
            if (abs(apts[b] - desiredSize[j]) <= k) {                  
                desiredSize.erase(desiredSize.begin() + j);
                b++;
                aptsMatched++;
                break;
            }
            else {
                continue;


            }
        }
    }

    cout << aptsMatched;
}

I realize this code is pretty hideous, and that there are much faster ways to achieve the same idea. I'd first like to understand this methodology (if it's not completely flawed to start with), but am open to more effective ways to approach this problem too.

Thanks

Ted Lyngmo
  • 37,764
  • 5
  • 23
  • 50
Pandaiga
  • 1
  • 1
  • One clear improvement would be to sort you vectors after you have input the values. There's no need to repeatedly sort the vectors. – john Dec 10 '20 at 09:30
  • `else { continue; }` is unnecessary and distracting. Loops continue automatically, you don't need to request that they continue. – john Dec 10 '20 at 09:33
  • But the main problem is that you don't say what is actually wrong. Are you getting a crash? Unexpected results? In order to get help, you really should say what is wrong in the first place. – john Dec 10 '20 at 09:34
  • 1
    I can't compile this. Please read [Why should I not `#include `?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) – Ted Lyngmo Dec 10 '20 at 09:35
  • The code runs, but it doesn't produce the expected result for all tests. One moment and I'll provide the inputs I'm trying, and what I expect – Pandaiga Dec 10 '20 at 09:35
  • @Pandaiga So can you give an example of the wrong output, the input that produces it, and the output you expected instead. – john Dec 10 '20 at 09:36
  • This test failed, n= 10 m= 10 k=10 n integers = 90 41 20 39 49 21 35 31 74 86 m integers = 14 24 24 7 82 85 82 4 60 95 expected result for aptsMatched: 6, my result: 3 – Pandaiga Dec 10 '20 at 09:38
  • @Pandaiga Great, but you should really put that in the question. – john Dec 10 '20 at 09:38
  • Will do next time – Pandaiga Dec 10 '20 at 09:41
  • Working on it.. trying to format now. – Pandaiga Dec 10 '20 at 09:49
  • @Pandaiga Your algorithm is wrong. It's clearly a more difficult problem that you appreciate (not as intuitive as you assume). This is not a coding issue, it an issue of analyzing the problem correctly and only then writing the code. – john Dec 10 '20 at 09:50
  • Ok - thank you for taking a look. I will come back to this problem after I've made some more progress. Probably a little over my head for the moment. – Pandaiga Dec 10 '20 at 09:52

2 Answers2

0

One way could be to sort both the desired sizes and the appartment sizes and start matching from the smallest and upwards.

Example:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    int max_diff = 10;

    std::vector<int> desired{90, 41, 20, 39, 49, 21, 35, 31, 74, 86};
    std::vector<int> apts{14, 24, 24, 7, 82, 85, 82, 4, 60, 95};

    std::sort(desired.begin(), desired.end());
    std::sort(apts.begin(), apts.end());

    int aptsMatched = 0;

    auto dit = desired.begin(); // iterator to the desired sizes
    auto ait = apts.begin();    // iterator to appartment sizes
        
    // loop until one of them reaches the end
    while(dit != desired.end() && ait != apts.end()) {
        // do we have a good fit?

        if(*dit + max_diff >= *ait   // the maximum size is >= appartment size
           &&
           *dit - max_diff <= *ait ) // the minimum size is <= appartment size
        {
            ++aptsMatched;
            ++dit;
            ++ait;
        } else // not a good fit, step the iterator to the smallest size
            if(*dit < *ait) ++dit;
            else ++ait;
    }

    std::cout << aptsMatched << '\n';
}
Ted Lyngmo
  • 37,764
  • 5
  • 23
  • 50
0

I want to match n applicants to m apartments based on cost

But there is no cost; you mean desired size, right? ... but even then - how do you want to match applicants to apartments?

Since you're a beginner, consider writing down your algorithm in your own words, in English - both for yourself, before you write your C++ code, and for us if you want help understanding what' going wrong.

Also, here are some red flags for you:

  • You're sorting your (partial) data repeatedly - n and m times respectively! That's almost certainly the wrong thing to do.
  • You're reversing your (partial) data repeatedly. This too seems quite redundant.

Other issues with your code:

  • You really should not include internal library headers which are not defined by the standard, like <bits/whatever>. See here.
  • Your shorthand names are confusing. First, n and m are quite opaque. Also, aptfill, b? No idea what that's supposed to mean.
  • Why bother sorting in one direction, then reversing? Just sort in the opposite direction. Or, alternatively, you can iterate the sorted array backwards.
einpoklum
  • 86,754
  • 39
  • 223
  • 453