Submission #532099

# Submission time Handle Problem Language Result Execution time Memory
532099 2022-03-02T05:48:57 Z fhvirus Naan (JOI19_naan) C++17
29 / 100
4000 ms 9400 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));
	OI(AI(perm));

	for (const int& i: perm) {
		Frac need = ori[i] + f(i, x.back());
		Frac tmp = f(i, x.back());
		debug(i, need.a, need.b);
		debug(ori[i].a, ori[i].b);
		debug(x.back().a, x.back().b, tmp.a, tmp.b);
		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);

	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);

	do if (solve(perm)) exit(0);
	while (next_permutation(AI(perm)));

	cout << -1 << '\n';
	return 0;
}

Compilation message

naan.cpp: In function 'bool solve(const std::vector<int>&)':
naan.cpp:16:17: warning: statement has no effect [-Wunused-value]
   16 | #define OI(...) 0
      |                 ^
naan.cpp:51:2: note: in expansion of macro 'OI'
   51 |  OI(AI(perm));
      |  ^~
naan.cpp:15:20: warning: statement has no effect [-Wunused-value]
   15 | #define debug(...) 0
      |                    ^
naan.cpp:56:3: note: in expansion of macro 'debug'
   56 |   debug(i, need.a, need.b);
      |   ^~~~~
naan.cpp:15:20: warning: statement has no effect [-Wunused-value]
   15 | #define debug(...) 0
      |                    ^
naan.cpp:57:3: note: in expansion of macro 'debug'
   57 |   debug(ori[i].a, ori[i].b);
      |   ^~~~~
naan.cpp:15:20: warning: statement has no effect [-Wunused-value]
   15 | #define debug(...) 0
      |                    ^
naan.cpp:58:3: note: in expansion of macro 'debug'
   58 |   debug(x.back().a, x.back().b, tmp.a, tmp.b);
      |   ^~~~~
naan.cpp:55:8: warning: variable 'tmp' set but not used [-Wunused-but-set-variable]
   55 |   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 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 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 1 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 0 ms 332 KB Output is correct
9 Correct 2 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 0 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 332 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 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 0 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 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 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 1 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 0 ms 332 KB Output is correct
24 Correct 2 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 0 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 332 KB Output is correct
31 Correct 1 ms 460 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 1 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 0 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 Execution timed out 4072 ms 9400 KB Time limit exceeded
44 Halted 0 ms 0 KB -