#include<bits/stdc++.h>
using namespace std;
int N, X;
vector<int> temperatures, auxiliar, LIS, lengthOfLisByValue, segTree, previousValInPosition;
unordered_map<int, int> positionOrder;
void print(vector<int>&V){
for(int n: V) cout << n << " ";
cout << endl;
}
int binarySearchCrescent(int t){
int lo = 0, hi = N-1;
int ans = 0;
while(lo <= hi){
int mid = lo + (hi - lo)/2;
if(LIS[mid] < t){
ans = mid+1;
lo = mid+1;
}else hi = mid-1;
}
return min(ans, N-1);
}
void buildTree(int node, int left, int right, vector<int>& arr){
if(left > right) return;
if(left == right){
segTree[node] = arr[left];
return;
}
int mid = left + (right - left)/2;
buildTree(2*node, left, mid, arr);
buildTree(2*node+1, mid+1, right, arr);
segTree[node] = max(segTree[2*node], segTree[2*node+1]);
}
void modifyPos(int node, int left, int right, int pos, int newVal){
if(left > pos or right < pos) return;
if(left == right){
segTree[node] = newVal;
return;
}
int mid = left + (right - left)/2;
modifyPos(2*node, left, mid, pos, newVal);
modifyPos(2*node+1, mid+1, right, pos, newVal);
segTree[node] = max(segTree[2*node], segTree[2*node+1]);
}
int query(int node, int lBound, int rBound, int left, int right){
if(right < left) return 0;
if(lBound > right or rBound < left) return 0;
if(lBound>=left && rBound<=right) return segTree[node];
int mid = lBound + (rBound - lBound)/2;
return max(query(2*node, lBound, mid, left, right), query(2*node+1, mid+1, rBound, left, right));
}
void rellenarPrimerLIS(){
//Perfecto
lengthOfLisByValue = vector<int>(N, 0);
previousValInPosition = vector<int>(N);
LIS = vector<int>(N, 1e9+7);
//Crescent LIS
for(int i = 0; i < N; ++i){
int position = binarySearchCrescent(temperatures[i]);
//You find where to put it in the crescent LIS
LIS[position] = min(LIS[position], temperatures[i]);
int length = position+1;
//cout << "Temperatura: " << temperatures[i] << "\t tamaño de LIS: " << length << endl;
//cout << position << endl;
//you save what whas there before
previousValInPosition[i] = lengthOfLisByValue[positionOrder[temperatures[i]]];
//You save that for that VALUE the maximum LIS ending in that is what you've found
lengthOfLisByValue[positionOrder[temperatures[i]]] = length;
}
}
int binarySearchLimit(long long t){
int lo = 0, ans = -1, hi = N-1;
while(lo <= hi){
int mid = lo + (hi - lo)/2;
if(auxiliar[mid] < t){
lo = mid+1;
ans = mid;
}else hi = mid-1;
}
return ans;
}
int rellenarSegundoLIS(){
LIS = vector<int>(N, 1e9);
segTree = vector<int>(4*N);
buildTree(1, 0, N-1, lengthOfLisByValue);
int ans = 1;
for(int i = N-1; i >= 0; --i){
//LIS Decreciente
int positionDecrescent = binarySearchCrescent(-temperatures[i]);
LIS[positionDecrescent] = -temperatures[i];
int length = positionDecrescent+1;
//cout << "Temperatura: " << temperatures[i] << "\t Longitud segundo LIS: " << length << endl;
//Combinar con las soluciones previas
//deshacer lo puesto
lengthOfLisByValue[positionOrder[temperatures[i]]] = previousValInPosition[i];
//cout << previousValInPosition[i] << endl;
//de manera q no interfiera con la query
modifyPos(1, 0, N-1, positionOrder[temperatures[i]], previousValInPosition[i]);
long long limitVal = (long long)temperatures[i] + (long long)X;
int LimitPos = binarySearchLimit(limitVal);
//cout << "Query en el rango [" << 0 <<", " << LimitPos << "]" << endl;
int otherLIS = query(1, 0, N-1, 0, LimitPos);
//print(lengthOfLisByValue);
//cout << "Position until which get the LIS: " << LimitPos << "\t Other LIS value: " << otherLIS << "\t the total value of LIS: " << length + otherLIS << endl;
ans = max(ans, length+otherLIS);
//de manera que vuelva a lo anterior
}
return ans;
}
signed main(){
cin >> N >> X;
temperatures = vector<int>(N);
auxiliar = vector<int>(N);
for(int i = 0; i < N; ++i){
cin >> temperatures[i];
auxiliar[i] = temperatures[i];
}
//cout << "Data entry" << endl;
//setting the positions for values - a.k.a. coordinate compression
positionOrder.reserve(N);
sort(auxiliar.begin(), auxiliar.end());
for(int i = 0; i < N; ++i){
if(positionOrder.find(auxiliar[i]) == positionOrder.end()){
positionOrder[auxiliar[i]] = i;
//cout << "Temperatura: " << auxiliar[i] <<"\t posicion relativa: " << positionOrder[auxiliar[i]] << endl;
}
}
rellenarPrimerLIS();
//cout << "Primer LIS ok" << endl;
cout << rellenarSegundoLIS() << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
356 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
348 ms |
15200 KB |
Output is correct |
2 |
Correct |
362 ms |
15204 KB |
Output is correct |
3 |
Correct |
382 ms |
15200 KB |
Output is correct |
4 |
Correct |
361 ms |
15196 KB |
Output is correct |
5 |
Correct |
194 ms |
11952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
4036 KB |
Output is correct |
2 |
Correct |
62 ms |
4036 KB |
Output is correct |
3 |
Correct |
67 ms |
4040 KB |
Output is correct |
4 |
Correct |
37 ms |
3216 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
46 ms |
3260 KB |
Output is correct |
7 |
Correct |
62 ms |
4048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
126 ms |
7720 KB |
Output is correct |
2 |
Correct |
112 ms |
7732 KB |
Output is correct |
3 |
Correct |
319 ms |
15196 KB |
Output is correct |
4 |
Correct |
156 ms |
11968 KB |
Output is correct |
5 |
Correct |
67 ms |
7748 KB |
Output is correct |
6 |
Correct |
126 ms |
14480 KB |
Output is correct |
7 |
Correct |
145 ms |
14476 KB |
Output is correct |
8 |
Correct |
102 ms |
7724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
356 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
348 ms |
15200 KB |
Output is correct |
28 |
Correct |
362 ms |
15204 KB |
Output is correct |
29 |
Correct |
382 ms |
15200 KB |
Output is correct |
30 |
Correct |
361 ms |
15196 KB |
Output is correct |
31 |
Correct |
194 ms |
11952 KB |
Output is correct |
32 |
Correct |
70 ms |
4036 KB |
Output is correct |
33 |
Correct |
62 ms |
4036 KB |
Output is correct |
34 |
Correct |
67 ms |
4040 KB |
Output is correct |
35 |
Correct |
37 ms |
3216 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
46 ms |
3260 KB |
Output is correct |
38 |
Correct |
62 ms |
4048 KB |
Output is correct |
39 |
Correct |
126 ms |
7720 KB |
Output is correct |
40 |
Correct |
112 ms |
7732 KB |
Output is correct |
41 |
Correct |
319 ms |
15196 KB |
Output is correct |
42 |
Correct |
156 ms |
11968 KB |
Output is correct |
43 |
Correct |
67 ms |
7748 KB |
Output is correct |
44 |
Correct |
126 ms |
14480 KB |
Output is correct |
45 |
Correct |
145 ms |
14476 KB |
Output is correct |
46 |
Correct |
102 ms |
7724 KB |
Output is correct |
47 |
Correct |
152 ms |
8836 KB |
Output is correct |
48 |
Correct |
176 ms |
8756 KB |
Output is correct |
49 |
Correct |
343 ms |
17072 KB |
Output is correct |
50 |
Correct |
182 ms |
13256 KB |
Output is correct |
51 |
Correct |
135 ms |
10800 KB |
Output is correct |
52 |
Correct |
171 ms |
16504 KB |
Output is correct |
53 |
Correct |
142 ms |
16428 KB |
Output is correct |
54 |
Correct |
180 ms |
17156 KB |
Output is correct |
55 |
Correct |
287 ms |
17160 KB |
Output is correct |