Tuesday 8 October 2013

CodeChef October Challenge 2013 Editorial [Helping Lira]

Problem: Helping Lira
Given N triangles, each triangle is denoted by 6 coordinates (for 3 vertices), you have to print the indexes of the triangles with the smallest and largest area. If there are multiple solutions, print the last indexes.

Solution Idea:
as you know the coordinates of vertices, you can easily calculate area with this formula.
abs(0.5*((x2-x0)*(y1-y0)-(x1-x0)*(y2-y0)));
as you are comparing area of triangles, you need not to divided by 0.5. it offer you to solve the problem with 'int' data type.
no problem if you wish to use actual area.
now, Greedy approach to find the index of minimum and maximum area.
for last indexes:
tmp>=mx
if you were asked to print  first occurrece:
tmp>mx 

Code:
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int tc,kk=1, n, x0, x1, x2, y0, y1, y2;
  7. cin>>n;
  8. {
  9. int mn=1e9, mx=0, mni=0, mxi=0;
  10. for(int i=1;i<=n;i++)
  11. {
  12. cin>>x0>>y0>>x1>>y1>>x2>>y2;
  13. int tmp=abs((x2-x0)*(y1-y0)-(x1-x0)*(y2-y0));
  14. if(tmp>=mx)
  15. {
  16. mx=tmp;
  17. mxi=i;
  18. }
  19. if(tmp<=mn)
  20. {
  21. mn=tmp;
  22. mni=i;
  23. }
  24. }
  25. cout<<mni<<" "<<mxi<<endl;
  26. }
  27. return 0;
  28. }


No comments:

Post a Comment