B. Divan and a New Project | Codeforces Solution | Codeforces Round 757 (Div. 2)
Raunit Verma - in Codeforces
Views: 0
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;
}
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define F first
#define S second
#define pb push_back
#define si set<int>
#define vi vector<int>
#define pii pair<int, int>
#define vpi vector<pii>
#define vpp vector<pair<int, pii>>
#define mii map<int, int>
#define mpi map<pii, int>
#define spi set<pii>
#define endl "\n"
#define sz(x) ((int)x.size())
#define all(p) p.begin(), p.end()
#define double long double
#define que_max priority_queue<int>
#define que_min priority_queue<int, vi, greater<int>>
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define print(a) \
for (auto x : a) \
cout << x << " "; \
cout << endl
#define print1(a) \
for (auto x : a) \
cout << x.F << " " << x.S << endl
#define print2(a, x, y) \
for (int i = x; i < y; i++) \
cout << a[i] << " "; \
cout << endl
#define scanit(a, n) \
for (ll indexaa = 0; indexaa < n; indexaa++) \
cin >> a[indexaa];
inline int power(int a, int b)
{
int x = 1;
while (b)
{
if (b & 1)
x *= a;
a *= a;
b >>= 1;
}
return x;
}
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args)
{
const char *comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
const int N = 200005;
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;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
clock_t z = clock();
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define vpi vector<pair<int, int>> // Define vpi as a vector of pairs of integers
#define pb push_back // Shortcut for push_back
void solve()
{
int n; // Number of buildings excluding headquarters
cin >> n; // Input n
vpi v; // Vector of pairs to store (visits, building index)
// Input the number of visits for each building and store with its original index
for (int i = 0; i < n; i++)
{
int x; // Number of visits for the i-th building
cin >> x; // Input visits
v.pb({x, i}); // Store pair (visits, index) in vector v
}
// Sort buildings by number of visits in descending order to prioritize high-visit buildings
sort(v.rbegin(), v.rend()); // rbegin() and rend() ensure descending order
int l = -1, r = 1; // Initialize left (negative) and right (positive) coordinates relative to headquarters
vpi ans; // Vector to store answers: pairs of (original index, assigned coordinate)
ans.pb({-1, 0}); // Headquarters (building 0) is at coordinate 0 (index -1 is a placeholder)
int res = 0; // Total walking time (result)
// Assign coordinates to buildings, alternating between left and right to minimize total distance
for (int i = 0; i < n; i++)
{
// If absolute values of left and right pointers are equal, place on the left side
if (abs(l) == abs(r))
{
ans.pb({v[i].second, l}); // Assign current building to left coordinate
res += abs(l--) * 2 * v[i].first; // Add walking time: 2 * distance * visits, decrement l
}
// Otherwise, place on the right side
else
{
ans.pb({v[i].second, r}); // Assign current building to right coordinate
res += abs(r++) * 2 * v[i].first; // Add walking time: 2 * distance * visits, increment r
}
}
cout << res << endl; // Output the minimum total walking time
// Sort ans by original building index to match output order (0, 1, 2, ..., n)
sort(ans.begin(), ans.end());
// Output the assigned coordinates for each building (headquarters at 0, then buildings 1 to n)
for (auto it : ans)
cout << it.second << " "; // Print the coordinate (second element of pair)
cout << endl; // Newline after coordinates
}
TagsB. Divan and a New ProjectCodeforces Round 757 (Div. 2)Codeforces Solution
Raunit Verma
Technical Writer at CodingKaro
Share this on