QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#132510#6570. Who Watches the Watchmen?Sorting#Compile Error//C++239.5kb2023-07-30 03:42:552023-07-30 03:42:56

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-30 03:42:56]
  • 评测
  • [2023-07-30 03:42:55]
  • 提交

answer

#include <bits/stdc++.h>
#include <bits/extc++.h>

using namespace std;

typedef long long ll;
template<class T> void check_min(T &a, const T &b){ a = (a < b) ? a : b; }
template<class T> void check_max(T &a, const T &b){ a = (a > b) ? a : b; }
#define all(x) (x).begin(), (x).end()

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1000 + 3;

const ll INF = numeric_limits<ll>::max() / 4;
typedef vector<ll> VL;

struct MCMF {
    int N;
    vector<vi> ed, red;
    vector<VL> cap, flow, cost;
    vi seen;
    VL dist, pi;
    vector<pii> par;

    MCMF(int N) :
        N(N), ed(N), red(N), cap(N, VL(N)), flow(cap), cost(cap),
        seen(N), dist(N), pi(N), par(N) {}

    void addEdge(int from, int to, ll cap, ll cost) {
        this->cap[from][to] = cap;
        this->cost[from][to] = cost;
        ed[from].push_back(to);
        red[to].push_back(from);
    }

    void path(int s) {
        fill(all(seen), 0);
        fill(all(dist), INF);
        dist[s] = 0; ll di;

        __gnu_pbds::priority_queue<pair<ll, int>> q;
        vector<decltype(q)::point_iterator> its(N);
        q.push({0, s});

        auto relax = [&](int i, ll cap, ll cost, int dir) {
            ll val = di - pi[i] + cost;
            if (cap && val < dist[i]) {
                dist[i] = val;
                par[i] = {s, dir};
                if (its[i] == q.end()) its[i] = q.push({-dist[i], i});
                else q.modify(its[i], {-dist[i], i});
            }
        };

        while (!q.empty()) {
            s = q.top().second; q.pop();
            seen[s] = 1; di = dist[s] + pi[s];
            for (int i : ed[s]) if (!seen[i])
                relax(i, cap[s][i] - flow[s][i], cost[s][i], 1);
            for (int i : red[s]) if (!seen[i])
                relax(i, flow[i][s], -cost[i][s], 0);
        }
        rep(i,0,N) pi[i] = min(pi[i] + dist[i], INF);
    }

    pair<ll, ll> maxflow(int s, int t) {
        ll totflow = 0, totcost = 0;
        while (path(s), seen[t]) {
            ll fl = INF;
            for (int p,r,x = t; tie(p,r) = par[x], x != s; x = p)
                fl = min(fl, r ? cap[p][x] - flow[p][x] : flow[x][p]);
            totflow += fl;
            for (int p,r,x = t; tie(p,r) = par[x], x != s; x = p)
                if (r) flow[p][x] += fl;
                else flow[x][p] -= fl;
        }
        rep(i,0,N) rep(j,0,N) totcost += cost[i][j] * flow[i][j];
        return {totflow, totcost};
    }

    // If some costs can be negative, call this before maxflow:
    void setpi(int s) { // (otherwise, leave this out)
        fill(all(pi), INF); pi[s] = 0;
        int it = N, ch = 1; ll v;
        while (ch-- && it--)
            rep(i,0,N) if (pi[i] != INF)
                for (int to : ed[i]) if (cap[i][to])
                    if ((v = pi[i] + cost[i][to]) < pi[to])
                        pi[to] = v, ch = 1;
        assert(it >= 0); // negative cost cycle
    }
};

MCMF *mcmf = nullptr;

template<class T> struct Point3D {
    typedef Point3D P;
    typedef const P& R;
    T x, y, z;
    explicit Point3D(T x=0, T y=0, T z=0) : x(x), y(y), z(z) {}
    bool operator<(R p) const {
        return tie(x, y, z) < tie(p.x, p.y, p.z); }
    bool operator==(R p) const {
        return tie(x, y, z) == tie(p.x, p.y, p.z); }
    P operator+(R p) const { return P(x+p.x, y+p.y, z+p.z); }
    P operator-(R p) const { return P(x-p.x, y-p.y, z-p.z); }
    P operator*(T d) const { return P(x*d, y*d, z*d); }
    P operator/(T d) const { return P(x/d, y/d, z/d); }
    T dot(R p) const { return x*p.x + y*p.y + z*p.z; }
    P cross(R p) const {
        return P(y*p.z - z*p.y, z*p.x - x*p.z, x*p.y - y*p.x);
    }
    T dist2() const { return x*x + y*y + z*z; }
    double dist() const { return sqrt((double)dist2()); }
    //Azimuthal angle (longitude) to x-axis in interval [-pi, pi]
    double phi() const { return atan2(y, x); } 
    //Zenith angle (latitude) to the z-axis in interval [0, pi]
    double theta() const { return atan2(sqrt(x*x+y*y),z); }
    P unit() const { return *this/(T)dist(); } //makes dist()=1
    //returns unit vector normal to *this and p
    P normal(P p) const { return cross(p).unit(); }
    //returns point rotated 'angle' radians ccw around axis
    P rotate(double angle, P axis) const {
        double s = sin(angle), c = cos(angle); P u = axis.unit();
        return u*dot(u)*(1-c) + (*this)*c - cross(u)*s;
    }
};

typedef Point3D<ll> P;

int n;
P p[N], v[N];

bool on_line(P a, P b, P c){
    return (b - a).cross(c - a) == P();
}

bool same_direction(P a, P b){
    return a.dot(b) > 0ll;
}

