This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3,unroll-loops,Ofast")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MAX 1000007
#define pb push_back
#define mp make_pair
#define int long long
#define f first
#define s second
#define vi vector<int>
#define pii pair<int,int>
#define si set<int>
#define vpii vector<pair<int,int>>
const int mod = 2019201997;
const int INF = 1e18;
// myMap.begin()->first : key
// myMap.begin()->second : value
int epow(int a,int b){int ans=1;while(b){if(b&1) ans*=a;a*=a;/*ans%=mod;a%=mod;*/b>>=1;}return ans/*%mod*/;}
int gcd(int a,int b) {if(a<b)swap(a,b);while(b){int tmp=b;b=a%b;a=tmp;}return a;}
int mul(int a,int b){return ((a%mod)*(b%mod))%mod;}
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
struct DSU {
vi e,sayi; void init(int N) { e = vi(N,-1); sayi = vi(N);}
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool sameSet(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y) { // union by size
x = get(x), y = get(y); if (x == y) return 0;
if (e[x] > e[y]) swap(x,y);
e[x] += e[y]; e[y] = x; sayi[x]+=sayi[y]; return 1;
}
};
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
tree_order_statistics_node_update>ordered_set;
void solve()
{
int n,k; cin >> k >> n;
vi v; vpii arr;
int ans=0;
for(int i=1;i<=n;i++)
{
int b,d;char a,c; cin>>a>>b>>c>>d;
if(a==c) ans+=abs(b-d);
else
{
arr.pb({b,d});
v.pb((b+d)/2);
}
}
sort(v.begin(),v.end());
if(k==1)
{
if(v.size()%2)
{
int cvp=v[v.size()/2];
for(auto u : arr)
{
ans += abs(u.f-cvp)+abs(u.s-cvp)+1;
}
}
else
{
int mn=0,curr=0;
int cvp=v[v.size()/2];
for(auto u : arr)
{
curr += abs(u.f-cvp)+abs(u.s-cvp)+1;
}
mn=curr; curr=0;
cvp=v[v.size()/2-1];
for(auto u : arr)
{
curr += abs(u.f-cvp)+abs(u.s-cvp)+1;
}
mn=min(mn,curr);
ans+=mn;
}
}
cout << ans << endl;
return;
}
int32_t main() {
// freopen("hayfeast.in", "r", stdin);
// freopen("hayfeast.out", "w", stdout);
ios_base::sync_with_stdio(0);cin.tie(0);
int t=1;
// cin >> t;
while (t--) solve();
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... |