But I got TLE T_T
Is map too slow to solve this problem?
This is my code..
Code: Select all
#include<iostream>
#include<map>
using namespace std;
int main(){
int num, cnt;
long long input;
long long max, z;
map<long long, int> M;
while (cin >> num){
cnt = 0;
max = 0;
M.clear();
for (int i = 0 ; i < num ; i++){
cin >> input;
M[input]++;
if (input > max)
max = input;
}
map<long long, int>::iterator it1, it2, temp;
for (it1 = M.begin() ; it1 -> first * 2 <= max; it1++){
for (it2 = it1; it1 -> first + it2 -> first <= max ; it2++){
z = it1->first + it2->first;
temp = M.find(z);
if (temp -> first == z){
if (it1->first == it2->first){
cnt += it2->second * (it2->second - 1) / 2 * temp -> second;
}else{
cnt += it1->second * it2->second * temp -> second;
}
}
}
}
cout << cnt << endl;
}
}