CSES - Datatähti 2019 alku - Results
Submission details
Task:Taulukko
Sender:N00B.exe
Submission time:2018-10-12 19:00:16 +0300
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:30:35: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat=]
   printf("%d", (n*((n + 1.0) / 2)));
                ~~~~~~~~~~~~~~~~~~~^
input/code.cpp:59:13: error: 'INT_MAX' was not declared in this scope
   int min = INT_MAX;
             ^~~~~~~
input/code.cpp:59:13: note: suggested alternative: 'INT8_MAX'
   int min = INT_MAX;
             ^~~~~~~
             INT8_MAX
input/code.cpp:12:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~~

Code

#include <iostream>
#include <map>

using namespace std;

int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, k, ans = 0;
	scanf("%d %d", &n, &k);

	map<int, int> m;


	int* numbers = (int*)malloc(4 * n);

	for(int i = 0; i < n; i++)
	{
		cin >> *(numbers + i);
		//scanf("%d", (numbers + i));
	}

	if (k == 0)
	{ printf("0"); return 0; }

	if(k >= n)
	{
		printf("%d", (n*((n + 1.0) / 2)));
		return 0;
	}

	int dAmount = 0;

	for(int i = 0; i < n; i++)
	{
		int index = *(numbers + i);

		if(m[index] == 0)
		{ m[index] = 1; dAmount++; }

		else
		{ m[index] += 1; }
	}

	if (dAmount <= k)
	{ ans = n * ((n + 1.0) / 2); }

	else
	{
		int start = 0;
		int end = n - 1;
		bool goRight = false;
		int growIndex, shrinkIndex;
		int a, b;
		int currentDNumbers = dAmount;
		int d = currentDNumbers - k;
		int min = INT_MAX;

		if(d <= end - start - d)
		{
			for(int k = 0; k < d; k++)
			{
				m[*(numbers + start)] -= 1;
				
				if(m[*(numbers + start)] == 0)
				{ currentDNumbers--; }

				start++;
			}
		}

		else
		{
			start += d;
			m.clear();
			currentDNumbers = 0;

			for(int k = 0; k < (end-start + 1); k++)
			{
				if(m[*(numbers + start + k)] == 0)
				{ m[*(numbers + start + k)] = 1; currentDNumbers++; }

				else {
					m[*(numbers + start + k)] += 1;
				}
			}
		}

		while(end - start > (k - 1))
		{
			growIndex = (goRight) ? end + 1 : start - 1;

			if (growIndex > n - 1)
			{
				if (end - start == k)
				{ break; }

				d = min - k;

				if (d <= 1)
				{
					d = 1;
					m[*(numbers + start)] -= 1;

					if (m[*(numbers + start)] == 0)
					{ currentDNumbers--; }

					if (currentDNumbers <= k)
					{ ans++; }

					start++;
					goRight = !goRight;
				}

				else if (d <= end - start - d)
				{
					for (int k = 0; k < d; k++)
					{
						m[*(numbers + start)] -= 1;

						if (m[*(numbers + start)] == 0)
						{ currentDNumbers--; }

						start++;
					}
					goRight = !goRight;
				}

				else
				{
					start += d;
					m.clear();
					currentDNumbers = 0;

					for (int k = 0; k < (end - start + 1); k++)
					{
						if (m[*(numbers + start + k)] == 0)
						{ m[*(numbers + start + k)] = 1; currentDNumbers++; }

						else
						{ m[*(numbers + start + k)] += 1; }
					}

					goRight = !goRight;
				}

				if (currentDNumbers < min)
				{ min = currentDNumbers; }
			}

			else if(growIndex < 0)
			{
				if (end - start == k)
				{ break; }

				d = min - k;

				if (d <= 1)
				{
					d = 1;
					m[*(numbers + end)] -= 1;
					
					if (m[*(numbers + end)] == 0)
					{ currentDNumbers--; }
					
					if (currentDNumbers <= k)
					{ ans++; }

					end--;
					goRight = !goRight;
					}

					else if (d <= end - start - d)
					{
						for (int k = 0; k < d; k++)
						{
							m[*(numbers + end)] -= 1;

							if (m[*(numbers + end)] == 0)
							{ currentDNumbers--; }

							end--;
						}
						goRight = !goRight;
					}

					else
					{
						end -= d;
						m.clear();
						currentDNumbers = 0;

						for (int k = end - start; k > -1; k++)
						{
							if (m[*(numbers + end + k)] == 0)
							{ m[*(numbers + end + k)] = 1; currentDNumbers++; }

							else
							{ m[*(numbers + end + k)] += 1; }
						}

						goRight = !goRight;
					}

				}

			else
			{
				shrinkIndex = (goRight) ? start : end;

				a = m[*(numbers + shrinkIndex)];
				b = m[*(numbers + growIndex)];

				m[*(numbers + shrinkIndex)] -= 1;
				m[*(numbers + growIndex)] += 1;

				if (m[*(numbers + shrinkIndex)] == 0 && m[*(numbers + shrinkIndex)] != a)
				{ currentDNumbers--; }

				if (m[*(numbers + growIndex)] == 1 && m[*(numbers + growIndex)] != b)
				{ currentDNumbers++; }

				if (currentDNumbers <= k)
				{ ans++; }

				start += (goRight) ? 1 : -1;
				end += (goRight) ? 1 : -1;

				if(currentDNumbers < min)
				{ min = currentDNumbers; }
			}

		}
		ans += ((2 * n + 1)*k - k * k) / 2;
	}

	printf("%d", ans);
	m.clear();
	free(numbers);

	return 0;
}