Submission #532113

# Submission time Handle Problem Language Result Execution time Memory
532113 2022-03-02T06:05:45 Z fhvirus Naan (JOI19_naan) C++17
0 / 100
1 ms 332 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() {
		if (a == 0 and b == 0) { b = 1; return; }
		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;
}

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

	int cnt = 0;
	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 cnt;
		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);
		++cnt;
	}

	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()];
	exit(0);
	return N;
}

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()); 
	uniform_int_distribution<int> rnd(1, N);

	shuffle(AI(perm), mt);
	int lst = solve(perm);

	while (true) {
		auto cur = chrono::system_clock::now();
		std::chrono::duration<double> dur = cur - start;
		if (dur.count() > 3.0) break;
		int u = rnd(mt), v;
		do v = rnd(mt); while (v == u);
		swap(perm[u], perm[v]);

		int sol = solve(perm);

		if (sol < lst or (sol == lst and (mt() & 1)))
			swap(perm[u], perm[v]);
		lst = sol;
	}

	return 0;
}

Compilation message

naan.cpp: In function 'int solve(const std::vector<int>&)':
naan.cpp:58:8: warning: variable 'tmp' set but not used [-Wunused-but-set-variable]
   58 |   Frac tmp = f(i, x.back());
      |        ^~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 332 KB Integer parameter [name=P_i] equals to 0, violates the range [1, 2]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 1 ms 332 KB X_i is not increasing
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 332 KB Integer parameter [name=P_i] equals to 0, violates the range [1, 2]
2 Halted 0 ms 0 KB -