CSP 201909-2 小明种苹果(续)
题目链接:[http://118.190.20.162/view.page?gpid=T93]小明种苹果(续)
题目描述:
题目分析:
我们一部分一部分看,他输入N,代表着有N棵树
之后N行,每行第一个数字M代表那一行后面有多少个数字
我们要求T果实的总量,D掉落果实的树的总量,E相邻树都掉落果实的组数
先处理T:M后面的第一个数字代表初始树的数量(我们记为end),之后每出现一个非正数,就需要将end减少,如果出现正数则更新end;输入完这一行就T+=end;即可
处理D:每次输入非正数的时候需要判断,是否和end相等,如果不等则Drop[i]=true;(此数组记录当前索引的树是否为掉落果实的树),事后循环判断Drop数组中为true的数量即为D的数量
处理E:循环判断Drop[i],如果为true,则判断Drop[i-1]和Drop[i+1]均为true则E++;
若i =1 则判断Drop[2]和Drop[n];
若i=n 则判断Drop[1]和Drop[n-1];
满分代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
bool Drop[N]={
false};
int main()
{
int n,m;
int T=0,D=0,E=0;
cin>>n;
for(int i=1;i<=n;i++)
{
int handle=0;
int end=-1;
cin>>m>>end;
for(int k=1;k<m;k++)
{
cin>>handle;
if(handle<=0)
{
end+=handle;
}
else{
if(end!=handle)
{
Drop[i]=true;
end=handle;
}
}
}
T+=end;
}
for(int i=1;i<=n;i++)
{
if(Drop[i])
{
D++;
if(i==1)
{
if(Drop[2]&&Drop[n])E++;
}
else if(i==n){
if(Drop[1]&&Drop[n-1])E++;
}
else{
if(Drop[i+1]&&Drop[i-1])E++;
}
}
}
cout<<T<<" "<<D<<" "<<E<<endl;
}
/*
4
4 74 -7 -12 -5
5 73 -8 -6 59 -4
5 76 -5 -10 60 -2
5 80 -6 -15 59 0
*/
/*
5
4 10 0 9 0
4 10 -2 7 0
2 10 0
4 10 -3 5 0
4 10 -1 8 0
*/
几乎没有用到很多内存