Submission #1351942


Source Code Expand

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

using pii = pair<int,int>;
using ll = long long;
#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)
#define all(v) v.begin(),v.end()
#define debug(x) cerr << #x << " : " << x << endl

template<class T> bool set_min(T &a, const T &b) { return a > b  ? a = b, true : false; }
template<class T> bool set_max(T &a, const T &b) { return a < b  ? a = b, true : false; }
// vector
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
template<class T> ostream& operator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << "\t" << t; return os << endl; }
// pair
template<class T, class U> ostream& operator << (ostream &os , const pair<T, U> &v) { return os << "<" << v.first << ", " << v.second << ">"; }

const int INF = 1 << 30;
const ll INFL = 1LL << 60;
const ll MOD = 1000000000 + 7;

ll mod_pow(const ll &n, ll r){
    if(r == 0) return 1;
    ll ret = mod_pow(n, r / 2);
    ret *= ret;
    ret %= MOD;
    if(r % 2 != 0) ret = ret * n;
    ret %= MOD;
    return ret;
}

// @waring nはMODと互いに素
ll mod_inverse(ll n) {
    return mod_pow(n, MOD - 2);
}

ll mod_factorial(ll n) {
    static vector<ll> memo(100001);
    if(memo[n] > 0) return memo[n];
    if(n <= 1) return 1;
    ll res = n * mod_factorial(n - 1);
    return memo[n] = res;
}


struct UnionFind{
    int n;
    vector<int> p;
    UnionFind(int nn) {
        n = nn;
        p.resize(n);
        rep(i, n) p[i] = i;
    }
    int root(int x) {
        return p[x] == x ? x : (p[x] = root(p[x]));
    }
    void unite(int x,int y) {
        x = root(x); y = root(y);
        if(x != y) p[y] = x;
    }
    bool query(int x,int y){
        return root(x) == root(y);
    }
    vector<vector<int>> get_groups() {
        vector<vector<int>> res;
        vector<bool> visited(p.size());
        rep(i, p.size()) {
            if(!visited[i]) {
                res.push_back(vector<int>());
                rep(j, p.size()) if(query(i, j)) {
                    res.back().push_back(j);
                    visited[j] = true;
                }
            }
        }
        return res;
    }
};

struct S {
    int color, weight, index;    
};
bool operator < (const S &a, const S &b) {
    return a.weight < b.weight;
}
ostream& operator << (ostream &os, const S &a) {
    return os << a.index << " c:" << a.color << " w:" << a.weight << endl;
}

class Solver {
  public:
    bool solve() {
        int N, X, Y; cin >> N >> X >> Y;
        vector<S> in(N);
        rep(i, N) cin >> in[i].color >> in[i].weight;
        sort(all(in));
        rep(i, N) in[i].index = i;
        //cerr << in << endl;
        UnionFind uf(N);
        map<int, S> lightest; // C -> W
        vector<S> lightest2; // 色の異なるボール2個
        // O(n log n)
        rep(i, N) {
            int c = in[i].color, w = in[i].weight;
            //debug(c); debug(w);
            // O(log n)
            if(lightest.count(c) and w + lightest[c].weight <= X) {
                uf.unite(in[i].index, lightest[c].index);
            } else {
                lightest[c] = in[i];
            }
            // O(1)
            rep(j, lightest2.size()) {
                if(c != lightest2[j].color and w + lightest2[j].weight <= Y) {
                    uf.unite(in[i].index, lightest2[j].index);
                }
            }
            if(lightest2.size() == 0) lightest2.push_back(in[i]);
            else if(lightest2.size() == 1 and lightest2[0].color != c) lightest2.push_back(in[i]);
        }
        // O(n)
        auto groups = uf.get_groups();

        ll ans = 1;
        for(auto &g : groups) {
            map<int, int> cnts;
            for(int i : g) cnts[in[i].color]++;
            ans *= mod_factorial(g.size());
            ans %= MOD;
            for(auto p : cnts) {
                // debug(p.second);
                ans *= mod_inverse(mod_factorial(p.second));
                ans %= MOD;
            }
        }

        cout << ans << endl;
        
        return 0;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Solver s;
    s.solve();
    return 0;
}

Submission Info

Submission Time
Task D - Colorful Balls
User cormoran
Language C++14 (GCC 5.4.1)
Score 0
Code Size 4394 Byte
Status WA
Exec Time 2107 ms
Memory 15480 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 1000
Status
AC × 3
AC × 4
WA × 15
TLE × 38
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt
All 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 2 ms 1024 KB
00_example_02.txt AC 2 ms 1024 KB
00_example_03.txt AC 2 ms 1024 KB
01.txt WA 2 ms 1152 KB
02.txt WA 2 ms 1024 KB
03.txt WA 21 ms 1664 KB
04.txt WA 2 ms 1024 KB
05.txt WA 11 ms 1408 KB
06.txt AC 3 ms 1152 KB
07.txt TLE 2104 ms 4984 KB
08.txt WA 2 ms 1152 KB
09.txt WA 478 ms 3328 KB
10.txt WA 287 ms 2944 KB
11.txt WA 2 ms 1152 KB
12.txt WA 2 ms 1024 KB
13.txt WA 23 ms 1792 KB
14.txt WA 2 ms 1024 KB
15.txt WA 338 ms 5376 KB
16.txt TLE 2103 ms 4992 KB
17.txt WA 75 ms 2048 KB
18.txt WA 1502 ms 3388 KB
19.txt TLE 2103 ms 3328 KB
20.txt TLE 2104 ms 11900 KB
21.txt TLE 2104 ms 12796 KB
22.txt TLE 2104 ms 11644 KB
23.txt TLE 2104 ms 13560 KB
24.txt TLE 2104 ms 11776 KB
25.txt TLE 2104 ms 13944 KB
26.txt TLE 2104 ms 14072 KB
27.txt TLE 2104 ms 11392 KB
28.txt TLE 2104 ms 12536 KB
29.txt TLE 2103 ms 14068 KB
30.txt TLE 2104 ms 11776 KB
31.txt TLE 2104 ms 13048 KB
32.txt TLE 2104 ms 11776 KB
33.txt TLE 2104 ms 12536 KB
34.txt TLE 2104 ms 11896 KB
35.txt TLE 2103 ms 5112 KB
36.txt TLE 2104 ms 5624 KB
37.txt TLE 2104 ms 5116 KB
38.txt TLE 2103 ms 6772 KB
39.txt TLE 2104 ms 4600 KB
40.txt TLE 2104 ms 12920 KB
41.txt TLE 2104 ms 12796 KB
42.txt TLE 2104 ms 15480 KB
43.txt TLE 2104 ms 12544 KB
44.txt TLE 2104 ms 14328 KB
45.txt TLE 2104 ms 12544 KB
46.txt TLE 2104 ms 12796 KB
47.txt TLE 2103 ms 7412 KB
48.txt TLE 2107 ms 4732 KB
49.txt TLE 2104 ms 5240 KB
50.txt TLE 2104 ms 10876 KB
51.txt TLE 2103 ms 11904 KB
52.txt TLE 2104 ms 5368 KB
53.txt TLE 2104 ms 5368 KB
54.txt TLE 2104 ms 5368 KB