Page 1 of 2
11386 - Triples
Posted: Sat Jan 12, 2008 5:19 pm
by DP
Something hidden in the statement?
Posted: Sat Jan 12, 2008 5:45 pm
by mpi
My algorithm is very simple and yet i'm getting WA all the time. Here it goes:
Code: Select all
1 - Sort the input in non-decreasing order into num
2 - nt = 0
3 - Iterate from i=3..n
j = 0
k = i - 1
While (j < k)
sum = num[j] + num[k]
if (sum < num[i])
++j // discard the element from the left end
else if (sum > num[i])
--k // discard the element from the right end
else // new triple
if (num[j] == num[k]) // e.g.: 1 1 1 1 1 2
nt += c(k-j+1, 2) // binomial coefficient (e.g.: c(5, 2) = 10)
j = k // stop here
else
j2 = first element != num[j] && > j
k2 = first element != num[k] && < k
assert(j2 <= k2)
nt += (j2-j) * (k-k2)
j = j2
k = k2
4 - Print nt as result
Any hints?

Posted: Sat Jan 12, 2008 6:06 pm
by Observer
So does the obvious O(n^2) solution work? And how big can the input integers be? Just less than 2^31?
[EDIT] Accepted. Let me answer my questions: O(n^2) works. Have to use 64-bit ints in the code.
Posted: Sat Jan 12, 2008 7:17 pm
by mpi
Observer,
Are you saying that my approach is right? When I have to use 64-bit integers: output (of course) AND input?? unsigned, signed?
Posted: Sat Jan 12, 2008 8:02 pm
by Observer
Sorry. I was only talking about my own algorithm.

I use
for loop only for smallest and largest number in the tuple. It is also good to "group" the repeated numbers.
Posted: Sat Jan 12, 2008 8:25 pm
by mpi
OK. Then, what's the output for the following input:
My output
Posted: Sat Jan 12, 2008 8:28 pm
by rio
Your output is correct.
-----
Rio
Posted: Sat Jan 12, 2008 9:02 pm
by sonyckson
I did an O((n^2)log(n)) solution, and i didnt have problems with the Time Limit. gl! Eric.
Posted: Sat Jan 12, 2008 10:31 pm
by mpi
Somebody got any tricky input?? This problem is definitely driving me crazy, it's not so hard even with a naive O(n^2) algorithm like mine!
[Edited] Finally, I got ACC. Besides grouping the numbers after sorting, I changed 'int' to 'unsigned int' and all worked OK!

Thanks.
Posted: Sun Jan 13, 2008 3:41 am
by joshi13
Thanks Rio.
Posted: Sun Jan 13, 2008 1:45 pm
by Ron
why im getting TLE ..............
Code: Select all
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int i,j,k;
unsigned int arr[5000],n,m,count;
while(cin>>n){
for(i=0;i<n;i++) cin>>arr[i];
sort(arr,arr+n);
count=0;
for(i=0;i<n-2;i++){
for(j=i+1;j<n-1;j++){
m=arr[i]+arr[j];
for(k=j+1;k<n;k++){
if(arr[k]>=m){
if(arr[k]>m) break;
count++;
}
}
}
}
cout<<count<<endl;
}
return 0;
}
Posted: Sun Jan 13, 2008 1:57 pm
by rio
>>joshi13
Use long long int. The grouping idea is:
Anyway, your code and algorithm is beautiful
>>Ron
O(n^3) is too slow. Try O(n^2logn) or O(n^2).
-----
Rio
Posted: Sun Jan 13, 2008 6:22 pm
by pawelz
Hi!
Firstly, I did simple O(n^2*logn) program, but it didn't pass. Then I used my own hash table and after some troubles with RE I got AC! (3.470s). Can anyone tell me how to solve it with O(n^2) but without hashing?
Posted: Sun Jan 13, 2008 6:35 pm
by Observer
If you have grouped the numbers, then the O(n^2) is very obvious.
Hint: suppose we are to find tuples (X, y, z), where X is fixed. Think of how to do so in O(n).
There are many other ways to get O(n^2) solutions for this task.
O(n^2) algorithm
Posted: Sun Jan 13, 2008 10:52 pm
by mpi
The O(n^2) algorithm doesn't need any hashing mecanishm. All you have to do is, at each step i, iterate over the elements in the range j=1,..,k=i-1. Depending on the sum of the elements of both ends you will have to update j and k properly (incrementing j and/or decreasing k). The loop ends with j >= k (with no grouping) and with j > k (with grouping).