이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |