
Problem: 1712. 將數組分成三個子數組的方案數前綴和后綴和雙指針第一個指針是i第二個指針是23部分累加和的一半的位置也就是rem prefix.back() - prefix[i], half (rem / 2) (((rem%2) 0)? 0:1);此時第二個指針ind0指向可能的最右側累加兩個指針的距離就行, 第一個指針ind1指向可能的最左側ind0 lower_bound(after.begin(), after.end(), half) - after.begin();特殊情況是全0Codeclass Solution { public: const int mod 1e9 7; int waysToSplit(vectorint nums) { vectorint prefix{0}, after{0}; int n nums.size(), zero 0; for(int i 0; i n; i) { prefix.push_back(prefix.back() nums[i]); } for(int i n-1; i 0; i--) { after.push_back(after.back() nums[i]); } after.erase(after.begin()); unsigned long long sum 0; int ind0, ind1, now, rem, half; for(int i 1; i n; i) { now prefix[i]; rem prefix.back() - now; if(rem now * 2) continue; half (rem / 2) (((rem%2) 0)? 0:1); ind0 lower_bound(after.begin(), after.end(), half) - after.begin(); ind0 n - ind0; ind1 lower_bound(prefix.begin() i 1, prefix.end(), now * 2) - prefix.begin(); if(ind0 n) { while(prefix[ind0] - now prefix.back() - prefix[ind0]) ind0--; while(ind1 n prefix[ind1] - now now) ind1; if(ind0 ind1) { sum ind0 - ind1 1; if(prefix[ind0] 0 prefix.back() - prefix[ind0]0) sum--; sum sum % mod; } } } return sum % mod; } };