제출 #922877

#제출 시각아이디문제언어결과실행 시간메모리
922877vjudge1Palembang Bridges (APIO15_bridge)C++17
22 / 100
140 ms20752 KiB
#include<bits/stdc++.h> #define X first #define Y second #define all(x) begin(x), end(x) #define FOR(i, a, b) for(int i = (a); i <= (b); i++) #define FORD(i, b, a) for(int i = (b); i >= (a); i--) #define REP(i, a, b) for (int i = (a); i < (b); i++) #define mxx max_element #define mnn min_element #define SQR(x) (1LL * (x) * (x)) #define MASK(i) (1LL << (i)) #define Point Vector #define left Left #define right Right #define div Div using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; typedef pair<db, db> pdb; typedef pair<ld, ld> pld; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef pair<ll, ll> pll; typedef pair<ll, pll> plll; typedef pair<ll, int> pli; typedef pair<ll, pii> plii; template<class A, class B> bool maximize(A& x, B y) { if (x < y) return x = y, true; else return false; } template<class A, class B> bool minimize(A& x, B y) { if (x > y) return x = y, true; else return false; } /* END OF TEMPLATE */ const int N = 3e5 + 7; int n, k; vector<pii> seg; ll res = 0; namespace sub1 { ll pre[N], suf[N]; ll pre_cnt[N], suf_cnt[N]; vector<int> E; int idx(int x) { return lower_bound(all(E), x) - E.begin() + 1; } void buildE() { set<int> s; for (auto x : seg) { s.insert(x.X); s.insert(x.Y); } E = vector<int>(s.begin(), s.end()); } void Process() { buildE(); int m = (int)E.size(); ll ANS = 9e18; pre[0] = suf[n + 1] = 0; pre_cnt[0] = suf_cnt[n + 1] = 0; for (auto x : seg) { pre[idx(x.Y)]+=x.Y; pre_cnt[idx(x.Y)]++; suf[idx(x.X)]+=x.X; suf_cnt[idx(x.X)]++; } FOR(i, 1, m) { pre[i]+=pre[i - 1]; pre_cnt[i]+=pre_cnt[i - 1]; } FORD(i, m, 1) { suf[i]+=suf[i + 1]; suf_cnt[i]+=suf_cnt[i + 1]; } FOR(i, 1, m) { int x = E[i - 1]; ll ans = 2LL * (pre_cnt[i - 1] * x - pre[i - 1]) + pre_cnt[i - 1]; ans+=2LL * (suf[i + 1] - suf_cnt[i + 1] * x) + suf_cnt[i + 1]; minimize(ANS, res + ans + (int)seg.size() - pre_cnt[i - 1] - suf_cnt[i + 1]); } cout<<ANS; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>k>>n; FOR(i, 1, n) { char P, Q; int A, B; cin>>P>>A>>Q>>B; res+=abs(A - B); if (P != Q) seg.push_back({min(A, B), max(A, B)}); } if (seg.empty()) return cout<<res, 0; if (k == 1) { sub1::Process(); return 0; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...