답안 #647119

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
647119 2022-10-01T15:59:42 Z messiuuuuu 유괴 2 (JOI17_abduction2) C++14
100 / 100
2210 ms 131400 KB
///
#include<bits/stdc++.h>
#define task "C"
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 5e4 + 5;
const ll INF = 1e9 + 5;

int m, n, q;
int a[MAXN], b[MAXN];

void Input()
{
    cin >> m >> n >> q;
    for (int i = 1; i <= m; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++)
        cin >> b[i];
    a[0] = a[m + 1] = b[0] = b[n + 1] = INF;
}

map<int, ll> dp[MAXN][2];

int FindS(int r, int c, bool t)
{
    if (t == 0)
    {
        int i = r - 1;
        for (; a[i] < b[c]; i--);
        return i;
    } else
    {
        int i = c - 1;
        for (; b[i] < a[r]; i--);
        return i;
    }
}

int FindL(int r, int c, bool t)
{
    if (t == 0)
    {
        int i = r + 1;
        for (; a[i] < b[c]; i++);
        return i;
    } else
    {
        int i = c + 1;
        for (; b[i] < a[r]; i++);
        return i;
    }
}

ll DP(int r, int c, bool t)
{
    if (r < 1 || r > m || c < 1 || c > n)
        return -1;
    if (dp[r][t].count(c))
        return dp[r][t][c];
    ll& res = dp[r][t][c];

    int tmp = t == 0 ? r : c;

    int p1 = FindS(r, c, t);
    int d1 = tmp - p1;

    int p2 = FindL(r, c, t);
    int d2 = p2 - tmp;

    res = max(d1 + (t == 0 ? DP(p1, c, !t) : DP(r, p1, !t)), d2 + (t == 0 ? DP(p2, c, !t) : DP(r, p2, !t)));
    return res;
}

void Solve()
{
    while (q-- > 0)
    {
        int s, t;
        cin >> s >> t;
        cout << max(DP(s, t, 0), DP(s, t, 1)) << '\n';
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(task".INP","r"))
    {
        freopen(task".INP","r",stdin);
        //freopen(task".OUT","w",stdout);
    }
    Input();
    Solve();
}

Compilation message

abduction2.cpp: In function 'int main()':
abduction2.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         freopen(task".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4984 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4984 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 5 ms 5204 KB Output is correct
20 Correct 5 ms 5332 KB Output is correct
21 Correct 5 ms 5204 KB Output is correct
22 Correct 7 ms 5460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4984 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 5 ms 5204 KB Output is correct
20 Correct 5 ms 5332 KB Output is correct
21 Correct 5 ms 5204 KB Output is correct
22 Correct 7 ms 5460 KB Output is correct
23 Correct 13 ms 5332 KB Output is correct
24 Correct 12 ms 5376 KB Output is correct
25 Correct 12 ms 5332 KB Output is correct
26 Correct 11 ms 5360 KB Output is correct
27 Correct 14 ms 5412 KB Output is correct
28 Correct 417 ms 10448 KB Output is correct
29 Correct 90 ms 6048 KB Output is correct
30 Correct 1043 ms 11552 KB Output is correct
31 Correct 1202 ms 13608 KB Output is correct
32 Correct 82 ms 5708 KB Output is correct
33 Correct 405 ms 7316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5204 KB Output is correct
2 Correct 4 ms 5204 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 4 ms 5204 KB Output is correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 5 ms 5204 KB Output is correct
7 Correct 5 ms 5204 KB Output is correct
8 Correct 7 ms 5460 KB Output is correct
9 Correct 7 ms 5460 KB Output is correct
10 Correct 6 ms 5460 KB Output is correct
11 Correct 8 ms 5508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4984 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 5 ms 5204 KB Output is correct
20 Correct 5 ms 5332 KB Output is correct
21 Correct 5 ms 5204 KB Output is correct
22 Correct 7 ms 5460 KB Output is correct
23 Correct 13 ms 5332 KB Output is correct
24 Correct 12 ms 5376 KB Output is correct
25 Correct 12 ms 5332 KB Output is correct
26 Correct 11 ms 5360 KB Output is correct
27 Correct 14 ms 5412 KB Output is correct
28 Correct 417 ms 10448 KB Output is correct
29 Correct 90 ms 6048 KB Output is correct
30 Correct 1043 ms 11552 KB Output is correct
31 Correct 1202 ms 13608 KB Output is correct
32 Correct 82 ms 5708 KB Output is correct
33 Correct 405 ms 7316 KB Output is correct
34 Correct 4 ms 5204 KB Output is correct
35 Correct 4 ms 5204 KB Output is correct
36 Correct 4 ms 5204 KB Output is correct
37 Correct 4 ms 5204 KB Output is correct
38 Correct 4 ms 5204 KB Output is correct
39 Correct 5 ms 5204 KB Output is correct
40 Correct 5 ms 5204 KB Output is correct
41 Correct 7 ms 5460 KB Output is correct
42 Correct 7 ms 5460 KB Output is correct
43 Correct 6 ms 5460 KB Output is correct
44 Correct 8 ms 5508 KB Output is correct
45 Correct 18 ms 5684 KB Output is correct
46 Correct 17 ms 5648 KB Output is correct
47 Correct 17 ms 5588 KB Output is correct
48 Correct 19 ms 5572 KB Output is correct
49 Correct 18 ms 5560 KB Output is correct
50 Correct 503 ms 9992 KB Output is correct
51 Correct 472 ms 10544 KB Output is correct
52 Correct 1365 ms 15692 KB Output is correct
53 Correct 1327 ms 15368 KB Output is correct
54 Correct 1343 ms 14964 KB Output is correct
55 Correct 1853 ms 20132 KB Output is correct
56 Correct 2210 ms 131400 KB Output is correct
57 Correct 786 ms 38684 KB Output is correct
58 Correct 742 ms 37052 KB Output is correct
59 Correct 702 ms 36812 KB Output is correct