1

I am trying to solve this question but in the output the last digit is rounded. Is there a way to convert this output to a string so that the output isn't rounded?


We are given a convex polygon with N sides. You have to answer Q queries. The ith query is described by two integers vi,ti. In this query, all sides of the polygon start moving parallel to their respective perpendicular vector away from the centroid with a constant velocity of viunitssec. Output the final area covered by N sides of the polygon after time ti seconds.

#include <bits/stdc++.h>

#define ll long long int
#define lld long double

using namespace std;

lld dist(int x1, int y1, int x2, int y2)
{
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);
}

lld dist2(int x1, int y1, int x2, int y2)
{
    return (pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);
}

lld cot_sum(int x1, int y1, int x2, int y2)
{
    lld dot = x1 * x2 + y1 * y2;
    lld cosi = dot / (sqrt((lld)(x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2)));
    lld theta = acos(cosi);
    theta = (M_PI - theta) / 2;
    return cos(theta) / sin(theta);
}

lld polygonArea(vector<int> X, vector<int> Y)
{
    int n = X.size();
    lld area = 0.0;
    int j = n - 1;
    for (int i = 0; i < n; i++)
    {
        area += (X[j] + X[i]) * (Y[j] - Y[i]);
        j = i; // j previous i
    }
    return abs(area / 2.0);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long test;
    cin >> test;
    while (test--)
    {
        ll n, q;
        cin >> n >> q;
        vector<int> x(n);
        vector<int> y(n);
        vector<int> vx(n);
        vector<int> vy(n);
        vector<lld> angle(n);
        vector<int> v(q);
        vector<int> t(q);
        vector<lld> ans(q);

        for (int i = 0; i < n; i++)
        {
            cin >> x[i] >> y[i];
        }
        for (int i = 0; i < q; i++)
        {
            cin >> v[i] >> t[i];
        }

        lld area = polygonArea(x, y);
        lld perimeter = 0;
        lld cotsum = 0;
        // cout<<"Area: "<<area<<endl;
        x.push_back(x[0]);
        // x.push_back(x[1]);
        y.push_back(y[0]);
        // y.push_back(y[1]);
        for (int i = 0; i < n; i++)
        {
            vx[i] = x[i + 1] - x[i];
            vy[i] = y[i + 1] - y[i];
        }
        vx.push_back(vx[0]);
        vy.push_back(vy[0]);
        for (int i = 0; i < n; i++)
        {
            perimeter += dist(x[i], y[i], x[i + 1], y[i + 1]);
            cotsum += cot_sum(vx[i], vy[i], vx[i + 1], vy[i + 1]);
            // cotsum = cotsum + 1 / cot_sum(x[i], y[i], x[i + 1], y[i + 1], x[i + 2], y[i + 2]);
        }
        for (int i = 0; i < q; i++)
        {
            ans[i] = area + perimeter * v[i] * t[i] + cotsum * v[i] * t[i] * v[i] * t[i];
            // for (int j = 0; j < n; j++)
            // {
            //     // ans[i] += v[i]*t[i]*dist(x[j],y[j],x[j+1],y[j+1]);
            //     ans[i] += (lld)(v[i] * t[i] * v[i] * t[i]) / cot_sum(x[j], y[j], x[j + 1], y[j + 1],x[j+2],y[j+2]);
            // }
// cout<<setprecision(7)<<ans[i]<<endl; 

            std::stringstream stream;
cout<< stream << std::fixed << std::setprecision(7) <<ans[i]<<endl;
//std::string s = stream.str();

        }
    }
    return 0;
}

SAMPLE INPUT

2
4 1
1 1
2 1
2 2
1 2
1 1
3 2
1 1
2 1
1 2
1 1
2 3

SAMPLE OUTPUT

9.0000000
9.7426406
230.8086578

MY OUTPUT

9
9.742641
230.8087
BobMorane
  • 3,870
  • 3
  • 20
  • 42

0 Answers0