Submission #1351902


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;

class ModInt{
  public:
    static const ll MOD = 1000000007LL; 
    ll x;
    ModInt():x(0){};
    ModInt(ll x){
        while(x < 0) x += MOD;
        x %= MOD;
        this->x = x;
    }
    ModInt& operator += (const ModInt &l){ x += l.x; if( x >= MOD) x -= MOD; return *this; }
    ModInt& operator -= (const ModInt &l){ x -= l.x; if( x < 0 ) x += MOD; return *this; }
    ModInt& operator *= (const ModInt &l){ x = (x * l.x) % MOD; return *this; }
    ModInt operator +(const ModInt &l) const { return ModInt( x + l.x); }
    ModInt operator -(const ModInt &l) const { return ModInt( x - l.x); }
    ModInt operator *(const ModInt &l) const { return ModInt( x * l.x); }
};

// @warning rの部分はmodとってはいけない
ModInt pow(const ModInt &n, ll r){
    if(r == 0) return ModInt(1);
    ModInt ret = pow(n, r / 2);
    ret *= ret;
    if(r % 2 != 0) ret = ret * n;
    return ret;
}

// @waring nはMODと互いに素
ModInt inverse(const ModInt &n){
    return pow(n, ModInt::MOD - 2);
}

ModInt factorial(const ModInt &n){
    ll ret =1;
    for(ll i=n.x; i>1; i--){
        ret*=i;
    }
    return ModInt(ret);
}

ostream& operator << (std::ostream& os,const ModInt& a){ os << a.x; return os;}
istream& operator >> (std::istream& is,ModInt& a){ is >> a.x; return is;}

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個
        rep(i, N) {
            int c = in[i].color, w = in[i].weight;
            //debug(c); debug(w);
            
            if(lightest.count(c) and w + lightest[c].weight <= X) {
                uf.unite(in[i].index, lightest[c].index);
            } else {
                lightest[c] = in[i];
            }
            bool same = false;
            rep(j, lightest2.size()) {
                // cerr << "cmp to " << lightest2[j].weight << endl;
                if(c != lightest2[j].color and w + lightest2[j].weight <= Y) {

                    uf.unite(in[i].index, lightest2[j].index);
                }
                if(lightest2[j].color == c) same = true;
            }
            if(lightest.size() < 2 and !same) lightest2.push_back(in[i]);
        }
        auto groups = uf.get_groups();

        ModInt ans(1);
        //debug("each group");
        for(auto &g : groups) {
            //debug(g);
            map<int, int> cnts;
            for(int i : g) cnts[in[i].color]++;
            ans *= factorial(g.size());
            for(auto p : cnts) {
                // debug(p.second);
                ans *= inverse(factorial(p.second));
            }
        }

        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 5192 Byte
Status WA
Exec Time 2105 ms
Memory 16016 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 1000
Status
AC × 3
AC × 4
WA × 13
TLE × 40
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 1 ms 256 KB
00_example_02.txt AC 1 ms 256 KB
00_example_03.txt AC 1 ms 256 KB
01.txt WA 2 ms 256 KB
02.txt WA 1 ms 256 KB
03.txt WA 20 ms 768 KB
04.txt WA 1 ms 256 KB
05.txt WA 11 ms 512 KB
06.txt AC 2 ms 384 KB
07.txt TLE 2104 ms 4984 KB
08.txt TLE 2103 ms 256 KB
09.txt WA 498 ms 1920 KB
10.txt WA 285 ms 1792 KB
11.txt WA 2 ms 256 KB
12.txt WA 1 ms 256 KB
13.txt WA 22 ms 1024 KB
14.txt WA 1 ms 256 KB
15.txt WA 336 ms 3968 KB
16.txt TLE 2104 ms 4992 KB
17.txt WA 74 ms 1024 KB
18.txt TLE 2104 ms 2236 KB
19.txt TLE 2104 ms 3328 KB
20.txt TLE 2104 ms 11900 KB
21.txt TLE 2104 ms 14712 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 12028 KB
26.txt TLE 2104 ms 16016 KB
27.txt TLE 2104 ms 11392 KB
28.txt TLE 2105 ms 12536 KB
29.txt TLE 2104 ms 12408 KB
30.txt TLE 2104 ms 13568 KB
31.txt TLE 2105 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 2104 ms 5112 KB
36.txt TLE 2104 ms 5624 KB
37.txt TLE 2104 ms 5116 KB
38.txt TLE 2104 ms 5112 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 13564 KB
43.txt TLE 2104 ms 14460 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 2104 ms 5880 KB
48.txt TLE 2104 ms 4732 KB
49.txt TLE 2104 ms 5240 KB
50.txt TLE 2104 ms 10496 KB
51.txt TLE 2104 ms 9984 KB
52.txt TLE 2104 ms 5368 KB
53.txt TLE 2104 ms 5368 KB
54.txt TLE 2104 ms 5368 KB