题解 | 数组计数维护
数组计数维护
https://www.nowcoder.com/practice/47ee4c040f1648fc9ef7732d35402676
#include <iostream>
using namespace std;
int* makeList(int n,int k)
{
int* list = new int[n+2];
int S = 0;
int cnt = 0;
list[0] = n; //第一个存储数值个数(伪长度)
for(int i = 1;i <= n;i++)
{
int nodeValue;
cin>>nodeValue;
list[i] = nodeValue;
if(list[i]>=k){S = S+list[i];}
if(list[i]==0&&S>=1)
{S = S - 1;cnt +=1;}
}
list[n+1] = cnt;//cnt;
return list;
}
void func()
{
int m;//用例组数
cin>>m;
int** arr = new int* [m];
for(int i = 0;i < m;i++)
{
int n;
int k;
cin>>n;
cin>>k;
arr[i] = makeList(n,k);
}
for(int j = 0;j < m;j++)
{
int number = arr[j][0];
cout<<arr[j][number+1]<<endl;
}
for(int k = 0;k < m;k++)
{
delete[] arr[k];
}
delete[] arr;
}
int main() {
func();
return 0;
}