AtCoder Grand Contest 012

Submission #1351945

Source codeソースコード

#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 % MOD;
}


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);
    }
    map<int, vector<int>> get_groups() {
        map<int, vector<int>> res;
        rep(i, p.size()) {
            res[root(i)].push_back(i);
        }
        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.second) cnts[in[i].color]++;
            ans *= mod_factorial(g.second.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

Task問題 D - Colorful Balls
User nameユーザ名 043_cormoran
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 WA
Score得点 0
Source lengthソースコード長 4167 Byte
File nameファイル名
Exec time実行時間 ms
Memory usageメモリ使用量 -

Test case

Set

Set name Score得点 / Max score Cases
Sample - 00_example_01.txt,00_example_02.txt,00_example_03.txt
All 0 / 1000 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

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
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 AC 2 ms 1152 KB
02.txt AC 2 ms 1024 KB
03.txt AC 7 ms 1664 KB
04.txt AC 2 ms 1024 KB
05.txt AC 4 ms 1408 KB
06.txt AC 2 ms 1152 KB
07.txt AC 102 ms 10232 KB
08.txt AC 2 ms 1152 KB
09.txt AC 28 ms 3452 KB
10.txt AC 27 ms 3200 KB
11.txt AC 2 ms 1152 KB
12.txt AC 2 ms 1024 KB
13.txt AC 8 ms 1920 KB
14.txt AC 2 ms 1024 KB
15.txt AC 44 ms 5504 KB
16.txt AC 118 ms 13824 KB
17.txt AC 13 ms 2048 KB
18.txt AC 36 ms 4224 KB
19.txt AC 79 ms 8448 KB
20.txt WA
21.txt AC 268 ms 29304 KB
22.txt AC 277 ms 30588 KB
23.txt WA
24.txt AC 284 ms 32512 KB
25.txt WA
26.txt WA
27.txt AC 278 ms 26368 KB
28.txt WA
29.txt WA
30.txt AC 296 ms 33280 KB
31.txt WA
32.txt AC 268 ms 29180 KB
33.txt WA
34.txt WA
35.txt WA
36.txt WA
37.txt WA
38.txt WA
39.txt WA
40.txt WA
41.txt WA
42.txt AC 275 ms 30328 KB
43.txt AC 272 ms 31868 KB
44.txt WA
45.txt AC 287 ms 33280 KB
46.txt WA
47.txt WA
48.txt AC 147 ms 19704 KB
49.txt WA
50.txt WA
51.txt WA
52.txt WA
53.txt WA
54.txt WA