题目大意

给你一串数组,按要求分割,使得每一段内的负数个数不超过2个,问最少分割组数以及每一组的个数

Sample

#1 IN


11
1 2 3 -4 -5 -6 5 -5 -6 -7 6

#1 OUT


3
5 3 3

#2 IN


5
0 -1 100 -1 0

#2 OUT


1
5

解题思路

最近感觉很不在状态,做水题看半天做必WA几次。虽然过了打算再整理一下

(烦~)

暴力啊!(我也是服了自己,这道题写了这么长的代码)


#include <bits/stdc++.h>
using namespace std;

const int maxn = 105;
int b[maxn];

int main()
{
    std::ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> ve;
    for (int i = 1; i <= n; i++)
    {
        int temp;
        cin >> temp;
        if (temp < 0)
        {
            ve.push_back(i);
        }
    }
    int ans;
    if (ve.size() % 2 != 0 || ve.size() == 0)
    {
        ans = ve.size() / 2 + 1;
    }
    else
    {
        ans = ve.size() / 2;
    }
    cout << ans << endl;
    if (ans > 1)
    {
        int pre = 0, temp = 0, cont = 0, count = 0;
        for (int i = 0; i < ve.size(); i++)
        {
            cont++;
            if (cont > 1 && i == 1)
            {
                b[count++] = ve[i];
                cont = 0;
            }
            else if (cont == 1 && i == ve.size() - 1)
            {
                b[count++] = n - ve[i-1];
                cont = 0;
            }
            else if (cont > 1 && i == ve.size() - 1 && i < n - 1)
            {
                b[count++] = n - ve[i - 2];
                cont = 0;
            }
            else if (cont > 1)
            {
                b[count++] = ve[i] - ve[i - 2];
                cont = 0;
            }
        }
        for (int i = 0; i < count; i++)
        {
            if (i == 0)
                cout << b[i];
            else
            {
                cout << " " << b[i];
            }
        }
        cout << endl;
    }
    else{
        cout << n << endl;
    }
    return 0;
}
01-03 08:09