#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<iomanip>
#include<algorithm>
#include<utility>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<deque>
#include<math.h>
#include<assert.h>
#include<string.h>
#include<chrono>
#include<bitset>
using namespace std;
using namespace std::chrono;
const double epsi = (double) 1e-6;
const int64_t INF = (int64_t) 1e18 + 100;
const int64_t mINF = (int64_t) 1e9 + 5;
const int64_t MOD = 1e9 + 7;
const int64_t MOD2 = 998244353;
const int nbit = 24;
const int ndig = 10;
const int nchar = 26;
const int p1 = 31;
const int p2 = 53;
const int D = 4;
int dr[D] = {-1, 0, 1, 0};
int dc[D] = {0, 1, 0, -1};
const int Dk = 8;
int drk[Dk] = {2, 2, -2, -2, 1, 1, -1, -1};
int dck[Dk] = {1, -1, 1, -1, 2, -2, 2, -2};
bool mod_cmp(pair<int, int>& t1, pair<int, int>& t2)
{
return t1.first + t1.second < t2.first + t2.second;
}
struct Solution
{
int k; int n;
int64_t suml; int64_t sumr;
priority_queue<int64_t> left;
priority_queue<int64_t> right;
vector<pair<int, int> > v;
Solution() {}
void solve()
{
cin >> k >> n;
int64_t sum = 0;
for(int i = 0; i < n; i++)
{
char a; int x;
char b; int y;
cin >> a >> x >> b >> y;
if(a == b) sum += Abs(x - y);
else v.emplace_back(x, y);
}
suml = 0; sumr = 0;
sort(v.begin(), v.end(), mod_cmp);
n = (int) v.size();
sum += n;
if(n == 0)
{
cout << sum << "\n";
return;
}
vector<int64_t> prefix(n, 0);
for(int i = 0; i < n; i++)
{
iinsert(v[i].first);
iinsert(v[i].second);
prefix[i] = sumr - suml;
}
if(k == 1) cout << prefix[n - 1] + sum << "\n";
else
{
while(!left.empty()) left.pop();
while(!right.empty()) right.pop();
suml = 0; sumr = 0;
int64_t ans = INF;
for(int i = n - 1; i >= 0; i--)
{
iinsert(v[i].first);
iinsert(v[i].second);
int64_t p = 0;
if(i != 0) p = prefix[i - 1];
ans = min(ans, sumr - suml + p);
}
cout << ans + sum << "\n";
}
}
void iinsert(int64_t val)
{
int64_t median = INF;
if((int) left.size() != 0) median = left.top();
if(val <= median)
{
left.push(val);
suml += val;
}
else
{
right.push(-val);
sumr += val;
}
if((int) right.size() + 1 < (int) left.size())
{
int64_t nxt = left.top();
left.pop();
right.push(-nxt);
suml -= nxt;
sumr += nxt;
}
else if((int) left.size() < (int) right.size())
{
int64_t nxt = -right.top();
right.pop();
left.push(nxt);
sumr -= nxt;
suml += nxt;
}
}
int modadd(int t1, int t2)
{
int res = t1 + t2;
if(res >= MOD) res -= MOD;
return res;
}
int modsub(int t1, int t2)
{
int res = t1 - t2;
if(res < 0) res += MOD;
return res;
}
int modmul(int t1, int t2)
{
int64_t res = 1LL * t1 * t2;
return res % MOD;
}
int64_t Abs(int64_t t1)
{
if(t1 < 0) return -t1;
return t1;
}
int64_t MASK(int i)
{
return (1LL << i);
}
bool BIT(int64_t t1, int j)
{
return (t1 & MASK(j));
}
};
void __setup()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
int main()
{
__setup();
// auto start = high_resolution_clock::now();
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
Solution().solve();
}
// auto stop = high_resolution_clock::now();
// auto duration = duration_cast<microseconds>(stop - start);
// cerr << duration.count() << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
444 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
26 ms |
4844 KB |
Output is correct |
13 |
Correct |
43 ms |
4452 KB |
Output is correct |
14 |
Correct |
34 ms |
4048 KB |
Output is correct |
15 |
Correct |
25 ms |
2776 KB |
Output is correct |
16 |
Correct |
26 ms |
4452 KB |
Output is correct |
17 |
Correct |
33 ms |
4312 KB |
Output is correct |
18 |
Correct |
33 ms |
4300 KB |
Output is correct |
19 |
Correct |
38 ms |
4300 KB |
Output is correct |
20 |
Correct |
27 ms |
4312 KB |
Output is correct |
21 |
Correct |
35 ms |
4312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
372 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
500 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
29 ms |
5080 KB |
Output is correct |
26 |
Correct |
54 ms |
5040 KB |
Output is correct |
27 |
Correct |
70 ms |
6096 KB |
Output is correct |
28 |
Correct |
74 ms |
6700 KB |
Output is correct |
29 |
Correct |
73 ms |
6608 KB |
Output is correct |
30 |
Correct |
37 ms |
3572 KB |
Output is correct |
31 |
Correct |
31 ms |
6148 KB |
Output is correct |
32 |
Correct |
56 ms |
6688 KB |
Output is correct |
33 |
Correct |
58 ms |
6744 KB |
Output is correct |
34 |
Correct |
59 ms |
6604 KB |
Output is correct |
35 |
Correct |
38 ms |
6104 KB |
Output is correct |
36 |
Correct |
58 ms |
6344 KB |
Output is correct |
37 |
Correct |
23 ms |
5080 KB |
Output is correct |