#include <bits/stdc++.h>
#define file(name) freopen(name".inp" , " r " , stdin);freopen(name".out" , " w " , stdout);
#define faster ios_base :: sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ;
#define pii pair < int , int >
#define fi first
#define se second
#define mii map< int , int>
#define reset(a,val) memset(a ,val , sizeof(a) )
#define count_bit(i) __builtin_popcountll(i)
#define mask(i) ((1LL << (i)))
#define status(x, i) (((x) >> (i)) & 1)
#define set_on(x, i) ((x) | mask(i))
#define set_off(x, i) ((x) & ~mask(i))
#define endl '\n'
#define ll long long
#define int ll
const int maxn = 1e6 + 6;
const int mod= 1e9 + 7;
const ll INFLL= 3e18 + 5;
const int INF = 1e9 + 5 ;
const int LOG = 20 ;
template <class T> inline T sqr(T x) { return x * x; };
template <class T> inline int Power(T x, int y)
{ if (!y) return 1; if (y & 1) return sqr(Power(x, y / 2)) % mod * x % mod; return sqr(Power(x, y / 2)) % mod; }
template<class T> bool minimize(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool maximize(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
inline int gcd(int x, int y)
{ return y ? gcd(y , x % y) : x;}
inline int lcm(int x, int y) { return x * y / gcd(x, y); }
using namespace std;
int k , n;
int dist = 0;
vector<pii> path;
// sub1 2
int calc(int x)
{
int res = 0;
for (int i = 0 ; i < path.size() ; ++i)
res += (abs(path[i].fi - x) + abs(path[i].se - x));
return res;
}
void solve1()
{
vector<int> a;
for (int i = 0 ; i < path.size() ; ++i)
a.push_back(path[i].fi) , a.push_back(path[i].se);
sort(a.begin() ,a.end());
int k = (a.size() + 1) / 2 - 1;
cout << dist + min(calc(a[k]) , min(calc(a[k - 1]) , calc(a[k + 1]))) << endl;
// cout << a[k] << " " << calc(a[k]) << endl;
}
//----------------------------------------------------------------------------------------------
// sub 3 4 5
priority_queue<int , vector<int> , less<int>> low;
priority_queue<int , vector<int> , greater<int>> up;
int sum_low = 0;
int sum_up = 0;
int f[maxn];
void INSERT(int val)
{
int a = (low.empty() ? INF : low.top());
if (a < val)
{
up.push(val);
sum_up += val;
}
else
{
low.push(val);
sum_low += val;
}
if (up.size() + 1 < low.size())
{
sum_low -= low.top();
sum_up += low.top();
up.push(low.top());
low.pop();
}
if (low.size() < up.size())
{
sum_up -= up.top();
sum_low += up.top();
low.push(up.top());
up.pop();
}
}
void solve2()
{
sort(path.begin() , path.end() , [&](pii a , pii b)
{
return a.fi + a.se < b.fi + b.se;
});
sum_low = 0;
sum_up = 0;
for (int i = 0 ; i < path.size() ; ++i)
{
INSERT(path[i].fi);
INSERT(path[i].se);
f[i] = sum_up - sum_low;
}
sum_low = 0;
sum_up = 0;
while (up.size()) up.pop();
while (low.size()) low.pop();
int ans = f[path.size() - 1];
for (int i = path.size() - 1 ; i >= 0 ; --i)
{
INSERT(path[i].fi);
INSERT(path[i].se);
minimize(ans , sum_up - sum_low + f[i - 1]);
}
cout << ans + dist << endl;
}
void solve()
{
cin >> k >> n ;
for (int i = 1 ; i <= n; ++i)
{
char a , b ; int x , y ; cin >> a >> x >> b >> y;
if (a == b) dist += abs(x - y);
else path.push_back({x , y});
}
dist += path.size();
if (k == 1) solve1();
if (k == 2) solve2();
}
int32_t main()
{
faster;
// file("txt");
// int t ; cin >> t ; while (t--)
solve();
return 0;
}
Compilation message
bridge.cpp: In function 'long long int calc(long long int)':
bridge.cpp:54:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 0 ; i < path.size() ; ++i)
| ~~^~~~~~~~~~~~~
bridge.cpp: In function 'void solve1()':
bridge.cpp:64:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int i = 0 ; i < path.size() ; ++i)
| ~~^~~~~~~~~~~~~
bridge.cpp: In function 'void solve2()':
bridge.cpp:136:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | for (int i = 0 ; i < path.size() ; ++i)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
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 |
# |
Verdict |
Execution time |
Memory |
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 |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
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 |
15 ms |
4880 KB |
Output is correct |
13 |
Correct |
25 ms |
5076 KB |
Output is correct |
14 |
Correct |
19 ms |
4820 KB |
Output is correct |
15 |
Correct |
14 ms |
2776 KB |
Output is correct |
16 |
Correct |
16 ms |
5076 KB |
Output is correct |
17 |
Correct |
18 ms |
5072 KB |
Output is correct |
18 |
Correct |
21 ms |
5076 KB |
Output is correct |
19 |
Correct |
24 ms |
5072 KB |
Output is correct |
20 |
Correct |
16 ms |
5076 KB |
Output is correct |
21 |
Correct |
22 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
460 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 |
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 |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
472 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
464 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 |
464 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
496 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
22 ms |
7760 KB |
Output is correct |
26 |
Correct |
42 ms |
8248 KB |
Output is correct |
27 |
Correct |
51 ms |
8744 KB |
Output is correct |
28 |
Correct |
58 ms |
9420 KB |
Output is correct |
29 |
Correct |
52 ms |
8984 KB |
Output is correct |
30 |
Correct |
27 ms |
6100 KB |
Output is correct |
31 |
Correct |
24 ms |
8224 KB |
Output is correct |
32 |
Correct |
39 ms |
9368 KB |
Output is correct |
33 |
Correct |
42 ms |
9428 KB |
Output is correct |
34 |
Correct |
47 ms |
9412 KB |
Output is correct |
35 |
Correct |
28 ms |
8908 KB |
Output is correct |
36 |
Correct |
41 ms |
9168 KB |
Output is correct |
37 |
Correct |
27 ms |
7768 KB |
Output is correct |