This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define debug(x) cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ld long double
#define nl endl
#define tb '\t'
#define sp ' '
#define sqr(x) (x)*(x)
#define arr3 array<int, 3>
using namespace std;
const int MX=30005, MOD=1000000007, BLOCK=160, INF=2e9+7, LG=62;
const ll INFF=1000000000000000007;
const ld ERR=1e-6, pi=3.14159265358979323846;
ll N, K, ans, sum;
vector<int> A, B;
int main(){
fastio;
cin >> K >> N;
for(int i=0; i<N; i++){
char a, c;
int b, d;
cin >> a >> b >> c >> d;
if(a==c){
ans+=abs(b-d);
}else{
ans++;
if(a=='B')
swap(b, d);
A.pb(b);
B.pb(d);
sum+=b+d;
}
}
sort(all(A));
sort(all(B));
ll loc=0, ptrA=0, ptrB=0, best=sum;
N=A.size();
while(ptrA<A.size() && ptrB<B.size()){
ll dist;
if(A[ptrA]<=B[ptrB]){
dist=A[ptrA]-loc;
sum=sum+dist*(ptrA+ptrB)-dist*(N-ptrA+N-ptrB);
loc=A[ptrA];
ptrA++;
}else{
dist=B[ptrB]-loc;
sum=sum+dist*(ptrA+ptrB)-dist*(N-ptrA+N-ptrB);
loc=B[ptrB];
ptrB++;
}
best=min(best, sum);
}
while(ptrA<A.size()){
ll dist=A[ptrA]-loc;
sum=sum+dist*(ptrA+ptrB)-dist*(N-ptrA+N-ptrB);
loc=A[ptrA];
ptrA++;
}
while(ptrB<B.size()){
ll dist=B[ptrB]-loc;
sum=sum+dist*(ptrB+ptrB)-dist*(N-ptrB+N-ptrB);
loc=B[ptrB];
ptrB++;
}
cout << ans+best << nl;
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:50:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | while(ptrA<A.size() && ptrB<B.size()){
| ~~~~^~~~~~~~~
bridge.cpp:50:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | while(ptrA<A.size() && ptrB<B.size()){
| ~~~~^~~~~~~~~
bridge.cpp:65:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | while(ptrA<A.size()){
| ~~~~^~~~~~~~~
bridge.cpp:71:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | while(ptrB<B.size()){
| ~~~~^~~~~~~~~
# | 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... |