이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
Solution for APIO 2015 - Palembang Bridges
Tags :
*/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("Ofast")
#pragma GCC optimization ("unroll-loops")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
ll MOD = 1000000007;
ll power(ll base, ll n){
if (n == 0) return 1;
if (n == 1) return base;
ll halfn = power(base, n/2);
if (n % 2 == 0) return (halfn * halfn) % MOD;
return (((halfn * halfn) % MOD) * base) % MOD;
}
ll inverse(ll n){
return power(n, MOD-2);
}
ll add(ll a, ll b){
return (a+b) % MOD;
}
ll mul(ll a, ll b){
a %= MOD;
return (a*b) % MOD;
}
ll gcd(ll a, ll b){
if (a == 1) return 1;
if (a == 0) return b;
return gcd(b%a, a);
}
void solve(){
int k, n; cin >> k >> n;
ll ans = 0;
vector<pair<ll, ll>> loc;
FOR(i,0,n){
char z1, z2;
ll l, r;
cin >> z1 >> l >> z2 >> r;
if (l > r) swap(l,r);
if (z1 == z2){
ans += (r-l);
}else{
ans += (r-l+1);
loc.pb({l, r});
}
}
n = loc.size();
sort(loc.begin(), loc.end());
vector<ll> l(n), r(n);
FOR(i,0,n){
l[i] = loc[i].fi;
r[i] = loc[i].se;
}
if (k==1){
sort(l.begin(), l.end());
sort(r.begin(), r.end());
ll mini = MOD*MOD;
vector<ll> pre_r(n), suf_l(n);
pre_r[0] = r[0];
FOR(i,1,n){
pre_r[i] = pre_r[i-1] + r[i];
}
suf_l[n-1] = l[n-1];
ROF(i,n-2,0){
suf_l[i] = suf_l[i+1] + l[i];
}
FOR(i,0,n){
ll ans_r = r[i] * (ll) (i+1) - pre_r[i];
if (r[i] >= l[n-1]){
mini = min(mini, ans_r);
continue;
}
ll pos_r = upper_bound(l.begin(), l.end(), r[i]) - l.begin();
ans_r += suf_l[pos_r] - r[i] * (ll) (n-pos_r);
mini = min(mini, ans_r);
}
FOR(i,0,n){
ll ans_l = suf_l[i] - l[i] * (ll) (n-i);
if (l[i] <= r[0]){
mini = min(mini, ans_l);
continue;
}
ll pos_l = upper_bound(r.begin(), r.end(), l[i]) - r.begin();
ans_l += l[i] * (ll) (pos_l) - pre_r[pos_l-1];
mini = min(mini, ans_l);
}
ans+=mini*2;
cout<<ans;
return;
}
}
int main() {
ios::sync_with_stdio(false);
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bridge.cpp:10: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
10 | #pragma GCC optimization ("Ofast")
|
bridge.cpp:11: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
11 | #pragma GCC optimization ("unroll-loops")
|
# | 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... |