小明种苹果(续)第十七次CCF认证
题目
很高心,在现在CCF CSP
可以下载自己当时的答卷了,也就是自己当时提交的代码,下面就贴上自己认证时写的代码
// INFO BEGIN
//
// User = 201910014101(赵**)
// Group = C/C++
// Problem = 小明种苹果(续)
// Language = DCPP
// SubmitTime = 2019-09-15 16:54:19
//
// INFO END
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e4+7;
struct node
{
bool drop;
ll init, sum;
}ap[maxn];
int n, m;
int main()
{
ll tmp;
scanf("%d", &n);
for(int i=1; i<=n; i++)
{
scanf("%d", &m);
for(int j=1; j<=m; j++)
{
scanf("%lld", &tmp);
if(tmp<=0)
{
ap[i].sum+=(-tmp);
}
else if(tmp>0 && j!=1)
{
if(ap[i].init-ap[i].sum!=tmp)
ap[i].drop=true;
ap[i].init=tmp;
ap[i].sum=0;
}
else
{
ap[i].drop=false;
ap[i].init=tmp;
ap[i].sum=0;
}
}
}
ll res=0;
int pre, next, drops=0, team=0;
for(int i=1; i<=n; i++)
{
res+=ap[i].init-ap[i].sum;
if(ap[i].drop)
{
drops++;
pre=i-1;
next=i+1;
if(pre<=0) pre=n;
if(next>=n+1) next=1;
if(ap[pre].drop && ap[next].drop)
{
team++;
}
}
}
printf("%lld %d %d
", res, drops, team);
return 0;
}
update:2019.12.11 16:54
解题思路
进行模拟即可
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1007;
struct node
{
ll init, dsum;
bool drop; //用来记录这棵树是否发生苹果掉落
}a[maxn];
int n, m;
int main()
{
ll tmp;
scanf("%lld", &n);
for(int i=1; i<=n; i++)
{
scanf("%d", &m);
for(int j=1; j<=m; j++)
{
scanf("%lld", &tmp);
if(j==1) //第一次要进行初始化
{
a[i].init=tmp;
a[i].drop=false;
continue;
}
if(tmp>0)
{
if(a[i].init!=tmp) //判断是否发生苹果掉落
{
a[i].dsum+=a[i].init-tmp;
a[i].init=tmp;
a[i].drop=true;
}
}
else
{
a[i].init += tmp; //注意这个tmp是非正数
}
}
}
ll sum=0, nums=0, ans=0;
for(int i=1; i<=n; i++)
{
sum+=a[i].init;
if(a[i].drop)
nums++;
}
printf("%lld %lld ", sum, nums);
for(int i=1; i<=n; i++)
{
if(a[i].drop)
{
if(i==1 && a[n].drop && a[i+1].drop)
ans++;
else if(i==n && a[i-1].drop && a[1].drop)
ans++;
else if(a[i-1].drop && a[i+1].drop)
ans++;
}
}
printf("%lld
", ans);
return 0;
}