Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions combinatorial_opt/linear_sum_assignment.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ for (int t = 0; t < k; ++t) {
## 問題例

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

## 文献・リンク集

Expand Down
31 changes: 31 additions & 0 deletions combinatorial_opt/test/linear_sum_assignment.yuki3306.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#define PROBLEM "https://yukicoder.me/problems/no/3306"

#include "../linear_sum_assignment.hpp"

#include <iostream>
#include <vector>
using namespace std;

int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);

int N, M;
cin >> N >> M;
vector A(N, vector<int>(M));
for (auto &v : A) {
for (auto &x : v) cin >> x;
}

const int K = N / 2;
vector mat(K, vector<long long>(K));
for (int i = 0; i < K; ++i) {
for (int j = 0; j < K; ++j) {
int best = 0;
for (int k = 0; k < M; ++k) best = max(best, A.at(N - 1 - j).at(k) - A.at(i).at(k));
mat.at(i).at(j) = -best;
}
}

auto res = linear_sum_assignment::solve(K, K, mat);
cout << -res.opt << '\n';
}
1 change: 1 addition & 0 deletions utilities/floor_sum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// 1 <= m < 2e32 (if Int is long long)
// 0 <= a, b < m
// Complexity: O(lg(m))
// (Int, Unsigned) = (long long, unsigned long long), (__int128_t, __uint128_t)
template <class Int, class Unsigned> Int floor_sum(Int n, Int m, Int a, Int b) {
static_assert(-Int(1) < 0, "Int must be signed");
static_assert(-Unsigned(1) > 0, "Unsigned must be unsigned");
Expand Down
32 changes: 32 additions & 0 deletions utilities/test/floor_sum.int128.yuki3307.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#define PROBLEM "https://yukicoder.me/problems/no/3307"
#include "../floor_sum.hpp"
#include <iostream>
using namespace std;

int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);

long long A, B, C, D;
cin >> A >> B >> C >> D;

if (A * D == B * C) {
cout << "-1\n";
return 0;
}

if (A * D > C * B) swap(A, C), swap(B, D);

// round(i * A / B) = floor(iA/B + 1/2) = floor((2iA + B)/2B)

// A/B < C/D
// i(A/B) + 1 <= i(C/D)
// i(C/D - A/B) >= 1
// i(BC - AD) >= BD

const auto det = B * C - A * D;
const auto ub = (B * D + det - 1) / det;

const auto ret = floor_sum<__int128_t, __uint128_t>(ub, D * 2, C * 2, D) -
floor_sum<__int128_t, __uint128_t>(ub, B * 2, A * 2, B);
cout << (long long)(ub - ret - 1) << '\n';
}