CSES - Datatähti 2021 alku - Results
Submission details
Task:Alitaulukot
Sender:Kertor
Submission time:2020-10-05 14:52:55 +0300
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp:12:1: error: 'lint' does not name a type; did you mean 'uint'?
 lint length(lint x)
 ^~~~
 uint
input/code.cpp:17:1: error: 'lint' does not name a type; did you mean 'uint'?
 lint CalculateSubArray(lint arr[], lint n, lint k)
 ^~~~
 uint
input/code.cpp: In function 'int main()':
input/code.cpp:76:2: error: 'lint' was not declared in this scope
  lint n;
  ^~~~
input/code.cpp:76:2: note: suggested alternative: 'uint'
  lint n;
  ^~~~
  uint
input/code.cpp:77:9: error: 'n' was not declared in this scope
  cin >> n;
         ^
input/code.cpp:77:9: note: suggested alternative: 'yn'
  cin >> n;
         ^
         yn
input/code.cpp:78:7: error: expected ';' before 'k'
  lint k;
       ^
input/code.cpp:79:9: error: 'k' was not declared in this scope
  cin >> k;
         ^
input/code.cpp:80:7: error: expected ';' before 'arr'
  lint arr[n];
       ^~~
input/code.cpp:81:11: error: expected ';' before 'i'
  for(lint i = 0; i < n; i++)
           ^
input/code.cpp:81:18:...

Code

#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <bits/stdc++.h> 
#define ll long long int
 
using namespace std;

lint length(lint x)
{
	return x * (x + 1) / 2;
}
 
lint CalculateSubArray(lint arr[], lint n, lint k)
{
	lint i = 0;
	lint len = 0;
	lint result = 0;
	lint duplicate_len = 0;
	
	lint max = arr[0];
	lint min = arr[0];
	
	lint max_index = 0;
	lint min_index = 0;

	lint checkpoint_index = 0;
	
	//An adventurer will always progress or get to the same point.
	while(i < n)
	{
		bool MaxChange = max < arr[i];
		bool MinChange = min > arr[i];
		
		if(MaxChange) {
			max = arr[i];
			max_index = i;
		}
		else if(MinChange) {
			min = arr[i];
			min_index = i;
		}
		
		if(max - min <= k) {
			len++;
			i++;		
			duplicate_len += i <= checkpoint_index;
		}
		else
		{
			result += length(len);
			len = 0;
			result -= length(duplicate_len);
			duplicate_len = 0;		
			checkpoint_index = i;
			
			
			i = (min_index * MaxChange) + (max_index * !MaxChange) + 1;
 
			max = arr[i];
			max_index = i;
			min = arr[i];
			min_index = i;
		}	
	}
	result += length(len);
	result -= length(duplicate_len);
	return result;
}
 
int main()
{
	lint n;
	cin >> n;
	lint k;
	cin >> k;
	lint arr[n];
	for(lint i = 0; i < n; i++)
	{
		cin >> arr[i];
	}
	cout << CalculateSubArray(arr, n, k);
	return 0;
}