Submission #805374

# Submission time Handle Problem Language Result Execution time Memory
805374 2023-08-03T15:54:25 Z nonono Palembang Bridges (APIO15_bridge) C++14
100 / 100
225 ms 16112 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int INF = 1e18;
const int mxN = 1e5 + 10;

int n, k;
char p[mxN], q[mxN];
int s[mxN], t[mxN];

namespace full {
    
    multiset<int> L, R;
    int sumL = 0, sumR = 0;
    int f1[mxN], f2[mxN];
    
    int calc() {
        int tmp = INF;
        
        if(L.size() > 0) {
            int l = *(-- L.end());
            tmp = min(tmp, l * (long long)L.size() - sumL + sumR - l * (long long)R.size());
        }
        
        if(R.size() > 0) {
            int r = *R.begin();
            tmp = min(tmp, r * (long long)L.size() - sumL + sumR - r * (long long)R.size());
        }
        
        return tmp;
    }
    
    void add(int x) {
        if(L.size() > 0 && *(-- L.end()) > x) {
            sumL += x;
            L.insert(x);
        } else {
            sumR += x;
            R.insert(x);
        }
    
        while(L.size() > R.size() + 1) {
            sumR += *(-- L.end());
            R.insert(*(-- L.end()));
            
            sumL -= *(-- L.end());
            L.erase(-- L.end());
        }
        
        while(R.size() > L.size()) {
            sumL += *R.begin();
            L.insert(*R.begin());
            
            sumR -= *R.begin();
            R.erase(R.begin());
        }
    }
    
    void solve() {
        
        int sum = 0;
        vector<int> ar;
        
        for(int i = 1; i <= n; i ++) {
            if(p[i] == q[i]) sum += abs(s[i] - t[i]);
            else {
                ar.push_back(i);
            }
        }
        
        sort(ar.begin(), ar.end(), [&](const int &x, const int &y) {
            return s[x] + t[x] < s[y] + t[y]; 
        });
        
        for(int i = 0; i < ar.size(); i ++) {
            int id = ar[i];
            add(s[id]);
            add(t[id]);
            f1[i] = calc();
        }
    
        L.clear(); R.clear();
        sumL = 0; sumR = 0;
        
        for(int i = ar.size() - 1; i >= 0; i --) {
            int id = ar[i];
            add(s[id]);
            add(t[id]);
            f2[i] = calc();
        }
        
        int result = INF;
        
        if(k >= 1) result = min({result, f1[ar.size() - 1], f2[0]});
        
        if(k >= 2) {
            for(int i = 0; i + 1 < ar.size(); i ++) {
                result = min(result, f1[i] + f2[i + 1]);
            }
        }
        
        cout << sum + result + ar.size() << "\n";
    }
}

signed main() {
    
    cin.tie(0)->sync_with_stdio(0);
    
    cin >> k >> n;
    
    for(int i = 1; i <= n; i ++) cin >> p[i] >> s[i] >> q[i] >> t[i];
    
    full::solve();
    return 0;
}

Compilation message

bridge.cpp: In function 'void full::solve()':
bridge.cpp:76:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |         for(int i = 0; i < ar.size(); i ++) {
      |                        ~~^~~~~~~~~~~
bridge.cpp:98:34: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |             for(int i = 0; i + 1 < ar.size(); i ++) {
      |                            ~~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 500 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 472 KB Output is correct
9 Correct 1 ms 472 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 476 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 104 ms 14604 KB Output is correct
13 Correct 187 ms 16092 KB Output is correct
14 Correct 159 ms 13616 KB Output is correct
15 Correct 84 ms 9576 KB Output is correct
16 Correct 89 ms 15464 KB Output is correct
17 Correct 98 ms 16112 KB Output is correct
18 Correct 97 ms 15676 KB Output is correct
19 Correct 120 ms 16092 KB Output is correct
20 Correct 123 ms 15636 KB Output is correct
21 Correct 106 ms 15872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 1 ms 476 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 480 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 480 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 480 KB Output is correct
21 Correct 2 ms 500 KB Output is correct
22 Correct 1 ms 472 KB Output is correct
23 Correct 1 ms 476 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 119 ms 14536 KB Output is correct
26 Correct 202 ms 14664 KB Output is correct
27 Correct 225 ms 15540 KB Output is correct
28 Correct 218 ms 16100 KB Output is correct
29 Correct 206 ms 16060 KB Output is correct
30 Correct 73 ms 8644 KB Output is correct
31 Correct 91 ms 15368 KB Output is correct
32 Correct 98 ms 16076 KB Output is correct
33 Correct 81 ms 15696 KB Output is correct
34 Correct 113 ms 16064 KB Output is correct
35 Correct 107 ms 15596 KB Output is correct
36 Correct 101 ms 15844 KB Output is correct
37 Correct 95 ms 14576 KB Output is correct