B. Divan and a New Project | Codeforces Solution | Codeforces Round 757 (Div. 2)


The company "Divan's Sofas" is planning to build 𝑛+1 different buildings on a coordinate line so that: the coordinate of each building is an integer number; no two buildings stand at the same point. Let 𝑥𝑖 be the coordinate of the 𝑖 -th building. To get from the building 𝑖 to the building 𝑗 , Divan spends |𝑥𝑖−𝑥𝑗| minutes, where |𝑦| is the absolute value of 𝑦 .
void solve()
{

    int n;
    cin >> n;

    vpi v;

    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        v.pb({x, i});
    }

    sort(v.rbegin(), v.rend());
    int l = -1, r = 1;
    vpi ans;
    ans.pb({-1, 0});
    int res = 0;

    for (int i = 0; i < n; i++)
    {
        if (abs(l) == abs(r))
        {
            ans.pb({v[i].second, l});
            res += abs(l--) * 2 * v[i].first;
        }
        else
        {
            ans.pb({v[i].second, r});
            res += abs(r++) * 2 * v[i].first;
        }
    }

    cout << res << endl;
    sort(ans.begin(), ans.end());
    for (auto it : ans)
        cout << it.second << " ";
    cout << endl;
}
TagsB. Divan and a New ProjectCodeforces Round 757 (Div. 2)Codeforces Solution
Raunit Verma-picture
Raunit Verma

Technical Writer at CodingKaro

Share this on
CodingKaro Poster
CodingKaro
4.7

3K+ Downloads

One of its unique features is an automated coding contest reminder, which sets alarms for upcoming coding contests on popular platforms like CodeChef, CodeForces, and LeetCode, without the need for user input. This feature ensures that users never miss a coding contest and can stay updated with the latest coding challenges.

Download CodingKaro
Other Blogs in Codeforces