NBC001- Editorial

PROBLEM LINK:

Practice
Noob Coding Contest

Author: unknown_9876
Tester: sourav472
Editorialist: sourav472

DIFFICULTY:

Easy

PREREQUISITES:

Basic Mathematics

EXPLANATION:

Whether triangle is possible or not, check ( larger side of triangle < sum of other two sides ) then triangle is possible otherwise not.
For find out the area of triangle, first find out semi perimeter (s=(a+b+c)/2), then sqrt of (s*(s-a)(s-b)(s-c)); Where a,b,c are three sides of triangle.

SOLUTIONS:

Setter's Solution
#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
int main() {
    ll t;
    cin>>t;
	while(t--)
	{
	ll a,b,c,ar[3];
	cin>>a>>b>>c;
	long double s,ans,p;
	ar[0] = a;
	ar[1] = b;
	ar[2] = c;
	sort(ar,ar+3);
	if(ar[0]+ar[1]<=ar[2])
	 cout<<"NO\n";
	else
	 {
	    s = (a+b+c)/2;
	    p=1.00;
	    p*= sqrt(s);
	    p*=sqrt(s-a);
	    p*=sqrt(s-b);
	    p*=sqrt(s-c);
	    ans = p;
	    cout<<"YES "<<fixed<<setprecision(5)<<(ans)<<"\n";
	 }
	}
	return 0;
}
Tester's Solution"
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define ull unsigned long long
#define rep(i,n) for(ll i=0;i<n;i++)
#define dfor(i,a,b) for(ll i=(a);i<(b);i++)
#define rfor(i,a,b) for(ll i=(a);i>=(b);i--)
#define pii pair<ll,ll>
#define vi vector<ll>
#define vpii vector<pii>
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define fast ios_base::sync_with_stdio( false );cin.tie(NULL);
const ll mod = (ll)(1e9+7);
int main() {
    fast
    ll t;
    cin>>t;
    while(t--)
    {
	   ll a,b,c,ar[3];
	   cin>>a>>b>>c;
	   long double s,ans,p;
	   ar[0] = a;
	   ar[1] = b;
	   ar[2] = c;
	   sort(ar,ar+3);
	   if(ar[0]+ar[1]<=ar[2])
	   cout<<"NO\n";
	  else
	  {
	     s = (a+b+c)/2;
	     p = s*(s-a)*(s-b)*(s-c);
	     ans = sqrt(p);
	     cout<<"YES "<<fixed<<setprecision(5)<<ans<<"\n";
	  }
	}
	return 0;

}

2 Likes

What is wrong with this solution

I solved NBC001 with the exact same method. Still got WA :frowning:

Can you please figure out the mistake in this 10 line code :slightly_smiling_face:
https://www.codechef.com/viewsolution/39663034

Thanks in advance :smiley:

I think there is some precision issue. I used setprecision(6) first and got a WA. Changed it to setprecision(5) and got AC, though I am not sure, if this is the only issue.

Code
bool yes(ll a, ll b, ll c) 
{  
    if (a + b <= c || a + c <= b || b + c <= a) 
        return false; 
    else
        return true; 
} 
  
void solve(){
       //input
       ll a,b,c;
       cin>>a>>b>>c;
       if(yes(a,b,c)){
		   cout<<"YES"<<" ";
		   ll s=(a+b+c)/2;
		   long double area=s*(s-a)*(s-b)*(s-c);
		   cout<<fixed<<setprecision(5)<<sqrt(area)<<endl;
	   }
	   else{
		   cout<<"NO"<<endl;
	   }
}

I think there is some issue with python precision

how about this

There is some precision error in your solution. I typecasted-
ld s = (a[0]+a[1]+a[2])/2.00 ;
with int and it gives AC.
Your AC Code

Man that was painful, thanks a lot btw

1 Like

I am unable to figure out one thing, I first used cin and cout for io, I got WA, later I used scanf and printf for io and I got Right Answer, why did this happen?

Bro check your solution when u use cin and cout, you print the answer upto 6 decimal point but when u use scanf and printf, u printt upto 5 decimal point(.5f) that’s why you got AC.

1 Like

Ohhh, I got it now thanks @unknown_9876

As per explanation given by @sourav472 I am getting wrong answers and it is getting accepted.

let say value of three sides be 8 5 4, so

s = (8+5+4)/2 = 17/2 = 8

then

p= 8 * (8-8) * (8-5) * (8-4) = 8 * 0 * 3 * 4=0

so final answer will be YES 0.00000 and it is getting accepted.

But it should be YES 8.18153

Note: Convert sum to double before division.
double sum= a + b + c;
s = sum /2;

Please correct me if wrong.