제출 #1349664

#제출 시각아이디문제언어결과실행 시간메모리
1349664retardeMarathon Race 2 (JOI24_ho_t3)C++20
100 / 100
158 ms127320 KiB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define int long long
#define all(x) (x).begin(), (x).end()

typedef long double ld;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef map<int, int> mii;
typedef unordered_map<int, int> umii;

const int mod = 1e9 + 7;
const int inf = (int)4e18;
const bool tc = false;

const int mxn = 2005;

int dp[mxn][mxn][2][2];
int x[mxn], cnt[mxn], ps[mxn];
int prefL[mxn], sufL[mxn], prefR[mxn], sufR[mxn];

inline void solve() {
    int N, L;
    cin >> N >> L;

    vi tmp(N);
    for (int i = 0; i < N; i++) cin >> tmp[i];
    sort(all(tmp));

    int n = 0;
    for (int v : tmp) {
        if (n == 0 || x[n] != v) {
            x[++n] = v;
            cnt[n] = 1;
        } else {
            cnt[n]++;
        }
    }

    int q;
    cin >> q;

    if (n > 2000) {
        while (q--) {
            int s, e, t;
            cin >> s >> e >> t;
            cout << "No\n";
        }
        return;
    }

    ps[0] = 0;
    for (int i = 1; i <= n; i++) ps[i] = ps[i - 1] + cnt[i];

    for (int i = 0; i <= n + 1; i++) {
        for (int j = 0; j <= n + 1; j++) {
            for (int k = 0; k < 2; k++) {
                for (int t = 0; t < 2; t++) {
                    dp[i][j][k][t] = inf;
                }
            }
        }
    }

    dp[1][n][0][0] = 0;
    dp[1][n][1][1] = 0;
    dp[1][n][0][1] = x[n] - x[1];
    dp[1][n][1][0] = x[n] - x[1];

    for (int len = n; len >= 2; len--) {
        for (int l = 1; l + len - 1 <= n; l++) {
            int r = l + len - 1;

            int multL = N - (ps[r] - ps[l]) + 1;
            int multR = N - (ps[r - 1] - ps[l - 1]) + 1;

            for (int t = 0; t < 2; t++) {
                dp[l + 1][r][0][t] = min(dp[l + 1][r][0][t],
                    dp[l][r][0][t] + multL * (x[l + 1] - x[l]));
                dp[l + 1][r][1][t] = min(dp[l + 1][r][1][t],
                    dp[l][r][0][t] + multL * (x[r] - x[l]));

                dp[l][r - 1][0][t] = min(dp[l][r - 1][0][t],
                    dp[l][r][1][t] + multR * (x[r] - x[l]));
                dp[l][r - 1][1][t] = min(dp[l][r - 1][1][t],
                    dp[l][r][1][t] + multR * (x[r] - x[r - 1]));
            }
        }
    }

    prefL[0] = inf;
    prefR[0] = inf;
    for (int i = 1; i <= n; i++) {
        int bestL = min(dp[i][i][0][0], dp[i][i][1][0]);
        int bestR = min(dp[i][i][0][1], dp[i][i][1][1]);

        prefL[i] = min(prefL[i - 1], bestL - (N + 1) * x[i]);
        prefR[i] = min(prefR[i - 1], bestR - (N + 1) * x[i]);
    }

    sufL[n + 1] = inf;
    sufR[n + 1] = inf;
    for (int i = n; i >= 1; i--) {
        int bestL = min(dp[i][i][0][0], dp[i][i][1][0]);
        int bestR = min(dp[i][i][0][1], dp[i][i][1][1]);

        sufL[i] = min(sufL[i + 1], bestL + (N + 1) * x[i]);
        sufR[i] = min(sufR[i + 1], bestR + (N + 1) * x[i]);
    }

    while (q--) {
        int s, e, t;
        cin >> s >> e >> t;

        int pos = upper_bound(x + 1, x + n + 1, e) - x - 1;

        int ansL = inf, ansR = inf;

        if (pos >= 1) {
            ansL = min(ansL, prefL[pos] + (N + 1) * e);
            ansR = min(ansR, prefR[pos] + (N + 1) * e);
        }
        if (pos + 1 <= n) {
            ansL = min(ansL, sufL[pos + 1] - (N + 1) * e);
            ansR = min(ansR, sufR[pos + 1] - (N + 1) * e);
        }

        ansL += abs(s - x[1]);
        ansR += abs(s - x[n]);

        int ans = min(ansL, ansR) + N;
        cout << (ans <= t ? "Yes\n" : "No\n");
    }
}

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

signed main() {
    ios::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);

    int t = 1;
    if (tc) cin >> t;
    while (t--) solve();
}

컴파일 시 표준 에러 (stderr) 메시지

Main.cpp: In function 'void setIO(std::string)':
Main.cpp:154:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  154 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:155:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  155 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...