Please, try EDU on Codeforces! New educational section with videos, subtitles, texts, and problems. ×

DontLookBack's blog

By DontLookBack, history, 5 weeks ago, In English,

The judge shows WA on test 23 but when I try on my compiler it gives expected output for the same test. Why is this so ?

Problem : https://codeforces.com/contest/570/problem/B

Have a look at submission for test cases :

82462028

Thanks!

 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
5 weeks ago, # |
  Vote: I like it +9 Vote: I do not like it

What do you want to get here:

if(pr1-pr2 > eps || pr1==pr2)

Probably, you have to use abs(pr1-pr2) > eps. Having long double pr1 and pr2, is there any sense to pr1==pr2?

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I know I tried doing just pr1>=pr2 before but got the same result. Any idea why this occurs?

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Happened with me a couple of times. Float point arithmetic depends on compiler specifications, you need a better implementation I guess.

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

I think the bug is in your code. You can completely remove doubles from your code if you don't divide by n. Since all the probabilities have n in the denominator, you can just leave n out when comparing.

My code using this trick:

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

using ll = long long;

int n, m;

void solve() {
    cin >> n >> m;

    // m + 1 vs m - 1
    if (n == 1) {
        cout << 1 << "\n";
    } else if (m - 1 >= n - (m + 1) + 1) {
        cout << m - 1 << "\n";
    } else {
        cout << m + 1 << "\n";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return EXIT_SUCCESS;
}

Even while using long doubles my code still passes:

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

using ll = long long;
using ld = long double;

int n, m;

void solve() {
    cin >> n >> m;

    // m + 1 vs m - 1
    if (n == 1) {
        cout << 1 << "\n";
    } else if ((m - 1) / (ld)n >= (n - (m + 1) + 1) / (ld)n) {
        cout << m - 1 << "\n";
    } else {
        cout << m + 1 << "\n";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return EXIT_SUCCESS;
}

Therefore it is (fortunately?) likely that your code is the culprit.