QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#454791#8768. Arrested Developmentscau_ly#TL 10ms82052kbC++143.6kb2024-06-25 14:02:502024-06-25 14:02:50

Judging History

你现在查看的是最新测评结果

  • [2024-06-25 14:02:50]
  • 评测
  • 测评结果:TL
  • 用时:10ms
  • 内存:82052kb
  • [2024-06-25 14:02:50]
  • 提交

answer

#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100005;
const int MOD = 998244353;
const int MAX_VAL = N * 100;

int n;
struct Task {
    int a, b;
};

int dp[2][2][50 * N];
struct DpState {
    int i, j, k;
    DpState(int _i, int _j, int _k) {
        i = _i;
        j = _j;
        k = _k;
    }
};
queue<DpState> que;

int main() {
    memset(dp, MAX_VAL, sizeof(dp));
    while (cin >> n) {
        Task task[100];
        for (int i = 1; i <= n; i++) {
            scanf("%d%d", &task[i].a, &task[i].b);
        }

        int ans = MAX_VAL;
        que.push(DpState(0, 0, 0));
        dp[0][0][0] = 0;
        while (!que.empty()) {
            DpState state = que.front();
            que.pop();
            int i = state.i, j = state.j, k = state.k;
            if (i == n) {
                ans = min(ans, dp[i & 1][j][k]);
                continue;
            }
            if (dp[i & 1][j][k] == MAX_VAL) continue;
            if (j == 0) {
                // use a
                {
                    int _i = i + 1, _j = j, _k = k + task[i + 1].a;
                    int state = (_i << 25) | (_j << 23) | _k;
                    if (dp[_i & 1][_j][_k] > dp[i & 1][j][k] + task[i + 1].a) {
                        dp[_i & 1][_j][_k] = dp[i & 1][j][k] + task[i + 1].a;
                        que.push(DpState(_i, _j, _k));
                    }
                }

                if (task[i + 1].b <= k) {
                    int _i = i + 1, _j = j, _k = k - task[i + 1].b;
                    int state = (_i << 25) | (_j << 23) | _k;
                    if (dp[_i & 1][_j][_k] > dp[i & 1][j][k]) {
                        dp[_i & 1][_j][_k] = dp[i & 1][j][k];
                        que.push(DpState(_i, _j, _k));
                    }
                } else {
                    int _i = i + 1, _j = j ^ 1, _k = task[i + 1].b - k;
                    int state = (_i << 25) | (_j << 23) | _k;
                    if (dp[_i & 1][_j][_k] > dp[i & 1][j][k] + _k) {
                        dp[_i & 1][_j][_k] = dp[i & 1][j][k] + _k;
                        que.push(DpState(_i, _j, _k));
                    }
                }
            } else {
                // use b
                {
                    int _i = i + 1, _j = j, _k = k + task[i + 1].b;
                    int state = (_i << 25) | (_j << 23) | _k;
                    if (dp[_i & 1][_j][_k] > dp[i & 1][j][k] + task[i + 1].b) {
                        dp[_i & 1][_j][_k] = dp[i & 1][j][k] + task[i + 1].b;
                        que.push(DpState(_i, _j, _k));
                    }
                }

                if (task[i + 1].a <= k) {
                    int _i = i + 1, _j = j, _k = k - task[i + 1].a;
                    int state = (_i << 25) | (_j << 23) | _k;
                    if (dp[_i & 1][_j][_k] > dp[i & 1][j][k]) {
                        dp[_i & 1][_j][_k] = dp[i & 1][j][k];
                        que.push(DpState(_i, _j, _k));
                    }
                } else {
                    int _i = i + 1, _j = j ^ 1, _k = task[i + 1].a - k;
                    int state = (_i << 25) | (_j << 23) | _k;
                    if (dp[_i & 1][_j][_k] > dp[i & 1][j][k] + _k) {
                        dp[_i & 1][_j][_k] = dp[i & 1][j][k] + _k;
                        que.push(DpState(_i, _j, _k));
                    }
                }
            }
            dp[i & 1][j][k] = MAX_VAL;
        }

        cout << ans << endl;
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 82052kb

input:

4
100 1
1 90
1 20
1 20

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 8ms
memory: 81800kb

input:

2
314 1
592 6

output:

7

result:

ok single line: '7'

Test #3:

score: 0
Accepted
time: 0ms
memory: 81820kb

input:

1
1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 0ms
memory: 82024kb

input:

1
100000 1

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 10ms
memory: 81744kb

input:

1
1 100000

output:

1

result:

ok single line: '1'

Test #6:

score: 0
Accepted
time: 3ms
memory: 82052kb

input:

1
100000 100000

output:

100000

result:

ok single line: '100000'

Test #7:

score: -100
Time Limit Exceeded

input:

50
78681 95291
22639 1538
12119 52253
50430 63757
66133 92826
61048 40069
33506 30382
96049 50134
42895 62735
86943 16955
9667 61843
49647 9320
29082 16909
69601 68436
19892 34306
29822 79462
73262 14568
1693 35040
89757 61888
56993 48750
89611 77773
54159 21067
32520 41091
52501 92770
36530 17589
5...

output:


result: