Submission #532102

# Submission time Handle Problem Language Result Execution time Memory
532102 2022-03-02T05:55:38 Z fhvirus Naan (JOI19_naan) C++17
29 / 100
260 ms 15852 KB
// Knapsack DP is harder than FFT.
#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll; typedef pair<int, int> pii;
#define pb emplace_back
#define AI(x) begin(x),end(x)
#define ff first
#define ss second
#ifdef OWO
#define debug(args...) LKJ("\033[1;32m[ " + string(#args) + " ]\033[0m", args)
template <class I> void LKJ(I&&x) { cerr << x << endl; }
template <class I, class...T> void LKJ(I&&x, T&&...t) { cerr << x << ", "; LKJ(t...); }
template <class I> void OI(I a, I b) { while (a != b) cerr << *a << " \n"[(a = next(a)) == b]; }
#else
#define debug(...) 0
#define OI(...) 0
#endif

struct Frac {
	ll a, b;
	Frac () = default;
	Frac (ll _a, ll _b): a(_a), b(_b) { shrink(); }
	void shrink() { ll g = gcd(a, b); a /= g; b /= g; }
	const ll toi() const { return a / b; }
	const Frac operator + (const Frac& o) const
	{ return Frac(a * o.b + o.a * b, b * o.b); }
	const Frac operator - (const Frac& o) const
	{ return Frac(a * o.b - o.a * b, b * o.b); }
	const Frac operator * (const ll& val) const
	{ return Frac(a * val, b); }
	const bool operator < (const Frac& o) const
	{ return a * o.b < o.a * b; }
};

const int kN = 2002;
int N, L, V[kN][kN];
ll pre[kN][kN];
Frac ori[kN];

Frac f(int i, Frac j) {
	ll k = j.toi();
	ll sum1 = pre[i][k];
	j.a -= k * j.b;
	Frac sum2 = j * V[i][k + 1];
	sum2.a += sum1 * sum2.b;
	return sum2;
}

bool solve(const vector<int>& perm) {
	vector<Frac> x(1, Frac(0, 1));

	for (const int& i: perm) {
		Frac need = ori[i] + f(i, x.back());
		Frac tmp = f(i, x.back());
		if (f(i, Frac(L, 1)) < need)
			return false;
		int k = x.back().toi();
		while (f(i, Frac(k + 1, 1)) < need) ++k;
		need = need - f(i, Frac(k, 1));
		Frac nxt = need;
		nxt.b *= V[i][k + 1];
		nxt.shrink();
		x.emplace_back(nxt.a + k * nxt.b, nxt.b);
	}

	for (int i = 1; i < N; ++i)
		cout << x[i].a << ' ' << x[i].b << '\n';
	for (const int& i: perm)
		cout << i << " \n"[i == perm.back()];
	return true;
}

signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	auto start = chrono::system_clock::now();

	cin >> N >> L;
	for (int i = 1; i <= N; ++i) {
		ori[i] = Frac(0, 1);
		for (int j = 1; j <= L; ++j) {
			cin >> V[i][j];
			pre[i][j] = pre[i][j - 1] + V[i][j];
			ori[i].a += V[i][j];
		}
		ori[i].b = N;
		ori[i].shrink();
	}

	vector<int> perm(N);
	iota(AI(perm), 1);

	random_device rd;
	mt19937 mt(rd());

	while (true) {
		auto cur = chrono::system_clock::now();
		std::chrono::duration<double> dur = cur - start;
		if (dur.count() > 3.5) break;
		shuffle(AI(perm), mt);
		if (solve(perm)) break;
	}

	return 0;
}

Compilation message

naan.cpp: In function 'bool solve(const std::vector<int>&)':
naan.cpp:54:8: warning: variable 'tmp' set but not used [-Wunused-but-set-variable]
   54 |   Frac tmp = f(i, x.back());
      |        ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 2 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 460 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 1 ms 460 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 1 ms 460 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 460 KB Output is correct
37 Correct 1 ms 460 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Runtime error 260 ms 15852 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -