概述
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 ai, the company's profit on the i-th day. If ai 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 (ai < 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 ai, will print the minimum number of folders.
The first line contains integer n (1 ≤ n ≤ 100), n is the number of days. The second line contains a sequence of integers a1, a2, ..., an(|ai| ≤ 100), where ai means the company profit on the i-th day. It is possible that the company has no days with the negative ai.
Print an integer k — the required minimum number of folders. In the second line print a sequence of integers b1, b2, ..., bk, where bj 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.
11 1 2 3 -4 -5 -6 5 -5 -6 -7 6
3 5 3 3
5 0 -1 100 -1 0
1 5
Here goes a way to sort the reports from the first sample into three folders:
In the second sample you can put all five reports in one folder.
解题说明:题目的意思翻译过来是把一组数进行分组,确保每个分组中的负数不超过2个。做法是在输入的时候统计负数的个数,每当负数达到了3个,立刻增加一个新的分组,负数个数变为1.依次下去,直到分组完成。
#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
int n,i,j=1,m,a[101]={0},t=0,q=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&m);
if(m<0)
{
t++;
}
if(t>=3)
{
t=1;
a[j++]=q;
q=1;
}
else
{
q++;
}
}
a[j]=q;
printf("%dn",j);
for(i=1;i<=j;i++)
{
printf("%d ",a[i]);
}
return 0;
}
最后
以上就是自由书本为你收集整理的A. Paper Work的全部内容,希望文章能够帮你解决A. Paper Work所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复