이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int k, n;
int res = 0;
int m = 0;
vector<int> vc;
map<int, int> mp;
struct TCitizen
{
char p;
int s;
char q;
int t;
} a[maxN];
void ReadInput()
{
cin >> k >> n;
for(int i=1; i<=n; i++)
{
char p, q;
int s, t;
cin >> p >> s >> q >> t;
vc.pb((s + t) / 2);
if(p == q)
{
res += abs(s - t);
continue;
}
vc.pb(s);
vc.pb(t);
if(s > t)
swap(s, t);
a[++m] = {p, s, q, t};
}
}
void Solve()
{
assert(k == 1);
if(!m)
{
cout << res;
return;
}
int _min = oo;
sort(vc.begin(), vc.end());
vc.erase(unique(vc.begin(), vc.end()), vc.end());
sort(a + 1, a + m + 1, [](TCitizen p, TCitizen q)
{
return p.s < q.s;
});
int p = 1;
int sum = 0;
for(int x : vc)
{
while(p <= m && a[p].s <= x)
{
sum += a[p].s;
p++;
}
mp[x] += x * (p - 1) - sum;
}
sort(a + 1, a + m + 1, [](TCitizen p, TCitizen q)
{
return p.t < q.t;
});
p = 1;
sum = 0;
for(int x : vc)
{
while(p <= m && a[p].t <= x)
{
sum += a[p].t;
p++;
}
mp[x] += x * (p - 1) - sum;
}
reverse(vc.begin(), vc.end());
sort(a + 1, a + m + 1, [](TCitizen p, TCitizen q)
{
return p.s > q.s;
});
p = 1;
sum = 0;
for(int x : vc)
{
while(p <= m && a[p].s >= x)
{
sum += a[p].s;
p++;
}
mp[x] += -(x * (p - 1) - sum);
}
sort(a + 1, a + m + 1, [](TCitizen p, TCitizen q)
{
return p.t > q.t;
});
p = 1;
sum = 0;
for(int x : vc)
{
while(p <= m && a[p].t >= x)
{
sum += a[p].t;
p++;
}
mp[x] += -(x * (p - 1) - sum);
}
for(pair<int, int> v : mp)
_min = min(_min, v.se);
_min += m;
cout << res + _min;
}
int32_t main()
{
// freopen("x.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |