답안 #1116378

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1116378 2024-11-21T14:40:05 Z gdragon Palembang Bridges (APIO15_bridge) C++17
22 / 100
33 ms 4312 KB
#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const long long INF = (long long)1e18 + 7;
const int mod = 1e9 + 7;
pair<int, int> b[N], a[N];
int n, m, k;
long long ans = 0;
void read() {
    cin >> k >> n;
    m = 0;
    for(int i = 1; i <= n; i++) {
        char c1, c2;
        cin >> c1 >> b[i].fi >> c2 >> b[i].se;
        if (c1 == c2) ans += abs(b[i].fi - b[i].se);
        else {
            if (c1 == 'B') swap(b[i].fi, b[i].se);
            a[++m] = b[i];
        }
    }
    n = m;
}
void sub1() {
    vector<int> val;
    val.push_back(-1);
    for(int i = 1; i <= n; i++) {
        val.push_back(a[i].fi);
        val.push_back(a[i].se);
    }
    sort(ALL(val));
    vector<long long> f(SZ(val) + 1, 0);
    f[0] = 0;
    for(int i = 1; i < SZ(val); i++) {
        f[i] = f[i - 1] + val[i];
    }
    int sz = SZ(val) - 1;
    long long tmp = INF;
    for(int i = 1; i <= sz; i++) {
        long long lef = 1LL * val[i] * i - f[i];
        long long rig = f[sz] - f[i - 1] - 1LL * val[i] * (sz - i + 1);
        minimize(tmp, lef + rig + n);
        // long long tmp1 = 0;
        // for(int j = 1; j < i; j++) tmp1 += abs(val[i] - val[j]);
        // for(int j = i + 1; j < SZ(val); j++) tmp1 += abs(val[i] - val[j]);
        // minimize(tmp, tmp1 + n);
        // cerr << val[i] << ' ' << lef << ' ' << rig << ' ' << lef + rig + n + ans << endl;
    }
    if (tmp != INF) ans += tmp;
    cout << ans;
}
void solve() {
    if (k == 1) sub1();
    else assert(false);
}
signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
    int test = 1;
    // cin >> test;
    while(test--) {
        read();
        solve();
    }
    return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 476 KB Output is correct
8 Correct 1 ms 504 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 19 ms 4312 KB Output is correct
13 Correct 33 ms 4300 KB Output is correct
14 Correct 25 ms 4212 KB Output is correct
15 Correct 20 ms 2760 KB Output is correct
16 Correct 24 ms 4292 KB Output is correct
17 Correct 25 ms 4300 KB Output is correct
18 Correct 31 ms 4292 KB Output is correct
19 Correct 33 ms 4300 KB Output is correct
20 Correct 21 ms 4300 KB Output is correct
21 Correct 30 ms 4292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 592 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 592 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 592 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -