Problem Statement :
N lines are given in 2D plane of form ax+by+c=0.'Q' queries are asked.Each query denotes a line number and we have to display the number of lines perpendicular to this line.
Input:
First line contains a single integer N - the total number of lines
N lines follow - each line contains 3 space separated integers: a, b and c, representing the line ax+by+c=0.
Next line contains a single integer Q - the number of questions
Q lines follow - each line contains an index between 1 and N (inclusive)
Constraints:
1 <= N <= 10^5
10^-9 <= a, b, c <= 10^9
1 <= Q <= 10^5
Note: All triplets (a, b, c) in the input denote valid straight lines.
Sample Input(Plaintext Link)
4
1 1 1
-1 1 1
1 2 3
3 4 5
4
1
2
3
4
Sample Output(Plaintext Link)
1
1
0
0
Explanation
Lines 1 and 2 are perpendicular to each other.
Time Limit: 1 sec(s) for each input file.
Memory Limit: 256 MB
My code is working but for some test cases the time limit is exceeding . Please tell me how to improve the performance
Code:
Code:
Code:
#include<stdio.h>
int main(){
long int n,k=0,q,i,count;
long double a,b,c;
scanf("%ld",&n);
long double line[n][2];
for(k=0;k<n;k++){
scanf("%Lf %Lf %Lf",&a,&b,&c);
line[k][1]=a;
line[k][2]=b;
}
scanf("%ld",&q);
while(q--){
scanf("%ld",&k); // line number
k=k-1;
count=0;
for(i=0;i<n;i++){
if( (line[i][1]*line[k][1]) + (line[i][2]*line[k][2]) == 0 )
count++;
}
printf("%ld\n",count);
}
}