Submission #310941

#TimeUsernameProblemLanguageResultExecution timeMemory
310941FalconGlobal Warming (CEOI18_glo)C++17
100 / 100
131 ms9432 KiB
#pragma GCC optimize("O2")

#include <bits/stdc++.h>

#ifdef DEBUG
#include "debug.hpp"
#endif

using namespace std;

#define all(c)              (c).begin(), (c).end()
#define rall(c)             (c).rbegin(), (c).rend()
#define traverse(c, it)     for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N)           for(int i = 0; i < (N); ++i)
#define rep1(i, N)          for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e)       for(int i = (s); i <= (e); ++i)
#define rep3(i, s, e, d)    for(int i = (s); (d) >= 0 ? i <= (e) : i >= (e); i += (d))
#define pb push_back


#ifdef DEBUG
#define debug(x...)         { dbg::depth++; string dbg_vals = dbg::to_string(x); dbg::depth--; dbg::fprint(__func__, __LINE__, #x, dbg_vals); }
#define light_debug(x)      { dbg::light = 1; dbg::dout << __func__ << ":" << __LINE__ << "  " << #x << " = " << x << endl; dbg::light = 0; }
#else
#define debug(x...)         42
#define light_debug(x)      42
#endif


template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }

template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template<typename IT, typename T>
vi lis_ending_at(IT s, IT e, const T& comp) {
	int n{e - s};
	vi v, dp(n);

	rep(i, n) {
		int x = *(s + i);
		auto it = lower_bound(all(v), x, comp);
		dp[i] = it - v.begin() + 1;
		if(it == v.end()) v.pb(x);
		else *it = x;
	}

	return dp;
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

#ifdef DEBUG
	freopen("debug", "w", stderr);
#endif

	int n, x; cin >> n >> x;
	vi a(n); rep(i, n) cin >> a[i];

	vi end_at{lis_ending_at(all(a), less<int>())};
	vi start_at{lis_ending_at(rall(a), greater<int>())}; reverse(all(start_at));

	debug(start_at, end_at);

	map<int, int> mp; mp[INT_MAX] = 0;

	vi tmp;
	int ans{0};
	rep3(i, n - 1, 0, -1) {
		auto it = mp.upper_bound(a[i]);
		ckmax(ans, end_at[i] + it->second);
		
		it = mp.upper_bound(a[i] + x);
		if(it != mp.end() && it->second >= start_at[i]) 
			continue;
		
		while(it != mp.begin()) { 
			--it;
			if(it->second <= start_at[i]) tmp.pb(it->first);
			else break;
		}
		for(auto v : tmp) mp.erase(v);
		tmp.clear();

		mp[a[i] + x] = start_at[i];
	}

	cout << ans << '\n';

	

#ifdef DEBUG
	dbg::dout << "\nExecution time: " << clock() << "ms\n";
#endif
	return 0;
}

Compilation message (stderr)

glo.cpp: In function 'int main()':
glo.cpp:25:29: warning: statement has no effect [-Wunused-value]
   25 | #define debug(x...)         42
      |                             ^~
glo.cpp:71:2: note: in expansion of macro 'debug'
   71 |  debug(start_at, end_at);
      |  ^~~~~
glo.cpp: In instantiation of 'vi lis_ending_at(IT, IT, const T&) [with IT = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; T = std::less<int>; vi = std::vector<int>]':
glo.cpp:68:45:   required from here
glo.cpp:42:10: warning: narrowing conversion of '__gnu_cxx::operator-<int*, std::vector<int> >(e, s)' from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' [-Wnarrowing]
   42 |  int n{e - s};
      |        ~~^~~
glo.cpp: In instantiation of 'vi lis_ending_at(IT, IT, const T&) [with IT = std::reverse_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >; T = std::greater<int>; vi = std::vector<int>]':
glo.cpp:69:51:   required from here
glo.cpp:42:10: warning: narrowing conversion of 'std::operator-<__gnu_cxx::__normal_iterator<int*, std::vector<int> >, __gnu_cxx::__normal_iterator<int*, std::vector<int> > >(e, s)' from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' [-Wnarrowing]
#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...