Submission #372408

# Submission time Handle Problem Language Result Execution time Memory
372408 2021-02-28T03:49:34 Z gozonite Palembang Bridges (APIO15_bridge) C++14
100 / 100
472 ms 15236 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pi;
#define MOD 1000000007LL

struct Data {
    ll fi, si;
};
bool cmp(const Data& a, const Data& b) { return a.fi+a.si < b.fi+b.si; }

int K, N;
vector<Data> info;
int n;
multiset<ll> fl, fh, sl, sh;
ll fls = 0, fhs = 0, sls = 0, shs = 0;

void trans(ll x) {
    if (sl.find(x) != sl.end()) {
        sl.erase(sl.find(x)); sls -= x;
        if (sl.size()+1 < sh.size()) { auto it = sh.begin(); sls += *it; shs -= *it; sl.insert(*it); sh.erase(it); }
    } else if (sh.find(x) != sh.end()) {
        sh.erase(sh.find(x)); shs -= x;
        if (sh.size() < sl.size()) { auto it = sl.end(); it--; shs += *it; sls -= *it; sh.insert(*it); sl.erase(it); }
    }
    if (x >= *fh.begin()) {
        fh.insert(x); fhs += x;
        if (fl.size()+1 < fh.size()) { auto it = fh.begin(); fls += *it; fhs -= *it; fl.insert(*it); fh.erase(it); }
    } else {
        fl.insert(x); fls += x;
        if (fh.size() < fl.size()) { auto it = fl.end(); it--; fhs += *it; fls -= *it; fh.insert(*it); fl.erase(it); }
    }
}

int main() {
    //freopen("feast.in", "r", stdin);
    //freopen("feast.out", "w", stdout);
    //ios_base::sync_with_stdio(false);
    //cin.tie(0);
    
    cin >> K >> N;
    
    ll pre = 0;
    for (int i = 0; i < N; i++) {
        char fc, sc; int fi, si;
        cin >> fc >> fi >> sc >> si;
        if (fc == sc) pre += abs(fi-si);
        else info.push_back({fi, si});
    }
    n = info.size();
    //cout << "pre: " << pre << endl;
    //cout << "info: ";
    //for (auto pp : info) cout << pp.fi << " " << pp.si << " | "; cout << endl;
    
    if (info.size() == 0) {
        cout << pre << endl;
        return 0;
    }
    
    if (K == 1) {
        vector<ll> v;
        for (auto pp : info) {
            v.push_back(pp.fi);
            v.push_back(pp.si);
        }
        sort(v.begin(), v.end());
        ll ans = info.size();
        for (int i = 0; i < v.size(); i++) ans += abs(v[i]-v[v.size()/2]);
        cout << ans+pre << endl;
        
        //cout << "Debugging v: " << v[v.size()/2] << endl;
        //for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << endl;
    } else if (K == 2) {
        sort(info.begin(), info.end(), cmp);
        
        vector<ll> v;
        for (auto pp : info) {
            v.push_back(pp.fi);
            v.push_back(pp.si);
        }
        sort(v.begin(), v.end());
        for (int i = 0; i < v.size()/2; i++) { sl.insert(v[i]); sls += v[i]; }
        for (int i = v.size()/2; i < v.size(); i++) { sh.insert(v[i]); shs += v[i]; }
        
        ll ans = 1e18;
        for (int i = 0; i < info.size()-1; i++) {
            trans(info[i].fi); trans(info[i].si);
            ll med = *fh.begin(), fs = ((ll)(fl.size())*med - fls) + (fhs - ((ll)fh.size())*med);
            med = *sh.begin(); ll ss = ((ll)(sl.size())*med - sls) + (shs - ((ll)sh.size())*med);
            ans = min(ans, fs+ss);
            
            //cout << "Debugging: " << i << " " << fs << " " << ss << endl;
            //for (auto pp : fl) cout << pp << " "; cout << ": " << fls << endl;
            //for (auto pp : fh) cout << pp << " "; cout << ": " << fhs << endl;
            //for (auto pp : sl) cout << pp << " "; cout << ": " << sls << endl;
            //for (auto pp : sh) cout << pp << " "; cout << ": " << shs << endl;
        }
        
        ans += (ll)info.size();
        cout << ans+pre << endl;
    }
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:71:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |         for (int i = 0; i < v.size(); i++) ans += abs(v[i]-v[v.size()/2]);
      |                         ~~^~~~~~~~~~
bridge.cpp:85:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         for (int i = 0; i < v.size()/2; i++) { sl.insert(v[i]); sls += v[i]; }
      |                         ~~^~~~~~~~~~~~
bridge.cpp:86:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |         for (int i = v.size()/2; i < v.size(); i++) { sh.insert(v[i]); shs += v[i]; }
      |                                  ~~^~~~~~~~~~
bridge.cpp:89:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |         for (int i = 0; i < info.size()-1; i++) {
      |                         ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 2 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 2 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 57 ms 4956 KB Output is correct
13 Correct 118 ms 5000 KB Output is correct
14 Correct 76 ms 4828 KB Output is correct
15 Correct 68 ms 2784 KB Output is correct
16 Correct 84 ms 4956 KB Output is correct
17 Correct 107 ms 4956 KB Output is correct
18 Correct 102 ms 4956 KB Output is correct
19 Correct 116 ms 4956 KB Output is correct
20 Correct 92 ms 4956 KB Output is correct
21 Correct 114 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 3 ms 492 KB Output is correct
15 Correct 3 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 2 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 2 ms 492 KB Output is correct
20 Correct 3 ms 492 KB Output is correct
21 Correct 3 ms 492 KB Output is correct
22 Correct 2 ms 492 KB Output is correct
23 Correct 2 ms 620 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
15 Correct 3 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 2 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 2 ms 492 KB Output is correct
20 Correct 2 ms 492 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 2 ms 492 KB Output is correct
23 Correct 2 ms 492 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
25 Correct 207 ms 13764 KB Output is correct
26 Correct 304 ms 13940 KB Output is correct
27 Correct 472 ms 14744 KB Output is correct
28 Correct 455 ms 15200 KB Output is correct
29 Correct 463 ms 15236 KB Output is correct
30 Correct 198 ms 8292 KB Output is correct
31 Correct 233 ms 14688 KB Output is correct
32 Correct 250 ms 15200 KB Output is correct
33 Correct 258 ms 14816 KB Output is correct
34 Correct 264 ms 15200 KB Output is correct
35 Correct 241 ms 14688 KB Output is correct
36 Correct 247 ms 15072 KB Output is correct
37 Correct 198 ms 13664 KB Output is correct