f.verri's blog

By f.verri, history, 8 months ago, In English

Hello. I have been trying to solve this problem for a while now and I can't find the bug that's causing the WA, I know it's probably being caused by a stupid typo, but I can't find it at all. The code is failing on test case 2

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define iter(i, l, r) for (ll i = l; i < r; i++)

bool isPossible(ll day, ll n, ll p, ll l, ll t) {
    ll tasks = ceil(float(n) / 7.0);
    ll maxTasks = min(tasks, (n - day) * 2);

    return ((n - day) * l) + (maxTasks * t) >= p;
}

int main() {
    ll numberOfTestCases;
    cin >> numberOfTestCases;

    vector<ll> toShow;

    iter (_, 0, numberOfTestCases) {
        ll n, p, l, t;
        cin >> n >> p >> l >> t;

        ll start = 0;
        ll ender = n;
        while (ender - start > 1) {
            ll mid = (start + ender) / 2;

            if (isPossible(mid, n, p, l, t)) {
                start = mid;
            } else {
                ender = mid;
            }
        }

        toShow.push_back(start);
    }

    for (ll x : toShow) {
        cout << x << "\n";
    }

    return 0;
}
  • Vote: I like it
  • -4
  • Vote: I do not like it

»
8 months ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it
consider
2 100 50 50

your code outputs 0, it should output 1.

Edit : I got AC with your same code with C++ 17, C++ 20 gives WA, if u get rid of floor and ceil

and use (n + 6) / 7, then C++ 20 also gives AC with ur code 236404618

  • »
    »
    8 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Thank you so much. I would have never realized that. Do you think it's bad practice to use floating numbers like i did on the isPossible function? By the way, sorry for not linking the problem, i ended up forgotting

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Ur welcome and yeah don't use floor/ceils unless there is no other way.