Submission #73925

# Submission time Handle Problem Language Result Execution time Memory
73925 2018-08-29T09:31:43 Z haitun Global Warming (CEOI18_glo) C++14
100 / 100
210 ms 4604 KB
#include <bits/stdc++.h>
//#include "functions.h"
#define FOR(x,y) for(int x = 0; x < y; x++)
#define ALLR(x) x.begin(),x.end()
#define con continue
#define ll long long
#define LINF LLONG_MAX
#define INF INT_MAX
#define pii pair<int,int>
#define vi vector <int>
#define pb push_back
#define F first
#define S second
#define len(x) x.length()
#define sz(x) x.size()
#define SEE(v)	for(auto x : v)	cout << x << " "; cout << endl;
using namespace std;

int n, b;

int CeilIndex(vi v, int l, int r, int key){

	while(r - l > 1)
	{
		int m = l + (r - l) / 2;
		if (v[m] >= key)	r = m;
		else				l = m;
	}

	return r;
}

int RCeilIndex(vi v, int l, int r, int key) {

	while(r - l > 1)
	{
		int m = l + (r - l) / 2;
		if(v[m] <= key)	r = m;
		else			l = m;
	}

	return r;
}

int LongestIncreasingSubsequenceLength(vi v,vi &front) {

	if(v.size() == 0)	return 0;

	vi tail(v.size(), INF);
	int length = 1;

	front[0] = 0;
	tail[0] = v[0];

	for(int i = 1; i < v.size(); i++)
	{
		int pos = upper_bound(ALLR(tail), v[i] + b - 1) - tail.begin();
		front[i] = pos;

		if(v[i] < tail[0])					tail[0] = v[i];
		else if(v[i] > tail[length - 1])	tail[length++] = v[i];
		else								tail[lower_bound(tail.begin(), tail.begin() + length - 1, v[i]) - tail.begin()] = v[i];
	}

	return length;
}

int LongestDecreasingSubsequenceLength(vi v, vi &back) {

	if (v.size() == 0)	return 0;

	vi tail(v.size(), -1);
	int length = 1;

	back[0] = 1;
	tail[0] = v[0];

	for(int i = 1; i < v.size(); i++)
	{
		//int pos = RCeilIndex(tail, -1, length - 1, v[i]);
		int pos = lower_bound(tail.begin(), tail.begin() + length - 1, v[i], greater <int> ()) - tail.begin();

		if(v[i] > tail[0])					tail[0] = v[i], back[i] = 1;
		else if (v[i] < tail[length - 1])	tail[length++] = v[i], back[i] = length;
		else								tail[pos] = v[i], back[i] = pos + 1;

		//if you are not the head, length = 0;

		//back[i] = length;
	}

	return length;
}

int main() {

	//freopen("test.txt", "r",stdin);
	//int n, b;
	cin >> n >> b;

	vi t(n), front(n), back(n);
	FOR(j, n)   cin >> t[j];
	
	int ans = LongestIncreasingSubsequenceLength(t, front);

	if(b == 0)
	{
		cout << LongestIncreasingSubsequenceLength(t, front);
		return 0;
	}

	reverse(ALLR(t));
	LongestDecreasingSubsequenceLength(t, back);
	reverse(ALLR(back));
	//SEE(front);
	//SEE(back);
	for(int j = n - 1; j >= 0; j--)	ans = max(ans, front[j] + back[j]);
	cout << ans;
}

Compilation message

glo.cpp: In function 'int LongestIncreasingSubsequenceLength(std::vector<int>, std::vector<int>&)':
glo.cpp:55:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 1; i < v.size(); i++)
                 ~~^~~~~~~~~~
glo.cpp: In function 'int LongestDecreasingSubsequenceLength(std::vector<int>, std::vector<int>&)':
glo.cpp:78:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 1; i < v.size(); i++)
                 ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 248 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 560 KB Output is correct
