输入:
4
4 74 -7 -12 -5
5 73 -8 -6 59 -4
5 76 -5 -10 60 -2
5 80 -6 -15 59 0
输出:
222 1 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
输出:
39 4 2
注意:
1.发生苹果掉落和疏果是两种不同的操作 发生苹果掉落(5 3) 疏果(5 -3)
2.一棵树可能出现多次苹果掉落的情况 比如:3 5 2 1(对于一棵树来说 有3个操作,原来有5个苹果,第一次掉落后还剩2个,第二次掉落后还剩1个)
3.当发生苹果掉落的苹果树的棵树大于等于3时才可能形成连续的三个苹果树
发生苹果掉落的苹果树的序号我是用栈存的,感觉稍微麻烦了点,有时间试试数组
#include <cstdio> #include <algorithm> #include <iostream> #include <stack> #include <cstring> #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long LL; const int mod = 1e9+7; int dir[4][2] = {0,1,0,-1,1,0,-1,0}; stack<int>q; int main() { LL n; LL T = 0,D = 0,E = 0; cin >> n; int first = 0,second = 0; for(int i = 1; i <= n; i++) { int k; cin >> k; LL res1 = 0,kk; for(int j = 1; j <= k; j++) { cin >> kk; if(j == 1) { res1 = kk; } else { if(kk > 0) { if(kk != res1) { if(q.size() == 0) { q.push(i); first = i; D++;//在内部统计D 防止一个序号的苹果树被多次统计 // cout << i << endl; } else if(q.size() == 1) { int temp = q.top(); if(i != temp) { q.push(i); second = i; D++;//如果该苹果树没有在栈中出现过D++ // cout << i << endl; } } else { int temp = q.top(); if(i != temp) { q.push(i); D++; // cout << i << endl; } } } res1 = kk; } else { res1 -= -kk; } } } T += res1; } if(q.size() >= 3){//当发生苹果掉落的苹果树棵树大于等于3时才有可能出现连续的三个苹果树 while(!q.empty()){ int temp = q.top(); q.pop(); //苹果树序号连续的三种情况 if((temp+1 == first && first+1 == second) || ((temp == n) && (first == 1) && (second == 2)) || ((temp == n-1) && (first == n) && (second = 1))) { E++; } second = first; first = temp; } } cout << T << " " << D << " " << E << endl; return 0; }