이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(ll i = a; i < ll(b); ++i)
#define trav(a, v) for(auto& a : v)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define D(x) cerr << #x << " = " << x << endl
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
int main() {
cin.sync_with_stdio(false);
ll k,n;
ll totDist = 0;
vector<pii> intervals;
vector<ll> points;
cin>>k>>n;
rep(i,0,n){
char a,b;
ll c,d;
cin>>a>>c>>b>>d;
if(a!=b){
intervals.push_back(pii(c,d));
points.push_back(c);
points.push_back(d);
}else{
totDist += abs(c-d);
}
}
ll bestDist = 0;
if(intervals.size()>0){
bestDist = 1e18;
sort(all(points));
points.resize(distance(points.begin(), unique(points.begin(), points.end())) );
rep(a,0,points.size()){
rep(b,a,points.size()){
ll dist = 0;
rep(i,0,intervals.size()){
ll dist1 = abs(intervals[i].first-points[a])+abs(intervals[i].second-points[a]);
ll dist2 = abs(intervals[i].first-points[b])+abs(intervals[i].second-points[b]);
dist += min(dist1,dist2);
}
bestDist = min(dist,bestDist);
}
}
}
totDist += bestDist;
totDist += intervals.size();
cout<<totDist<<endl;
}
# | 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... |