#include<iostream>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<utility>
#include<unordered_map>
#include<vector>
#include<set>
#define pii pair<int,int>
#define pll pair<ll, ll>
#define F first
#define S second
#define ll long long
#define pb push_back
using namespace std;
const int N = (int)1e5 + 5, mod = (ll)1e9 + 7;
int n, k, x, y;
char ch, hc;
ll sm = 0, sm2 = (ll)1e18 + 18;
vector<ll int> cv;
bool cmp(ll int i,ll int j){
// cout << i << " " << j << endl;
return i < j;
}
void sub(){
vector<pii> vc;
cerr << "\n entered #sub 2 \n";
for(int i = 0; i < n; i++){
cin >> ch >>x >> hc >> y;
if(ch == hc)sm += abs(x - y);
else vc.pb({min(x,y), max(x,y)});
}
n = vc.size();
if(n == 0){
cout << sm << endl;
exit(0);
}
for (int i = 0; i < n; ++i)
cv.push_back(vc[i].F), cv.push_back(vc[i].S);
for (int i = 0; i < (n << 1); i ++){
for (int j = 0; j < (n << 1); j ++){
ll tmt = 0;
for (int k = 0; k < n; k ++){
int tmv1 = abs(vc[k].F - cv[i]) + abs(vc[k].S - cv[i]);
int tmv2 = abs(vc[k].F - cv[j]) + abs(vc[k].S - cv[j]);
tmt += min(tmv1, tmv2);
}
sm2 = min(sm2, tmt);
}
}
cout << sm + sm2 + vc.size() << endl;
exit(0);
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> k >> n;
vector<int> vc;
if(k > 2)exit(0);
if(k == 2) sub();
// cerr << "\n enteres #sub1 \n";
for(int i = 1; i <= n; i ++){
cin >> ch >> x >> hc >> y;
if(ch == hc) sm += abs(x - y);
else sm ++, vc.pb(x), vc.pb(y);
}
sort(vc.begin(), vc.end());
int pos = vc[(vc.size() - 1) / 2];
for(auto x : vc)sm += abs(x - pos);
cout << sm << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
18 ms |
1416 KB |
Output is correct |
13 |
Correct |
36 ms |
1492 KB |
Output is correct |
14 |
Correct |
26 ms |
1492 KB |
Output is correct |
15 |
Correct |
21 ms |
984 KB |
Output is correct |
16 |
Correct |
25 ms |
1492 KB |
Output is correct |
17 |
Correct |
25 ms |
1416 KB |
Output is correct |
18 |
Correct |
31 ms |
1420 KB |
Output is correct |
19 |
Correct |
35 ms |
1452 KB |
Output is correct |
20 |
Correct |
24 ms |
1500 KB |
Output is correct |
21 |
Correct |
32 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
9 ms |
340 KB |
Output is correct |
4 |
Correct |
8 ms |
328 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
12 ms |
212 KB |
Output is correct |
8 |
Correct |
13 ms |
212 KB |
Output is correct |
9 |
Correct |
12 ms |
212 KB |
Output is correct |
10 |
Correct |
12 ms |
212 KB |
Output is correct |
11 |
Correct |
13 ms |
212 KB |
Output is correct |
12 |
Correct |
12 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
8 ms |
212 KB |
Output is correct |
4 |
Correct |
9 ms |
324 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
12 ms |
324 KB |
Output is correct |
8 |
Correct |
12 ms |
328 KB |
Output is correct |
9 |
Correct |
12 ms |
320 KB |
Output is correct |
10 |
Correct |
13 ms |
212 KB |
Output is correct |
11 |
Correct |
12 ms |
320 KB |
Output is correct |
12 |
Correct |
12 ms |
212 KB |
Output is correct |
13 |
Execution timed out |
2094 ms |
340 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
10 ms |
324 KB |
Output is correct |
4 |
Correct |
9 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
14 ms |
324 KB |
Output is correct |
8 |
Correct |
13 ms |
212 KB |
Output is correct |
9 |
Correct |
13 ms |
320 KB |
Output is correct |
10 |
Correct |
12 ms |
212 KB |
Output is correct |
11 |
Correct |
16 ms |
324 KB |
Output is correct |
12 |
Correct |
13 ms |
212 KB |
Output is correct |
13 |
Execution timed out |
2087 ms |
340 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |