#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int pre[2005][2005];
int fre[2005][2005];
int arr[2005];
int bsearch(int i, int j, int k)
{
int len = j - i + 1;
int m = ceil((double)k / len);
int mid, pos;
m = ceil((double)k / m);
int low = 1;
int high = 2000;
while (low < high)
{
mid = (low + high) / 2;
pos = (pre[mid][j] - pre[mid][i - 1]);
if (pos < m)
{
low = mid + 1;
}
else
{
high = mid;
}
}
if (low == high - 1)
{
pos = (pre[low][j] - pre[low][i - 1]);
if (pos >= m)
{
high = low;
}
}
return high;
}
void resetArray()
{
for (int i = 0; i < 2005; i++)
{
arr[i] = 0;
for (int j = 0; j < 2005; j++)
{
pre[i][j] = 0;
fre[i][j] = 0;
}
}
}
int main()
{
int t, n, k, freq, count;
cin >> t;
while (t--)
{
cin >> n >> k;
count = 0;
resetArray();
for (int i = 1; i <= n; i++)
{
cin >> arr[i];
}
for (int i = 1; i <= 2000; i++)
{
for (int j = 1; j <= n; j++)
{
pre[i][j] = pre[i][j - 1] + (arr[j] <= i ? 1 : 0);
fre[i][j] = fre[i][j - 1] + (arr[j] == i ? 1 : 0);
}
}
for (int i = 1; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
//for subarray [i, j] find value of x
int x = bsearch(i, j, k);
//calculate frequency of x in subarray [i, j]
freq = fre[x][j] - fre[x][i - 1];
//calculate frequency of freq in subarray [i, j]
freq = fre[freq][j] - fre[freq][i - 1];
if (freq > 0)
{
count++;
}
}
}
cout << count << endl;
}
}
Comments
Post a Comment