#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll INF=1e18+5;
#define F first
#define S second
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int k, n;
cin >> k >> n;
char c, cc;
vector<vector<ll>> a(n, vector<ll>(3));
for(int i=0; i<n; i++) {
cin >> c;
cin >> a[i][0];
cin >> cc;
cin >> a[i][1];
a[i][2]=(c==cc);
if(a[i][0]>a[i][1]) {
swap(a[i][0], a[i][1]);
}
}
if(k==1 && n<=1000) {
vector<ll> pos;
for(int i=0; i<n; i++) {
pos.push_back(a[i][0]);
pos.push_back(a[i][1]);
}
ll rj=INF, cur;
for(int i=0; i<2*n; i++) {
cur=0;
for(int j=0; j<n; j++) {
cur+=a[j][1]-a[j][0];
if(a[j][2]==0) {
cur++;
if(pos[i]<a[j][0]) {
cur+=2*(a[j][0]-pos[i]);
}
else if(pos[i]>a[j][1]) {
cur+=2*(pos[i]-a[j][1]);
}
}
}
rj=min(rj, cur);
}
cout << rj << '\n';
return 0;
}
if(k==2 && n<=100) {
vector<ll> pos;
for(int i=0; i<n; i++) {
pos.push_back(a[i][0]);
pos.push_back(a[i][1]);
}
ll rj=INF, cur;
for(int i=0; i<2*n-1; i++) {
for(int j=i+1; j<2*n; j++) {
cur=0;
for(int l=0; l<n; l++) {
cur+=a[l][1]-a[l][0];
if(a[l][2]==0) {
cur++;
pair<ll, ll> cos;
if(pos[i]<a[l][0]) {
cos.F=2*(a[l][0]-pos[i]);
}
else if(pos[i]>a[l][1]) {
cos.F=2*(pos[i]-a[l][1]);
}
if(pos[j]<a[l][0]) {
cos.S+=2*(a[l][0]-pos[j]);
}
else if(pos[j]>a[l][1]) {
cos.S+=2*(pos[j]-a[l][1]);
}
cur+=min(cos.F, cos.S);
}
}
rj=min(rj, cur);
}
}
cout << rj << '\n';
return 0;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
408 KB |
Output is correct |
4 |
Correct |
7 ms |
412 KB |
Output is correct |
5 |
Correct |
7 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
5 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 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 |
4 ms |
412 KB |
Output is correct |
4 |
Correct |
7 ms |
412 KB |
Output is correct |
5 |
Correct |
7 ms |
404 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
5 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Incorrect |
16 ms |
5716 KB |
Output isn't correct |
13 |
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 |
4 ms |
212 KB |
Output is correct |
4 |
Correct |
6 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
5 ms |
212 KB |
Output is correct |
8 |
Correct |
5 ms |
212 KB |
Output is correct |
9 |
Correct |
5 ms |
212 KB |
Output is correct |
10 |
Correct |
5 ms |
212 KB |
Output is correct |
11 |
Correct |
5 ms |
212 KB |
Output is correct |
12 |
Correct |
5 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 |
4 ms |
212 KB |
Output is correct |
4 |
Correct |
5 ms |
328 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
5 ms |
212 KB |
Output is correct |
8 |
Correct |
5 ms |
320 KB |
Output is correct |
9 |
Correct |
5 ms |
212 KB |
Output is correct |
10 |
Correct |
6 ms |
212 KB |
Output is correct |
11 |
Correct |
5 ms |
212 KB |
Output is correct |
12 |
Correct |
5 ms |
212 KB |
Output is correct |
13 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
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 |
4 ms |
212 KB |
Output is correct |
4 |
Correct |
6 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
6 ms |
212 KB |
Output is correct |
8 |
Correct |
5 ms |
332 KB |
Output is correct |
9 |
Correct |
5 ms |
212 KB |
Output is correct |
10 |
Correct |
6 ms |
212 KB |
Output is correct |
11 |
Correct |
5 ms |
212 KB |
Output is correct |
12 |
Correct |
5 ms |
212 KB |
Output is correct |
13 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |