20200519————A(题解)

A - Paper Work
Polycarpus has been working in the analytic department of the “F.R.A.U.D.” company for as much as n days. Right now his task is to make a series of reports about the company’s performance for the last n days. We know that the main information in a day report is value a i, the company’s profit on the i-th day. If a i is negative, then the company suffered losses on the i-th day.

Polycarpus should sort the daily reports into folders. Each folder should include data on the company’s performance for several consecutive days. Of course, the information on each of the n days should be exactly in one folder. Thus, Polycarpus puts information on the first few days in the first folder. The information on the several following days goes to the second folder, and so on.

It is known that the boss reads one daily report folder per day. If one folder has three or more reports for the days in which the company suffered losses (a i < 0), he loses his temper and his wrath is terrible.

Therefore, Polycarpus wants to prepare the folders so that none of them contains information on three or more days with the loss, and the number of folders is minimal.

Write a program that, given sequence a i, will print the minimum number of folders.

Input
The first line contains integer n (1 ≤ n ≤ 100), n is the number of days. The second line contains a sequence of integers a 1, a 2, …, a n (|a i| ≤ 100), where a i means the company profit on the i-th day. It is possible that the company has no days with the negative a i.

Output
Print an integer k — the required minimum number of folders. In the second line print a sequence of integers b 1, b 2, …, b k, where b j is the number of day reports in the j-th folder.

If there are multiple ways to sort the reports into k days, print any of them.

Examples
Input
11
1 2 3 -4 -5 -6 5 -5 -6 -7 6
Output
3
5 3 3
Input
5
0 -1 100 -1 0
Output
1
5
Note
Here goes a way to sort the reports from the first sample into three folders:

1 2 3 -4 -5 | -6 5 -5 | -6 -7 6
In the second sample you can put all five reports in one folder.

题目意思大概为:
在一个长度为n的数组中,寻找其中出现负数的次数小于3的段数。
思路为:
从第一个数字开始判断正负,直至出现第三次负数,并记下该点,存入新定义的数组b[maxn]中,最终输出的时候用b中的后一个数减去前一个数即为此段的长度。
代码如下在这里插入代码片

#include<iostream>
#define maxn 105
using namespace std;
int main()
{
	int n,j=1,count=0,num=1;
	cin>>n;
	int a[maxn],b[maxn];
	b[0]=0;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	for(int i=0;i<n;i++){
		if(a[i]<0){
			count++;
		}
		if(count>2){
			b[j]=i;
			j++;
			num++;
			count=0;
			i--;
		}
	}
	cout<<num<<endl;
	for(int i=1;i<num;i++){
		cout<<b[i]-b[i-1]<<" ";
	}
	cout<<n-b[num-1]<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值