Submission #994858
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) { cout << #a << " = " << a << endl; }
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }
typedef long long ll;
int const inf = 1<<29;
namespace tree {
struct union_find {
vector<int> par, rank, size;
int compnum;
union_find(int N) {
compnum = N;
par.resize(N), rank.resize(N), size.resize(N);
for(int i=0; i<N; i++) {
par[i] = i;
rank[i] = 0;
size[i] = 1;
}
}
int root(int x) {
return par[x] == x ? x : par[x] = root(par[x]);
}
void unite(int x, int y) {
x = root(x), y = root(y);
if(x == y) return;
if(rank[x] < rank[y]) {
par[x] = y, size[y] += size[x];
} else {
par[y] = x, size[x] += size[y];
if(rank[x] == rank[y]) rank[x]++;
}
compnum--;
}
int operator[](int x) { return root(x); }
void operator()(int x, int y) { return unite(x, y); }
bool same(int x, int y) { return root(x) == root(y); }
int size_of(int x) { return size[root(x)]; }
int num_of_comps() { return compnum; }
};
}
int main() {
int N, M; cin >> N >> M;
vector<int> X(N);
rep(i, N) {
cin >> X[i];
}
sort(all(X));
tree::union_find uf(N);
// 元のindexが同じUFをする
rep(i, N - 1) {
if(X[i] == X[i + 1]) {
uf(i, i + 1);
}
}
map<int, int> st;
// MODが同じ数で、元の値ごとに、数の総数が奇数かどうかを調べる。
unordered_map<int, unordered_map<int, bool>> odd;
rep(i, N) {
odd[X[i] % M][X[i]] ^= 1;
// Mでmodをとり
X[i] %= M;
// 端数の一致する対称を詰める
st[(M - X[i]) % M] ++;
}
auto tmpST = st; // 端数の対称のerase前を保持する
unordered_map<int, int> oddcnt;
for(auto const& e: odd) {
int ocnt = 0;
for(auto const& u: e.second) {
if(u.second) ocnt++;
}
oddcnt[e.first] = ocnt;
}
ll ans = 0;
unordered_map<int, int> selectedXCnt;
rep(i, N) {
auto tmp = (M - X[i]) % M;
if(st[tmp] == 0) continue;
// 自分は選んだので消す
st[tmp]--;
// 自分の端数対称が一致するものを見つけては消す
if(st[X[i]] > 0) {
selectedXCnt[(M-tmp)%M]++;
st[X[i]]--;
ans ++;
}
else {
// 復活
st[tmp]++;
}
}
for(auto const& e: st) {
auto mx = e.first;
auto a = oddcnt[(M-mx) % M];
auto b = selectedXCnt[(M-mx)%M];
ans += (e.second - (b-a/2)*2) / 2;
}
cout << ans << endl;
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Pair Cards |
User |
motxx |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
2972 Byte |
Status |
WA |
Exec Time |
181 ms |
Memory |
34468 KB |
Judge Result
Set Name |
sample |
all |
Score / Max Score |
0 / 0 |
0 / 700 |
Status |
|
|
Set Name |
Test Cases |
sample |
sample-01.txt, sample-02.txt |
all |
sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt |
Case Name |
Status |
Exec Time |
Memory |
01-01.txt |
AC |
3 ms |
256 KB |
01-02.txt |
WA |
54 ms |
5260 KB |
01-03.txt |
WA |
55 ms |
5004 KB |
01-04.txt |
WA |
54 ms |
4480 KB |
01-05.txt |
WA |
56 ms |
5120 KB |
01-06.txt |
WA |
58 ms |
4480 KB |
01-07.txt |
WA |
59 ms |
4608 KB |
01-08.txt |
WA |
76 ms |
4864 KB |
01-09.txt |
WA |
77 ms |
5120 KB |
01-10.txt |
WA |
114 ms |
15628 KB |
01-11.txt |
WA |
168 ms |
26148 KB |
01-12.txt |
AC |
43 ms |
1792 KB |
01-13.txt |
AC |
39 ms |
1792 KB |
01-14.txt |
AC |
54 ms |
5912 KB |
01-15.txt |
AC |
57 ms |
6272 KB |
01-16.txt |
WA |
62 ms |
6400 KB |
01-17.txt |
WA |
74 ms |
6656 KB |
01-18.txt |
AC |
77 ms |
6784 KB |
01-19.txt |
AC |
87 ms |
9344 KB |
01-20.txt |
WA |
181 ms |
34468 KB |
01-21.txt |
WA |
44 ms |
2048 KB |
01-22.txt |
WA |
44 ms |
1920 KB |
01-23.txt |
WA |
46 ms |
1920 KB |
01-24.txt |
WA |
51 ms |
2304 KB |
01-25.txt |
WA |
55 ms |
3456 KB |
01-26.txt |
WA |
56 ms |
3456 KB |
01-27.txt |
AC |
110 ms |
23844 KB |
01-28.txt |
WA |
9 ms |
1792 KB |
01-29.txt |
WA |
4 ms |
512 KB |
01-30.txt |
AC |
3 ms |
256 KB |
sample-01.txt |
WA |
3 ms |
256 KB |
sample-02.txt |
AC |
3 ms |
256 KB |