luosw's blog

By luosw, history, 4 years ago, In English

My code is here:

#include <bits/stdc++.h>

using namespace std;

int r, g, b, r1, g1, b1;
int R[1000], G[1000], B[1000], ans;

namespace luosw {
namespace IO {
    template < typename T >
    inline T read() {
        T    ret = 0, f = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9') {
            if (ch == '-')
                f = -f;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9')
            ret = ret * 10 + ch - '0', ch = getchar();
        return ret * f;
    }
    template < typename T >
    void write(T x) {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        T y = 10, len = 1;
        while (y <= x) {
            y *= 10;
            len++;
        }
        while (len--) {
            y /= 10;
            putchar(x / y + 48);
            x %= y;
        }
    }
    template < typename T >
    void write(T x, bool flag) {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        T y = 10, len = 1;
        while (y <= x) {
            y *= 10;
            len++;
        }
        while (len--) {
            y /= 10;
            putchar(x / y + 48);
            x %= y;
        }
        if (flag)
            putchar('\n');
    }
}  // namespace IO
namespace tools {
    template < typename T >
    T cmax(T a, T b) {
        return max(a, b);
    }
    template < typename T >
    T cmin(T a, T b) {
        return min(a, b);
    }
    template < typename T >
    T cgcd(T a, T b) {
        return __gcd(a, b);
    }
    template < typename T >
    T clcm(T a, T b) {
        return a * b / cgcd(a, b);
    }
}  // namespace tools
}  // namespace luosw
bool cmp(int a, int b) {
    return a > b;
}
int max(int a, int b, int c) {
    if (a > b && a > c)
        return a;
    if (b > a && b > c)
        return b;
    if (c > a && c > b)
        return c;
}
bool check() {
    if (r == r1 - 1 && g == g1 - 1)
        return 0;
    if (g == g1 - 1 && b == b1 - 1)
        return 0;
    if (r == r1 - 1 && b == b1 - 1)
        return 0;
    return 1;
}
signed main() {
    cin >> r >> g >> b;
    memset(R, 0, sizeof(R));
    memset(G, 0, sizeof(G));
    memset(B, 0, sizeof(B));
    for (int i = 0; i < r; i++) {
        cin >> R[i];
    }
    for (int i = 0; i < g; i++) {
        cin >> G[i];
    }
    for (int i = 0; i < b; i++) {
        cin >> B[i];
    }
    sort(R, R + r + 5, cmp);
    sort(B, B + b + 5, cmp);
    sort(G, G + g + 5, cmp);
    while (check()) {
        if (max(R[r1], G[g1], B[b1]) == R[r1]) {
            if (max(G[g1], B[b1]) == G[g1]) {
                ans += R[r1++] * G[g1++];
            }
            else {
                ans += R[r1++] * B[b1++];
            }
        }
        else if (max(G[g1], B[b1]) == G[g1]) {
            if (max(R[g1], B[b1]) == R[g1]) {
                ans += R[r1++] * G[g1++];
            }
            else {
                ans += G[g1++] * B[b1++];
            }
        }
        else {
            if (max(R[g1], G[b1]) == R[g1]) {
                ans += R[r1++] * B[b1++];
            }
            else {
                ans += G[g1++] * B[b1++];
            }
        }
    }
    cout << ans << endl;
#ifdef debug
    system("pause");
#endif
    return 0;
}

Runtime Error, I don't know why. Please tell me the bug in my code. Thank you!

  • Vote: I like it
  • -19
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +4 Vote: I do not like it

First of all, your approach is wrong.

Your program gives rte because you have R[g1] instead of R[r1] in many places.

I have no clue how you didn't spot this yourself.