4 Correct 3 ms 560 KB Output is correct
5 Correct 2 ms 560 KB Output is correct
6 Correct 3 ms 560 KB Output is correct
7 Correct 3 ms 592 KB Output is correct
8 Correct 3 ms 592 KB Output is correct
9 Correct 2 ms 592 KB Output is correct
10 Correct 2 ms 592 KB Output is correct
11 Correct 3 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 248 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 560 KB Output is correct
4 Correct 3 ms 560 KB Output is correct
5 Correct 2 ms 560 KB Output is correct
6 Correct 3 ms 560 KB Output is correct
7 Correct 3 ms 592 KB Output is correct
8 Correct 3 ms 592 KB Output is correct
9 Correct 2 ms 592 KB Output is correct
10 Correct 2 ms 592 KB Output is correct
11 Correct 3 ms 592 KB Output is correct
12 Correct 2 ms 592 KB Output is correct
13 Correct 3 ms 592 KB Output is correct
14 Correct 3 ms 592 KB Output is correct
15 Correct 3 ms 624 KB Output is correct
16 Correct 2 ms 624 KB Output is correct
17 Correct 3 ms 624 KB Output is correct
18 Correct 2 ms 624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 248 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 560 KB Output is correct
4 Correct 3 ms 560 KB Output is correct
5 Correct 2 ms 560 KB Output is correct
6 Correct 3 ms 560 KB Output is correct
7 Correct 3 ms 592 KB Output is correct
8 Correct 3 ms 592 KB Output is correct
9 Correct 2 ms 592 KB Output is correct
10 Correct 2 ms 592 KB Output is correct
11 Correct 3 ms 592 KB Output is correct
12 Correct 2 ms 592 KB Output is correct
13 Correct 3 ms 592 KB Output is correct
14 Correct 3 ms 592 KB Output is correct
15 Correct 3 ms 624 KB Output is correct
16 Correct 2 ms 624 KB Output is correct
17 Correct 3 ms 624 KB Output is correct
18 Correct 2 ms 624 KB Output is correct
19 Correct 3 ms 632 KB Output is correct
20 Correct 5 ms 632 KB Output is correct
21 Correct 4 ms 632 KB Output is correct
22 Correct 3 ms 632 KB Output is correct
23 Correct 3 ms 632 KB Output is correct
24 Correct 4 ms 632 KB Output is correct
25 Correct 3 ms 632 KB Output is correct
26 Correct 3 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 196 ms 4580 KB Output is correct
2 Correct 210 ms 4580 KB Output is correct
3 Correct 179 ms 4580 KB Output is correct
4 Correct 185 ms 4580 KB Output is correct
5 Correct 126 ms 4580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 4580 KB Output is correct
2 Correct 50 ms 4580 KB Output is correct
3 Correct 38 ms 4580 KB Output is correct
4 Correct 28 ms 4580 KB Output is correct
5 Correct 3 ms 4580 KB Output is correct
6 Correct 31 ms 4580 KB Output is correct
7 Correct 38 ms 4580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 4580 KB Output is correct
2 Correct 74 ms 4580 KB Output is correct
3 Correct 154 ms 4580 KB Output is correct
4 Correct 110 ms 4580 KB Output is correct
5 Correct 57 ms 4580 KB Output is correct
6 Correct 104 ms 4580 KB Output is correct
7 Correct 115 ms 4580 KB Output is correct
8 Correct 78 ms 4580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 248 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 560 KB Output is correct
4 Correct 3 ms 560 KB Output is correct
5 Correct 2 ms 560 KB Output is correct
6 Correct 3 ms 560 KB Output is correct
7 Correct 3 ms 592 KB Output is correct
8 Correct 3 ms 592 KB Output is correct
9 Correct 2 ms 592 KB Output is correct
10 Correct 2 ms 592 KB Output is correct
11 Correct 3 ms 592 KB Output is correct
12 Correct 2 ms 592 KB Output is correct
13 Correct 3 ms 592 KB Output is correct
14 Correct 3 ms 592 KB Output is correct
15 Correct 3 ms 624 KB Output is correct
16 Correct 2 ms 624 KB Output is correct
17 Correct 3 ms 624 KB Output is correct
18 Correct 2 ms 624 KB Output is correct
19 Correct 3 ms 632 KB Output is correct
20 Correct 5 ms 632 KB Output is correct
21 Correct 4 ms 632 KB Output is correct
22 Correct 3 ms 632 KB Output is correct
23 Correct 3 ms 632 KB Output is correct
24 Correct 4 ms 632 KB Output is correct
25 Correct 3 ms 632 KB Output is correct
26 Correct 3 ms 632 KB Output is correct
27 Correct 196 ms 4580 KB Output is correct
28 Correct 210 ms 4580 KB Output is correct
29 Correct 179 ms 4580 KB Output is correct
30 Correct 185 ms 4580 KB Output is correct
31 Correct 126 ms 4580 KB Output is correct
32 Correct 47 ms 4580 KB Output is correct
33 Correct 50 ms 4580 KB Output is correct
34 Correct 38 ms 4580 KB Output is correct
35 Correct 28 ms 4580 KB Output is correct
36 Correct 3 ms 4580 KB Output is correct
37 Correct 31 ms 4580 KB Output is correct
38 Correct 38 ms 4580 KB Output is correct
39 Correct 76 ms 4580 KB Output is correct
40 Correct 74 ms 4580 KB Output is correct
41 Correct 154 ms 4580 KB Output is correct
42 Correct 110 ms 4580 KB Output is correct
43 Correct 57 ms 4580 KB Output is correct
44 Correct 104 ms 4580 KB Output is correct
45 Correct 115 ms 4580 KB Output is correct
46 Correct 78 ms 4580 KB Output is correct
47 Correct 90 ms 4580 KB Output is correct
48 Correct 85 ms 4580 KB Output is correct
49 Correct 177 ms 4580 KB Output is correct
50 Correct 117 ms 4580 KB Output is correct
51 Correct 105 ms 4580 KB Output is correct
52 Correct 160 ms 4604 KB Output is correct
53 Correct 119 ms 4604 KB Output is correct
54 Correct 187 ms 4604 KB Output is correct
55 Correct 163 ms 4604 KB Output is correct