Skip to content

Commit 161ea9c

Browse files
authored
Merge pull request #407 from hitonanode/add-tests
Add tests
2 parents b532f69 + f185b84 commit 161ea9c

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

combinatorial_opt/linear_sum_assignment.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ for (int t = 0; t < k; ++t) {
6060
## 問題例
6161

6262
- [Library Checker: Assignment Problem](https://judge.yosupo.jp/problem/assignment)
63+
- [No.3306 Life is Easy? - yukicoder](https://yukicoder.me/problems/no/3306)
6364

6465
## 文献・リンク集
6566

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#define PROBLEM "https://yukicoder.me/problems/no/3306"
2+
3+
#include "../linear_sum_assignment.hpp"
4+
5+
#include <iostream>
6+
#include <vector>
7+
using namespace std;
8+
9+
int main() {
10+
cin.tie(nullptr), ios::sync_with_stdio(false);
11+
12+
int N, M;
13+
cin >> N >> M;
14+
vector A(N, vector<int>(M));
15+
for (auto &v : A) {
16+
for (auto &x : v) cin >> x;
17+
}
18+
19+
const int K = N / 2;
20+
vector mat(K, vector<long long>(K));
21+
for (int i = 0; i < K; ++i) {
22+
for (int j = 0; j < K; ++j) {
23+
int best = 0;
24+
for (int k = 0; k < M; ++k) best = max(best, A.at(N - 1 - j).at(k) - A.at(i).at(k));
25+
mat.at(i).at(j) = -best;
26+
}
27+
}
28+
29+
auto res = linear_sum_assignment::solve(K, K, mat);
30+
cout << -res.opt << '\n';
31+
}

utilities/floor_sum.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// 1 <= m < 2e32 (if Int is long long)
77
// 0 <= a, b < m
88
// Complexity: O(lg(m))
9+
// (Int, Unsigned) = (long long, unsigned long long), (__int128_t, __uint128_t)
910
template <class Int, class Unsigned> Int floor_sum(Int n, Int m, Int a, Int b) {
1011
static_assert(-Int(1) < 0, "Int must be signed");
1112
static_assert(-Unsigned(1) > 0, "Unsigned must be unsigned");
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#define PROBLEM "https://yukicoder.me/problems/no/3307"
2+
#include "../floor_sum.hpp"
3+
#include <iostream>
4+
using namespace std;
5+
6+
int main() {
7+
cin.tie(nullptr), ios::sync_with_stdio(false);
8+
9+
long long A, B, C, D;
10+
cin >> A >> B >> C >> D;
11+
12+
if (A * D == B * C) {
13+
cout << "-1\n";
14+
return 0;
15+
}
16+
17+
if (A * D > C * B) swap(A, C), swap(B, D);
18+
19+
// round(i * A / B) = floor(iA/B + 1/2) = floor((2iA + B)/2B)
20+
21+
// A/B < C/D
22+
// i(A/B) + 1 <= i(C/D)
23+
// i(C/D - A/B) >= 1
24+
// i(BC - AD) >= BD
25+
26+
const auto det = B * C - A * D;
27+
const auto ub = (B * D + det - 1) / det;
28+
29+
const auto ret = floor_sum<__int128_t, __uint128_t>(ub, D * 2, C * 2, D) -
30+
floor_sum<__int128_t, __uint128_t>(ub, B * 2, A * 2, B);
31+
cout << (long long)(ub - ret - 1) << '\n';
32+
}

0 commit comments

Comments
 (0)