제출 #670521

#제출 시각아이디문제언어결과실행 시간메모리
670521SanguineChameleonFinancial Report (JOI21_financial)C++17
100 / 100
558 ms46960 KiB
// BEGIN BOILERPLATE CODE

#include <bits/stdc++.h>
using namespace std;

#ifdef KAMIRULEZ
	const bool kami_loc = true;
	const long long kami_seed = 69420;
#else
	const bool kami_loc = false;
	const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif

const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);

long long rand_range(long long rmin, long long rmax) {
	uniform_int_distribution<long long> rdist(rmin, rmax);
	return rdist(kami_gen);
}

long double rand_real(long double rmin, long double rmax) {
	uniform_real_distribution<long double> rdist(rmin, rmax);
	return rdist(kami_gen);
}

void file_io(string fi, string fo) {
	if (fi != "" && (!kami_loc || fi == kami_fi)) {
		freopen(fi.c_str(), "r", stdin);
	}
	if (fo != "" && (!kami_loc || fo == kami_fo)) {
		freopen(fo.c_str(), "w", stdout);
	}
}

void set_up() {
	if (kami_loc) {
		file_io(kami_fi, kami_fo);
	}
	ios_base::sync_with_stdio(0);
	cin.tie(0);
}

void just_do_it();

void just_exec_it() {
	if (kami_loc) {
		auto pstart = chrono::steady_clock::now();
		just_do_it();
		auto pend = chrono::steady_clock::now();
		long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
		string bar(50, '=');
		cout << '\n' << bar << '\n';
		cout << "Time: " << ptime << " ms" << '\n';
	}
	else {
		just_do_it();
	}
}

int main() {
	set_up();
	just_exec_it();
	return 0;
}

// END BOILERPLATE CODE

// BEGIN MAIN CODE

const int ms = 3e5 + 20;
int a[ms];
int par[ms];
int de[ms];
int mx[ms];
pair<int, int> p[ms];
int pt[ms];
set<int> s;
vector<int> rem[ms];
int prv[ms];
int tr[ms * 4];

int root(int u) {
	if (par[u] == -1) {
		return u;
	}
	return root(par[u]);
}

void update(int u, int v) {
	int ru = root(u);
	int rv = root(v);
	if (ru == rv) {
		return;
	}
	if (de[ru] > de[rv]) {
		swap(ru, rv);
	}
	par[ru] = rv;
	if (de[ru] == de[rv]) {
		de[rv]++;
	}
	mx[rv] = max(mx[rv], mx[ru]);
}

void update(int cc, int lt, int rt, int px, int pp) {
	if (lt == rt) {
		tr[cc] = pp;
		return;
	}
	int mt = (lt + rt) / 2;
	if (px <= mt) {
		update(cc * 2, lt, mt, px, pp);
	}
	else {
		update(cc * 2 + 1, mt + 1, rt, px, pp);
	}
	tr[cc] = max(tr[cc * 2], tr[cc * 2 + 1]);
}

int get(int cc, int lt, int rt, int px) {
	if (px == 0) {
		return 0;
	}
	if (lt == rt) {
		return tr[cc];
	}
	int mt = (lt + rt) / 2;
	if (px <= mt) {
		return get(cc * 2, lt, mt, px);
	}
	else {
		return max(tr[cc * 2], get(cc * 2 + 1, mt + 1, rt, px));
	}
}

void just_do_it() {
	int n, d;
	cin >> n >> d;
	s.insert(0);
	s.insert(n + 1);
	for (int i = 0; i <= n + 1; i++) {
		par[i] = -1;
		mx[i] = i;
	}
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		p[i] = {a[i], -i};
	}
	sort(p + 1, p + n + 1);
	for (int i = 1; i <= n; i++) {
		int px = -p[i].second;
		auto it = s.insert(px).first;
		it--;
		int lt = (*it);
		it++;
		it++;
		int rt = (*it);
		if (px - lt <= d) {
			update(lt, px);
		}
		if (rt - px <= d) {
			update(px, rt);
		}
		rt = min(mx[root(px)] + d, n);
		rem[rt].push_back(px);
	}
	for (int i = 1; i <= n; i++) {
		p[i] = {a[i], i};
	}
	sort(p + 1, p + n + 1);
	for (int i = 1; i <= n; i++) {
		pt[p[i].second] = i;
		if (p[i].first != p[i - 1].first) {
			prv[i] = i - 1;
		}
		else {
			prv[i] = prv[i - 1];
		}
	}
	int res = 0;
	for (int i = 1; i <= n; i++) {
		int px = pt[i];
		int cr = get(1, 1, n, prv[px]) + 1;
		res = max(res, cr);
		update(1, 1, n, px, cr);
		for (auto x: rem[i]) {
			update(1, 1, n, pt[x], 0);
		}
	}
	cout << res;
}

// END MAIN CODE

컴파일 시 표준 에러 (stderr) 메시지

Main.cpp: In function 'void file_io(std::string, std::string)':
Main.cpp:30:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |   freopen(fi.c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:33:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |   freopen(fo.c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...