輸入:
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; }