제출 #797804

#제출 시각아이디문제언어결과실행 시간메모리
797804hugo_pm메기 농장 (IOI22_fish)C++17
100 / 100
232 ms41756 KiB
#include <bits/stdc++.h>
#include "fish.h"
using namespace std;
	
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))
	
template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }
	
using ll = long long;
using v64 = vector<ll>;
using a2v64 = array<v64, 2>;
using p64 = pair<ll, ll>;
	
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
	bool first = true;
	string res = "[";
	for (const auto &x : v) {
		if (!first)
			res += ", ";
		first = false;
		res += to_string(x);
	}
	res += "]";
	return res;
}
	
template <typename A, typename B>
string to_string(pair<A, B> p) {
	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
	
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
	cout << ' ' << to_string(H);
	dbg_out(T...);
}
	
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

const ll INF = 3e18;
const int INCR = 0, DECR = 1;
ll max_weights(int szCarre, int totFish, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
	++szCarre;
	vector<vector<p64>> fish(szCarre);
	vector<vector<int>> niceTake(szCarre);
	rep(i, 0, totFish) {
		++X[i];
		fish[X[i]].emplace_back(Y[i], W[i]);
		// Y+1 pour couvrir Y
		niceTake[X[i]-1].push_back(Y[i] + 1);
		if (X[i]+1 < szCarre) {
			niceTake[X[i]+1].push_back(Y[i] + 1);
		}
	}
	v64 nbNice(szCarre), nbFish(szCarre);
	vector<v64> prefSum(szCarre);
	rep(col, 0, szCarre) {
		auto &nice = niceTake[col];
		nice.push_back(0);
		sort(all(nice));
		nice.resize(unique(all(nice)) - begin(nice));
		nbNice[col] = sz(nice);
		sort(all(fish[col]));
		nbFish[col] = sz(fish[col]);
		auto &ss = prefSum[col];
		ss.push_back(0);
		for (auto [_, w] : fish[col]) {
			ss.push_back(ss.back() + w);
		}
	}
	a2v64 dpOld;
	dpOld[0] = dpOld[1] = v64(nbNice[0], -INF);
	dpOld[INCR][0] = dpOld[DECR][0] = 0;
	rep(col, 1, szCarre) {
		a2v64 dpNew;
		dpNew[0] = dpNew[1] = v64(nbNice[col], -INF);
		dpNew[INCR][0] = *max_element(all(dpOld[DECR]));
		if (true) {
			ll maxTr = -INF;
			int ptrOld = 0, ptrOldFish = 0, ptrNewFish = 0;
			/// retourne la somme des poissons < upTo dans col-1
			auto sfIncr = [&] (int &ptr, int upTo) {
				while (ptr < nbFish[col-1] && fish[col-1][ptr].first < upTo) {
					++ptr;
				}
				return prefSum[col-1][ptr];
			};
			rep(ptrNew, 0, nbNice[col]) {
				int newTake = niceTake[col][ptrNew];
				while (ptrOld < nbNice[col-1] && niceTake[col-1][ptrOld] <= newTake) {
					int oldTake = niceTake[col-1][ptrOld];
					chmax(maxTr, dpOld[INCR][ptrOld] - sfIncr(ptrOldFish, oldTake));
					++ptrOld;
				}
				chmax(dpNew[INCR][ptrNew], maxTr + sfIncr(ptrNewFish, newTake));
			}
		}
		if (true) {
			ll maxTr = -INF;
			int ptrOld = nbNice[col-1]-1, ptrOldFish = nbFish[col], ptrNewFish = nbFish[col];
			/// retourne la somme des poissons < upTo dans col
			auto sfDecr = [&] (int &ptr, int upTo) {
				while (ptr > 0 && fish[col][ptr-1].first >= upTo) {
					--ptr;
				}
				return prefSum[col][ptr];
			};
			for (int ptrNew = nbNice[col]-1; ptrNew >= 0; --ptrNew) {
				int newTake = niceTake[col][ptrNew];
				while (ptrOld >= 0 && niceTake[col-1][ptrOld] >= newTake) {
					int oldTake = niceTake[col-1][ptrOld];
					chmax(maxTr, dpOld[DECR][ptrOld] + sfDecr(ptrOldFish, oldTake));
					--ptrOld;
				}
				chmax(dpNew[DECR][ptrNew], maxTr - sfDecr(ptrNewFish, newTake));
			}
		}
		rep(take, 0, nbNice[col]) {
			chmax(dpNew[DECR][take], dpNew[INCR][take]);
		}
		dpOld = dpNew;
	}
	return *max_element(all(dpOld[DECR]));
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...