제출 #1254066

#제출 시각아이디문제언어결과실행 시간메모리
1254066ofozNaval battle (CEOI24_battle)C++20
6 / 100
1206 ms1114112 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O2") 
using namespace std;
#define vi vector<int>
#define pi pair<int, int>
#define ppi pair<pair<int, int>, int>
#define mtis multiset<pi>
#define endl '\n'
struct Ship {
    int x, y, ind;
    char dir;
};

int man_dist(Ship s1, Ship s2) {
    return abs(s1.x - s2.x) + abs(s1.y - s2.y);
}

bool is_valid(Ship s1, Ship s2) {
    bool res = s1.dir != s2.dir;

    if (s1.dir == 'E') {
        res &= (s2.x > s1.x) && s2.dir != 'E';
        if (s2.dir == 'W') res &= (s2.y == s1.y);
        else res &= (abs(s1.x - s2.x) == abs(s1.y - s2.y));
    }

    if (s1.dir == 'W') {
        res &= (s2.x < s1.x) && s2.dir != 'W';
        if (s2.dir == 'E') res &= (s2.y == s1.y);
        else res &= (abs(s1.x - s2.x) == abs(s1.y - s2.y));
    }

    if (s1.dir == 'N') {
        res &= (s2.y < s1.y) && s2.dir != 'N';
        if (s2.dir == 'S') res &= (s2.x == s1.x);
        else res &= (abs(s1.x - s2.x) == abs(s1.y - s2.y));
    }

    if (s1.dir == 'S') {
        res &= (s2.y > s1.y) && s2.dir != 'S';
        if (s2.dir == 'N') res &= (s2.x == s1.x);
        else res &= (abs(s1.x - s2.x) == abs(s1.y - s2.y));
    }
    return res;
}

void solve() {
    int n;
    cin >> n;
    vector<Ship> ships(n);
    for (int i = 0; i < n; i++) {
        cin >> ships[i].x >> ships[i].y >> ships[i].dir;
        ships[i].ind = i+1;
    }
    unordered_set<int> surviving;
    for (int i = 1; i <= n; i++) surviving.insert(i);
    vector<pair<int, pair<Ship, Ship>>> ord;
    for (int i = 0; i < n; i++) {
        for (int j = i+1; j < n; j++) {
            if (!is_valid(ships[i], ships[j])) continue;
            ord.push_back({man_dist(ships[i], ships[j]), {ships[i], ships[j]}});
        }
    }
    vector<int> sunk(n+1, 0);
    sort(ord.begin(), ord.end(), [](pair<int, pair<Ship, Ship>> p1, pair<int, pair<Ship, Ship>> p2) {
        return p1.first < p2.first;
    });
    for (pair<int, pair<Ship, Ship>> p : ord) {
        int dist = p.first;
        Ship s1 = p.second.first, s2 = p.second.second;
        if (!sunk[s1.ind] && !sunk[s2.ind]) {
            surviving.erase(s1.ind);
            surviving.erase(s2.ind);
            sunk[s1.ind] = sunk[s2.ind] = dist;
        }
        else if (sunk[s1.ind] && sunk[s2.ind]) continue;
        else if (sunk[s1.ind] == dist) {
            surviving.erase(s2.ind);
            sunk[s2.ind] = dist;
        }
        else if (sunk[s2.ind] == dist) {
            surviving.erase(s1.ind);
            sunk[s1.ind] = dist;
        }
    }
    for (int p : surviving) {
        cout << p << endl;
    }
}


signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    bool test = 0;
    int t;
    if (!test) t = 1;
    else cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...