Submission #1351945
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 % 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 Info
Submission Time | |
---|---|
Task | D - Colorful Balls |
User | cormoran |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 4167 Byte |
Status | WA |
Exec Time | 296 ms |
Memory | 33280 KB |
Judge Result
Set Name | Sample | All | ||||||
---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 1000 | ||||||
Status |
|
|
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 | 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 | 257 ms | 25208 KB |
21.txt | AC | 268 ms | 29304 KB |
22.txt | AC | 277 ms | 30588 KB |
23.txt | WA | 264 ms | 22900 KB |
24.txt | AC | 284 ms | 32512 KB |
25.txt | WA | 277 ms | 24312 KB |
26.txt | WA | 269 ms | 22516 KB |
27.txt | AC | 278 ms | 26368 KB |
28.txt | WA | 288 ms | 20980 KB |
29.txt | WA | 279 ms | 23284 KB |
30.txt | AC | 296 ms | 33280 KB |
31.txt | WA | 266 ms | 21364 KB |
32.txt | AC | 268 ms | 29180 KB |
33.txt | WA | 263 ms | 21492 KB |
34.txt | WA | 244 ms | 19188 KB |
35.txt | WA | 89 ms | 10096 KB |
36.txt | WA | 158 ms | 9972 KB |
37.txt | WA | 94 ms | 10104 KB |
38.txt | WA | 88 ms | 10100 KB |
39.txt | WA | 115 ms | 9592 KB |
40.txt | WA | 257 ms | 20980 KB |
41.txt | WA | 265 ms | 26744 KB |
42.txt | AC | 275 ms | 30328 KB |
43.txt | AC | 272 ms | 31868 KB |
44.txt | WA | 259 ms | 24180 KB |
45.txt | AC | 287 ms | 33280 KB |
46.txt | WA | 259 ms | 25976 KB |
47.txt | WA | 96 ms | 13172 KB |
48.txt | AC | 147 ms | 19704 KB |
49.txt | WA | 114 ms | 15224 KB |
50.txt | WA | 172 ms | 21752 KB |
51.txt | WA | 233 ms | 32256 KB |
52.txt | WA | 122 ms | 15356 KB |
53.txt | WA | 122 ms | 15356 KB |
54.txt | WA | 122 ms | 15484 KB |