#include <cassert> #include <iostream> #include <istream> #include <fstream> #include <algorithm> #include <cstdio> #include <complex> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> #include <string> #include <cstdlib> #include <memory> #include <ctime> #include <bitset> #include <queue> #include <stack> #include <unordered_map> #include <unordered_set> #include <bitset> #include <ranges> using namespace std; class Buc2024 { int n; vector<long> input; vector<long> left; vector<long> right; long sum = 0; int res = 0; public: void Prep() { for (int i = 0; i < n; i++) { if (i == 0) { left[i] = 0; } else { left[i] = left[i - 1] + input[i - 1]; } } for (int i = n - 1; i >= 0; i--) { if (i == (n - 1)) { right[i] = 0; } else { right[i] = right[i + 1] + input[i + 1]; } } } long threeSum(vector<long>& nums) { int n = nums.size(); bool found = false; long count = 0; for (int i = 0; i < n - 2; ++i) { for (int j = i + 1; j < n - 1; ++j) { for (int k = j + 1; k < n; ++k) { if (nums[i] + nums[j] + nums[k] == 0) { found = true; count++; //cout << "[" << nums[i] << ", " << nums[j] << ", " << nums[k] << "]" << endl; } } } } return count; } long threeSumCount(vector<long>& nums) { long count = 0; int n = nums.size(); // Sort the input array sort(nums.begin(), nums.end()); for (int i = 0; i < n - 2; ++i) { int left = i + 1; int right = n - 1; while (left < right) { long sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { count++; // Move the pointers to find more triplets left++; right--; } else if (sum < 0) { left++; } else { right--; } } } return count; } void virtual Solve() { cin >> n; input = vector<long>(n, 0); left = vector<long>(n, 0); right = vector<long>(n, 0); //READ: for (int i = 0; i < n; i++) { cin >> input[i]; sum += input[i]; } this->Prep(); vector<long> sol; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { long elem = sum - (left[i] + right[j]); sol.push_back(elem); } } int result = threeSum(sol); cout << result; } }; int main() { Buc2024* p = new Buc2024(); p->Solve(); delete p; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | #include <cassert> #include <iostream> #include <istream> #include <fstream> #include <algorithm> #include <cstdio> #include <complex> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> #include <string> #include <cstdlib> #include <memory> #include <ctime> #include <bitset> #include <queue> #include <stack> #include <unordered_map> #include <unordered_set> #include <bitset> #include <ranges> using namespace std; class Buc2024 { int n; vector<long> input; vector<long> left; vector<long> right; long sum = 0; int res = 0; public: void Prep() { for (int i = 0; i < n; i++) { if (i == 0) { left[i] = 0; } else { left[i] = left[i - 1] + input[i - 1]; } } for (int i = n - 1; i >= 0; i--) { if (i == (n - 1)) { right[i] = 0; } else { right[i] = right[i + 1] + input[i + 1]; } } } long threeSum(vector<long>& nums) { int n = nums.size(); bool found = false; long count = 0; for (int i = 0; i < n - 2; ++i) { for (int j = i + 1; j < n - 1; ++j) { for (int k = j + 1; k < n; ++k) { if (nums[i] + nums[j] + nums[k] == 0) { found = true; count++; //cout << "[" << nums[i] << ", " << nums[j] << ", " << nums[k] << "]" << endl; } } } } return count; } long threeSumCount(vector<long>& nums) { long count = 0; int n = nums.size(); // Sort the input array sort(nums.begin(), nums.end()); for (int i = 0; i < n - 2; ++i) { int left = i + 1; int right = n - 1; while (left < right) { long sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { count++; // Move the pointers to find more triplets left++; right--; } else if (sum < 0) { left++; } else { right--; } } } return count; } void virtual Solve() { cin >> n; input = vector<long>(n, 0); left = vector<long>(n, 0); right = vector<long>(n, 0); //READ: for (int i = 0; i < n; i++) { cin >> input[i]; sum += input[i]; } this->Prep(); vector<long> sol; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { long elem = sum - (left[i] + right[j]); sol.push_back(elem); } } int result = threeSum(sol); cout << result; } }; int main() { Buc2024* p = new Buc2024(); p->Solve(); delete p; return 0; } |