ll get_sign(P a, P dir_a, P b, P dir_b){
    P n2 = dir_b.cross(dir_a.cross(dir_b));
    ll sign_1 = (b - a).dot(n2);
    sign_1 = (sign_1 > 0) - (sign_1 < 0);
    sign_1 *= dir_a.dot(n2);
    sign_1 = (sign_1 > 0) - (sign_1 < 0);
    return sign_1;
}

bool intersect(P a, P dir_a, P b, P dir_b){
    P n = dir_a.cross(dir_b);
    if(n == P())
        return false;
    if((b - a).dot(n) != 0)
        return false;

    ll sign_1 = get_sign(a, dir_a, b, dir_b);
    ll sign_2 = get_sign(b, dir_b, a, dir_a);
    return (sign_1 > 0) && (sign_2 > 0);
}

bool b[N][N];
int points_to[N];

vector<vi> adj;
vi btoa;

void solve(){
    for(int i = 0; i < n; ++i){
        for(int j = i + 1; j < n; ++j){
            bool ok = true;
            for(int j2 = 0; j2 < n; ++j2){
                if(j2 == i || j2 == j) continue;
                if(on_line(p[i], p[j], p[j2]) && same_direction(p[j2] - p[i], p[j] - p[j2])){
                    ok = false;
                    break;
                }
            }
            b[i][j] = b[j][i] = ok;
        }
    }

    for(int i = 0; i < n; ++i){
        points_to[i] = -1;

        for(int j = 0; j < n; ++j){
            if(i == j) continue;
            if(!b[i][j]) continue;
            if(on_line(p[i], p[j], p[i] + v[i]) && same_direction(v[i], p[j] - p[i])){
                points_to[i] = j;
                break;
            }
        }
    }

    mcmf = new MCMF(2 * n + 2);

    adj.resize(n);
    btoa.resize(n, -1);

    for(int i = 0; i < n; ++i){
        if(points_to[i] != -1){
            mcmf->addEdge(i, n + points_to[i], 1, 0);
        }
        for(int j = 0; j < n; ++j){
            if(j != i && j != points_to[i])
                mcmf->addEdge(i, n + j, 1, 1);
        }
        mcmf->addEdge(2 * n, i, 1, 0);
        mcmf->addEdge(i + n, 2 * n + 1, 1, 0);
    }

    cout << mcmf->maxflow(2 * n, 2 * n + 1).second << "\n";
}

ll solve_line(const vector<P> &p, const vector<P> &v,  P v_chosen){
    ll cnt_in_dir = 0, cnt_not = 0;
    for(int j = 0; j < (int)v.size() - 1; ++j){
        if(on_line(p[0], p[1], p[0] + v[j]) && same_direction(v[j], p[1] - p[0])){
            ++cnt_in_dir;
        }
        else{
            ++cnt_not;
        }
    }

    bool d_chosen = !on_line(p[0], p[1], p[0] + v_chosen);
    bool d_back = !on_line(p[0], p[1], p[0] + v.back());
    ll ans = cnt_not;

    if(d_chosen && d_back){
        if(!intersect(p.back(), v.back(), p[0], P()-v_chosen))
            ++ans;
    }
    else{
        if(d_chosen || d_back){
            ++ans;
        }
        else{
            ans += 2;
        }
    }

    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> p[i].x >> p[i].y >> p[i].z;
        cin >> v[i].x >> v[i].y >> v[i].z;
    }

    if(n == 1){
        cout << "-1\n";
        return 0;
    }

    if(n == 2){
        
        cout << ans << "\n";
        return 0;
    }

    bool one_line = true;
    for(int i = 2; i < n; ++i)
        one_line &= on_line(p[0], p[1], p[i]);

    if(one_line){        
        if(n == 2){
            vector<pair<P, P>> new_points;
            for(int j = 0; j < n; ++j){
                new_points.push_back({p[j], v[j]});
            }
            sort(all(new_points));

            ll ans = 0;
            for(int i = 0; i < n; i += 2){
                ll add = 2;
                P p0 = new_points[i].first;
                P v0 = new_points[i].second;
                P p1 = new_points[i + 1].first;
                P v1 = new_points[i + 1].second;

                add -= on_line(p0, p1, p0 + v0) && same_direction(p1 - p0, v0);
                add -= on_line(p1, p0, p1 + v1) && same_direction(p0 - p1, v0);
                ans += add;
            }
            cout << ans << "\n";
            return 0;
        }

        ll ans = 1000;
        for(int chosen = 0; chosen < n; ++chosen){
            vector<pair<P, P>> new_points;
            for(int j = 0; j < n; ++j){
                if(chosen == j) continue;
                new_points.push_back({p[j], v[j]});
            }
            sort(all(new_points));

            for(int dir = 0; dir <= 1; ++dir, reverse(all(new_points))){                
                vector<P> new_p, new_v;
                for(int i = 0; i < new_points.size(); ++i){
                    new_p.push_back(new_points[i].first);
                    new_v.push_back(new_points[i].second);
                }
                ll curr = solve_line(new_p, new_v, v[chosen]);
                check_min(ans, curr);
            }
        }

        cout << 1000 + ans << "\n";
        return 0;
    }

    solve();
}
/*
2
0 0 0 0 0 1
0 0 1 0 -1 -1
*/

详细

answer.code:12: warning: "all" redefined
   12 | #define all(x) begin(x), end(x)
      | 
answer.code:9: note: this is the location of the previous definition
    9 | #define all(x) (x).begin(), (x).end()
      | 
answer.code: In function ‘int main()’:
answer.code:271:17: error: ‘ans’ was not declared in this scope; did you mean ‘abs’?
  271 |         cout << ans << "\n";
      |                 ^~~
      |                 abs