Submission #317993

#TimeUsernameProblemLanguageResultExecution timeMemory
317993ali_tavakoliPalembang Bridges (APIO15_bridge)C++17
22 / 100
61 ms3820 KiB
//In the name of Allah #include<bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' #define F first #define S second #define pb push_back #define all(x) x.begin(), x.end() #pragma GCC optimize("O2") const int N = 1e5 + 5; int n, k, med[N][2], med2[N][2], p, q; char ch1, ch2; ll base; vector<pair<ll, pair<int, int> > > ve; priority_queue<int> q1, q3; priority_queue<int, vector<int>, greater<int> > q2, q4; int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); ve.reserve(N); cin >> k >> n; for(int i = 0; i < n; i++) { cin >> ch1 >> p >> ch2 >> q; //scanf("%c%d%c%d", &ch1, &p, &ch2, &q); if(ch1 != ch2) ve.pb({p + q, {min(p, q), max(p, q)}}); else base += abs(p - q); } base += ve.size(); sort(all(ve)); for(int i = 0; i < ve.size(); i++) { auto x = ve[i]; q1.push(x.S.F); q2.push(x.S.S); while(q1.top() > q2.top()) { ll to1 = q1.top(), to2 = q2.top(); q1.pop();q2.pop(); q1.push(to2); q2.push(to1); } med[i][0] = q1.top(); med[i][1] = q2.top(); } //cout << med[ve.size() - 1][0] << " " << med[ve.size() - 1][1] << endl; //if(!n) //cout << base << endl; if(!ve.size()) { cout << base << endl; return 0; } if(k == 1|| ve.size() == 1) { ll ans1, ans2; ans1 = ans2 = 0; for(int i = 0; i < ve.size(); i++) { ans1 += abs(ve[i].S.F - med[ve.size() - 1][0]); ans1 += abs(ve[i].S.S - med[ve.size() - 1][0]); ans2 += abs(ve[i].S.F - med[ve.size() - 1][1]); ans2 += abs(ve[i].S.S - med[ve.size() - 1][1]); } cout << min(ans1, ans2) + base << endl; } else { ll ans = 1e15 + 5; for(int i = ve.size() - 1; i >= 0; i--) { auto x = ve[i]; q3.push(x.S.F); q4.push(x.S.S); while(q3.top() > q4.top()) { ll to1 = q3.top(), to2 = q4.top(); q3.pop();q4.pop(); q3.push(to2); q4.push(to1); } med2[i][0] = q3.top(); med2[i][1] = q4.top(); } for(int i = 0; i < ve.size() - 1; i++) { ll ans1, ans2, ans3, ans4; ans1 = ans2 = ans3 = ans4 = 0; for(int j = 0; j <= i; j++) { ans1 += abs(ve[j].S.F - med[j][0]); ans1 += abs(ve[j].S.S - med[j][0]); ans2 += abs(ve[j].S.F - med[j][1]); ans2 += abs(ve[j].S.S - med[j][1]); } for(int j = i + 1; j < ve.size(); j++) { ans3 += abs(ve[j].S.F - med2[j][0]); ans3 += abs(ve[j].S.S - med2[j][0]); ans4 += abs(ve[j].S.F - med2[j][1]); ans4 += abs(ve[j].S.S - med2[j][1]); } ans = min(ans, min(ans1, ans2) + min(ans3, ans4)); } ll ans1, ans2; ans1 = ans2 = 0; for(int i = 0; i < ve.size(); i++) { ans1 += abs(ve[i].S.F - med[ve.size() - 1][0]); ans1 += abs(ve[i].S.S - med[ve.size() - 1][0]); ans2 += abs(ve[i].S.F - med[ve.size() - 1][1]); ans2 += abs(ve[i].S.S - med[ve.size() - 1][1]); } ans = min(ans, min(ans1, ans2)); cout << ans + base << endl; } }

Compilation message (stderr)

bridge.cpp: In function 'int main()':
bridge.cpp:42:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for(int i = 0; i < ve.size(); i++)
      |                    ~~^~~~~~~~~~~
bridge.cpp:74:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |         for(int i = 0; i < ve.size(); i++)
      |                        ~~^~~~~~~~~~~
bridge.cpp:104:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |         for(int i = 0; i < ve.size() - 1; i++)
      |                        ~~^~~~~~~~~~~~~~~
bridge.cpp:115:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |             for(int j = i + 1; j < ve.size(); j++)
      |                                ~~^~~~~~~~~~~
bridge.cpp:128:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  128 |         for(int i = 0; i < ve.size(); i++)
      |                        ~~^~~~~~~~~~~
#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...