ParthJha17's blog

By ParthJha17, history, 8 months ago, In English

Hey Guys,

Here's the line that didn't work: cout<<(double)((*time.begin() + *((time.end()-1)))/2.0)<<endl;

I expected this to give me the required precision of 10^-6. It didn't work for some reason. I tried many things after that and then this worked.

cout << fixed << setprecision(1) << (long double)(time.front() + time.back()) / 2.0 << endl;

Well, I understand that every solution will have either one decimal or none as the contents of time vector are integers only, but still, I'd appreciate it if someone could help me understand why the first code didn't work.

Question Link

Solution that didn't work (precision errors)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define tc     \
    ll tc;     \
    cin >> tc; \
    while (tc--)
#define pb push_back
#define mp make_pair
const ll MOD = 1e9 + 7;

void fastio()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

void solve()
{
    ll n,temp;
    cin>>n;
    vector<ll>points,time;
    for(ll i=0;i<n;++i)
    {
        cin>>temp;
        points.push_back(temp);
    }
    for(ll i=0;i<n;++i)
    {
        cin>>temp;
        time.push_back(points[i] + temp);
        time.push_back(points[i] - temp);
    }
    sort(time.begin(), time.end());
    if(n==1)
    {
        cout<<points[0]<<endl;
    }
    else
    cout<<(double)((*time.begin() + *((time.end()-1)))/2.0)<<endl;
    
}

int main()
{
    fastio();
    ll t;
    cin >> t;
    for (ll i = 1; i <= t; i++)
    {
        solve();
    }

    return 0;
}

Solution that did work

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define endl '\n'
#define tc     \
    ll tc;     \
    cin >> tc; \
    while (tc--)
#define pb push_back
#define mp make_pair
const ll MOD = 1e9 + 7;

void fastio()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

void solve()
{
    ll n, temp;
    cin >> n;
    vector<ll> points, time;
    for (ll i = 0; i < n; ++i)
    {
        cin >> temp;
        points.push_back(temp);
    }
    for (ll i = 0; i < n; ++i)
    {
        cin >> temp;
        time.push_back(points[i] + temp);
        time.push_back(points[i] - temp);
    }
    sort(time.begin(), time.end());
    if (n == 1)
    {
        cout << points[0] << endl;
    }
    else
    {
        cout << fixed << setprecision(1) << (long double)(time.front() + time.back()) / 2.0 << endl;
    }
}

int main()
{
    fastio();
    ll t;
    cin >> t;
    for (ll i = 1; i <= t; i++)
    {
        solve();
    }

    return 0;
}

Thanks a lot for your help.

Full text and comments »

  • Vote: I like it
  • -4
  • Vote: I do not like it

By ParthJha17, history, 9 months ago, In English

In my coding journey, I currently jot down code snippets and theories in a traditional notebook with added remarks for context recall.

However, I'm exploring more efficient methods, like using a pad to streamline the process and enhance organization.

I'm curious to learn how fellow coders tackle note-taking to ensure comprehensive understanding.Please share your strategies and insights so I can adopt a more effective approach to learning. Thanks!

Full text and comments »

  • Vote: I like it
  • +5
  • Vote: I do not like it

By ParthJha17, history, 17 months ago, In English

Hello, Codeforces community,

I recently encountered a performance difference while solving a problem using an unordered_map and a vector, and I would like to seek some insights on this matter.

In my solution, I initially implemented the logic using an unordered_map to keep track of element occurrences. However, to my surprise, this implementation resulted in a Time Limit Exceeded (TLE) verdict. Curious about the issue, I refactored the code to use a vector instead, and the TLE issue was resolved.

Solution with Unorder_Map, Solution with Vector, Problem

I expected both implementations to perform similarly, as the time complexity of accessing elements in an unordered_map and a vector is generally O(1). However, it seems that the implementation with unordered_set resulted in a TLE, while the vector implementation passed the tests successfully.

I would greatly appreciate it if someone could explain why this performance difference occurred. Are there any particular reasons or considerations that make unordered_map less performant in this scenario? Any insights or suggestions would be valuable to me.

Thank you for your time and expertise.

Full text and comments »

  • Vote: I like it
  • -7
  • Vote: I do not like it