# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1092841 |
2024-09-25T08:39:36 Z |
Pannda |
Naan (JOI19_naan) |
C++17 |
|
1 ms |
604 KB |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
struct Fraction {
long long a = 0, b = 1;
bool operator<(const Fraction &other) const & {
return a * other.b < other.a * b;
}
bool operator<=(const Fraction &other) const & {
return !(other < *this);
}
Fraction operator*(const Fraction &other) const & {
long long A = a * other.a;
long long B = b * other.b;
long long G = abs(__gcd(A, B));
return {A / G, B / G};
}
Fraction operator+(const Fraction &other) const & {
long long B = b * other.b;
long long A = a * other.b + other.a * b;
long long G = abs(__gcd(A, B));
return {A / G, B / G};
}
Fraction operator-(Fraction other) {
other.a *= -1;
return (*this) + other;
}
};
int n, m;
cin >> n >> m;
vector<vector<int>> v(n, vector<int>(m));
vector<vector<int>> f(n, vector<int>(m + 1, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int x;
cin >> x;
v[i][j] = x;
f[i][j + 1] = f[i][j] + x;
}
}
vector<Fraction> key;
vector<int> perm;
vector<bool> chosen(n, false);
Fraction cur = {0, 1};
vector<int> ptr(n, 0); // possible cut for i is in [ptr[i], ptr[i] + 1)
for (int t = 0; t < n - 1; t++) {
auto [nxt, i] = [&]() -> pair<Fraction, int> {
const long long INF = 1e18;
pair<Fraction, int> res = make_pair(Fraction{INF, 1}, -1);
for (int i = 0; i < n; i++) {
if (chosen[i]) continue;
Fraction nxt = [&]() -> Fraction {
auto F = [&](Fraction x) -> Fraction {
int j = x.a / x.b;
if (j < m) return Fraction{f[i][j], 1} + Fraction{v[i][j], 1} * Fraction{x.a % x.b, x.b};
return Fraction{f[i][m], 1};
};
Fraction req = F(Fraction{m, 1}) * Fraction{1, n};
while (true) {
if (F(Fraction{ptr[i] + 1, 1}) - F(cur) <= req) { // then is not enough
ptr[i]++;
continue;
}
Fraction rem = req + F(cur) - F(Fraction{ptr[i], 1});
return Fraction{ptr[i], 1} + rem * Fraction{1, v[i][ptr[i]]};
}
}();
res = min(res, make_pair(nxt, i));
}
return res;
}();
key.push_back(nxt);
perm.push_back(i);
cur = nxt;
chosen[i] = true;
}
for (int i = 0; i < n; i++) {
if (!chosen[i]) perm.push_back(i);
}
for (Fraction frac : key) {
cout << frac.a << ' ' << frac.b << '\n';
}
for (int i : perm) {
cout << i + 1 << ' ';
}
cout << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |