1. 程式人生 > 其它 >csp 201909-2 小明種蘋果(續)(模擬)

csp 201909-2 小明種蘋果(續)(模擬)

目錄

題目傳送門

題目描述

模擬

分析

程式碼

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;

struct Tree
{
	LL cnt; // 最後剩餘的果子數 
	bool isDrop;
}trees[1010];

int n;
int main()
{
	scanf("%d", &n);
	for(int i = 0; i < n; i++)
	{
		int m; 
		scanf("%d", &m);
		
		LL pre = 0;
		bool flag = false;
		
		for(int j = 0; j < m; j++)
		{
			int x;
			scanf("%d", &x);
			if(j == 0)
			{
				pre = x;
			}
			else
			{
				if(x <= 0) pre += x;
				else 
				{
					if(pre > x) flag = true, pre = x;
				}
			}
		}
		Tree t;
		t.cnt = pre;
		t.isDrop = flag;
			
		trees[i] = t;
	}
	
	LL sum = 0;
	LL cnt = 0;
	LL res = 0;
	for(int i = 0; i < n; i++)
	{
//		printf("%d  %d\n",trees[i].cnt, trees[i].isDrop);
		
		sum += (LL) trees[i].cnt;
		
		if(trees[i].isDrop) cnt++;
		
		if(i == 0) 
		{
			if(trees[0].isDrop && trees[1].isDrop && trees[n-1].isDrop) res++;
		}
		else if(i == n - 1)
		{
			if(trees[i].isDrop && trees[i-1].isDrop && trees[0].isDrop) res++;
		}
		else 
		{
			if(trees[i].isDrop && trees[i-1].isDrop && trees[i+1].isDrop) res ++;
		}
	}
	cout << sum << " " << cnt << " " << res << endl;
	return 0;
}
 

時間複雜度

參考文章