题解 | 数组计数维护

数组计数维护

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;
}

全部评论

相关推荐

点赞 评论 收藏
分享
有点心碎的杨桃很想润:我在美团,小道消息今年秋招之后美团可能就完全不会招日常实习了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务