Rezultati

Up. imeNalogaJezikRezultatČas oddaje
ayy-2018 Fruit Ninja C++ 100/100OK 13. okt '18 @ 12:54

Test Točke Porabljen spomin Porabljen čas Status
#1 11/11 3,215 MiB 0,004 s OK
#2 11/11 3,059 MiB 0,000 s OK
#3 11/11 3,066 MiB 0,004 s OK
#4 11/11 3,215 MiB 0,000 s OK
#5 11/11 3,215 MiB 0,010 s OK
#6 11/11 3,066 MiB 0,004 s OK
#7 11/11 3,059 MiB 0,010 s OK
#8 11/11 3,215 MiB 0,004 s OK
#9 12/12 3,207 MiB 0,004 s OK

Ocenjevani program (main.cpp):
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

struct vec {
    double x, y;

    double length() {
        return sqrt(x * x + y * y);
    }

    vec normalize() {
        vec v;
        double len = this->length();
        v.x = this->x / len;
        v.y = this->y / len;

        return v;
    }
};

double dotProduct(const vec& v1, const vec& v2) {
    return v1.x * v2.x + v1.y * v2.y;
}

vec subtractVec(const vec& v1, const vec& v2) {
    vec v3;
    v3.x = v1.x - v2.x;
    v3.y = v1.y - v2.y;
    return v3;
}

vec addVec(const vec& v1, const vec& v2) {
    vec v3;
    v3.x = v1.x + v2.x;
    v3.y = v1.y + v2.y;
    return v3;
}

int main() {
    int ok = 0;

    double xa, ya, xb, yb;
    cin >> xa >> ya >> xb >> yb;

    vec t1, t2;
    t1.x = xa; t1.y = ya;
    t2.x = xb; t2.y = yb;

    int n;
    cin >> n;

    double x, y, r;
    while (n--) {
        cin >> x >> y >> r;

        vec t3;
        t3.x = x; t3.y = y;

        vec v1, v2;
        v1 = subtractVec(t2, t1);
        v2 = subtractVec(t3, t1);
        vec v1n = v1.normalize();

        double s = dotProduct(v1n, v2);

        if (s < 0) {
            vec v = subtractVec(t1, t3);
            double len = v.length();

            if (len <= r) {
                ok++;
            }
        } else if (s > v1.length()) {
            vec v = subtractVec(t2, t3);
            double len = v.length();

            if (len <= r) {
                ok++;
            }
        } else {
            vec v;
            v.x = v1n.x * s;
            v.y = v1n.y * s;

            v = addVec(v, t1);

            vec w = subtractVec(v, t3);
            double len = w.length();

            if (len <= r) {
                ok++;
            }
        }
    }


    cout << ok << endl;
    return 0;
}