#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,a,b) for(int i=(a);i<=(int)(b);++i)
#define FORB(i,a,b) for(int i=(a);i>=(int)(b);--i)
#define E '\n'
#define name "main"
#define X first
#define Y second
#define int ll
#define ii pair<int,int>
const ll oo = 1e18+19;
const int N = 1e5;
multiset<int> low;
multiset<int> up;
int numBrigde, numPeople;
int pre[N+13];
int suf[N+13];
int sameside;
int sumLow;
int sumUp;
vector<ii> people;
bool minimize(ll &x, ll y){
return x > y ? x = y,1 : 0;
}
void ins(int x){
if (low.size() && *low.rbegin() < x) up.insert(x),sumUp += x;
else low.insert(x), sumLow += x;
while(up.size() > low.size()){
sumLow += *up.begin();
sumUp -= *up.begin();
low.insert(*up.begin());
up.erase(up.find(*up.begin()));
}
while(low.size() - 1 > up.size()){
sumLow -= *low.rbegin();
sumUp += *low.rbegin();
up.insert(*low.rbegin());
low.erase(low.find(*low.rbegin()));
}
}
int getMed(){
return *low.rbegin();
}
void prepare(){
low.clear(); up.clear();
sumUp = sumLow = 0;
FOR(i,0,people.size()-1){
ins(people[i].X);
ins(people[i].Y);
pre[i] = sumUp - sumLow;
}
low.clear(); up.clear();
sumUp = sumLow = 0;
FORB(i,people.size()-1,0){
ins(people[i].X);
ins(people[i].Y);
suf[i] = sumUp - sumLow;
}
}
signed main()
{
ios::sync_with_stdio(0); cin.tie(0); cout .tie(0);
// freopen(name".inp","r",stdin); freopen(name".out","w",stdout);
cin >> numBrigde >> numPeople;
FOR(i,1,numPeople){
char Ax,By;
int x,y;
cin >> Ax >> x >> By >> y;
if (Ax == By)
sameside += abs(x - y);
else
people.push_back(ii(x,y));
}
sort(people.begin(),people.end(),[&](ii A,ii B){
return (A.X + A.Y) < (B.X + B.Y);
});
prepare();
ll res = pre[people.size()-1];
if (numBrigde == 2){
FOR(i,1,people.size()-1){
minimize(res,suf[i] + pre[i-1]);
}
}
cout << res + sameside + people.size();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
131 ms |
12752 KB |
Output is correct |
13 |
Correct |
207 ms |
12788 KB |
Output is correct |
14 |
Correct |
201 ms |
11460 KB |
Output is correct |
15 |
Correct |
105 ms |
7616 KB |
Output is correct |
16 |
Correct |
113 ms |
12744 KB |
Output is correct |
17 |
Correct |
133 ms |
12712 KB |
Output is correct |
18 |
Correct |
111 ms |
12712 KB |
Output is correct |
19 |
Correct |
149 ms |
12784 KB |
Output is correct |
20 |
Correct |
139 ms |
12764 KB |
Output is correct |
21 |
Correct |
138 ms |
12792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
352 KB |
Output is correct |
25 |
Correct |
131 ms |
13612 KB |
Output is correct |
26 |
Correct |
192 ms |
13764 KB |
Output is correct |
27 |
Correct |
209 ms |
14512 KB |
Output is correct |
28 |
Correct |
223 ms |
15096 KB |
Output is correct |
29 |
Correct |
218 ms |
15160 KB |
Output is correct |
30 |
Correct |
85 ms |
8096 KB |
Output is correct |
31 |
Correct |
129 ms |
14504 KB |
Output is correct |
32 |
Correct |
134 ms |
15084 KB |
Output is correct |
33 |
Correct |
110 ms |
14708 KB |
Output is correct |
34 |
Correct |
142 ms |
15160 KB |
Output is correct |
35 |
Correct |
141 ms |
14648 KB |
Output is correct |
36 |
Correct |
136 ms |
14876 KB |
Output is correct |
37 |
Correct |
136 ms |
13580 KB |
Output is correct |