Submission #994488
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;
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;
rep(i, N) {
auto tmp = (M - X[i]) % M;
if(st[tmp] == 0) continue;
// 自分は選んだので消す
st[tmp]--;
// 自分の端数対称が一致するものを見つけては消す
if(st[X[i]] > 0) {
st[X[i]]--;
ans ++;
}
else {
// 復活
st[tmp]++;
}
}
// 残った (M - X[i]) % M を用いて、元の数が同じものを出来る限り生成する
// MODが同じ値の個数のうち、既に使ったMODの数をできるだけ各値の奇数の端数から削る。
// MODが同じ値の個数 / 2を単純に足した場合、解以上になることを確かめる
for(auto const& e: st) {
// 残った (M - X[i]) % M のmapについて、
// まず、X[i] % M に直す。この個数は未使用分の個数。そのうち使えるものだけを取り除きたい。
// 上で、奇数の端数から削っていた、と考える
// つまり、元のMOD一致ペア数から、奇数の数/2を引いて、0とmaxを取る。これをmpnumとする。
// mpnumをe.second/2から引いて足し合わせる
auto x = (M - e.first) % M;
auto mpnum = max(0, (tmpST[x] - oddcnt[x] / 2));
ans += e.second / 2 - mpnum;
}
cout << abs(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 |
3586 Byte |
Status |
WA |
Exec Time |
167 ms |
Memory |
30372 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 |
WA |
2 ms |
256 KB |
01-02.txt |
WA |
52 ms |
5260 KB |
01-03.txt |
WA |
53 ms |
5004 KB |
01-04.txt |
WA |
53 ms |
4480 KB |
01-05.txt |
WA |
54 ms |
5120 KB |
01-06.txt |
WA |
56 ms |
4480 KB |
01-07.txt |
WA |
58 ms |
4608 KB |
01-08.txt |
WA |
75 ms |
4864 KB |
01-09.txt |
WA |
75 ms |
4992 KB |
01-10.txt |
WA |
106 ms |
14092 KB |
01-11.txt |
WA |
156 ms |
23460 KB |
01-12.txt |
WA |
38 ms |
1792 KB |
01-13.txt |
AC |
38 ms |
1792 KB |
01-14.txt |
WA |
52 ms |
5912 KB |
01-15.txt |
WA |
55 ms |
6144 KB |
01-16.txt |
WA |
60 ms |
6400 KB |
01-17.txt |
WA |
72 ms |
6656 KB |
01-18.txt |
WA |
75 ms |
6656 KB |
01-19.txt |
WA |
83 ms |
8832 KB |
01-20.txt |
WA |
167 ms |
30372 KB |
01-21.txt |
WA |
43 ms |
2048 KB |
01-22.txt |
WA |
43 ms |
1920 KB |
01-23.txt |
WA |
45 ms |
1920 KB |
01-24.txt |
WA |
49 ms |
2304 KB |
01-25.txt |
WA |
53 ms |
3328 KB |
01-26.txt |
WA |
54 ms |
3328 KB |
01-27.txt |
WA |
107 ms |
22052 KB |
01-28.txt |
WA |
8 ms |
1664 KB |
01-29.txt |
WA |
3 ms |
512 KB |
01-30.txt |
WA |
2 ms |
256 KB |
sample-01.txt |
AC |
2 ms |
256 KB |
sample-02.txt |
AC |
2 ms |
256 KB |