Http://www.usaco.org/index.php?page=viewproblem2&cpid=807

Hello,

When I run my code to this problem:

#include <fstream>
using namespace std;

int a = 0; 
int b = 0;
int x = 0;
int y = 0;


/*
3 cases:
 - A -> B
 - A -> X -> Y -> B
 - A -> Y -> X -> B
*/

int main() {
    ifstream cin ("teleport.in");
    ofstream cout ("teleport.out");

    cin >> a >> b >> x >> y;

    int case_1 = abs(a - b);
    int case_2 = abs((a - x) + (b - y));
    int case_3 = abs((a - y) + (b - x));

    cout << min(case_1, case_2, case_3);
}

I get the following error:

Compilation Error
In file included from /usr/include/c++/7/bits/specfun.h:45:0,
                 from /usr/include/c++/7/cmath:1914,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:41,
                 from teleportation.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h: In instantiation of ‘constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare) [with _Tp = int; _Compare = int]’:
teleportation.cpp:27:39:   required from here
/usr/include/c++/7/bits/stl_algobase.h:246:17: error: ‘__comp’ cannot be used as a function
       if (__comp(__b, __a))
           ~~~~~~^~~~~~~~~~

I know that it means that I cannnot put three values in min(), but what can I do to substitute it?

cout << min(case_1, min(case_2, case_3));
min({case_1, case_2, case_3})

works too (see the initializer list version mentioned here).

1 Like