Submission #97735

#TimeUsernameProblemLanguageResultExecution timeMemory
97735luciocfGlobal Warming (CEOI18_glo)C++14
48 / 100
1253 ms263168 KiB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e5+10;
const int maxv = 2e9+10;

struct Node
{
	int v;
	Node *l, *r;

	Node()
	{
		v = 0;
		l = r = NULL;
	}
};

int n, x;
int num[maxn];
int pref[maxn], suf[maxn];

int get(Node *t) {return (t ? t->v : 0);}

void upd(Node *node, int l, int r, int pos, int v)
{
	if (l == r)
	{
		node->v = max(v, node->v);
		return;
	}

	int mid = (l+r)>>1;

	if (pos <= mid)
	{
		if (!node->l) node->l = new Node();
		upd(node->l, l, mid, pos, v);
	}
	else
	{
		if (!node->r) node->r = new Node();
		upd(node->r, mid+1, r, pos, v);
	}

	node->v = max(get(node->l), get(node->r));
}

int query(Node *node, int l, int r, int a, int b)
{
	if (!node || l > b || r < a) return 0;
	if (l >= a && r <= b) return node->v;

	int mid = (l+r)>>1;

	int p1 = query(node->l, l, mid, a, b);
	int p2 = query(node->r, mid+1, r, a, b);

	return max(p1, p2);
}

int main(void)
{
	scanf("%d %d", &n, &x);

	Node *t = new Node();

	for (int i = 1; i <= n; i++)
		scanf("%d", &num[i]);

	for (int i = 1; i <= n; i++)
	{
		int val = 0;

		if (num[i] > 1) 
			val = query(t, 1, maxv-1, 1, num[i]-1);

		pref[i] = val+1;

		upd(t, 1, maxv-1, num[i], pref[i]);
	}

	t = new Node();

	for (int i = n; i >= 1; i--)
	{
		int val = query(t, 1, maxv-1, num[i]+1, maxv-1);

		suf[i] = val+1;

		upd(t, 1, maxv-1, num[i], suf[i]);
	}

	int ans = 0;

	t = new Node();

	for (int i = 1; i <= n; i++)
	{
		int val = 0;
		if (num[i]+x > 1) val = query(t, 1, maxv-1, 1, num[i]+x-1);

		ans = max(ans, suf[i]+val);

		upd(t, 1, maxv-1, num[i], pref[i]);
	}

	t = new Node();

	for (int i = n; i >= 1; i--)
	{
		int val = query(t, 1, maxv-1, max(1, num[i]-x+1), maxv-1);

		ans = max(ans, pref[i]+val);

		upd(t, 1, maxv-1, num[i], suf[i]);
	}

	printf("%d\n", ans);
}

Compilation message (stderr)

glo.cpp: In function 'int main()':
glo.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &x);
  ~~~~~^~~~~~~~~~~~~~~~~
glo.cpp:70:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &num[i]);
   ~~~~~^~~~~~~~~~~~~~~
#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...