UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#212683#3828. Beam25390888ms6140kbC++112.7kb2024-10-20 09:32:172024-10-20 14:36:11

answer

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int T;
    cin >> T;
    
    
    int dx[4] = {-1, 1, 0, 0};
    int dy[4] = {0, 0, -1, 1};
    
    while(T--){
        int n, m;
        cin >> n >> m;
        int total = n * m;
        
        
        vector<int> grid(total);
        vector<int> pos(total);
        for(int x = 0; x < n; x++){
            for(int y = 0; y < m; y++){
                int val;
                cin >> val;
                int id = x * m + y;
                grid[id] = val;
                pos[val] = id;
            }
        }
        
        
        vector<int> dp(total, 1);
        vector<int> predecessor(total, -1);
        
        
        for(int val = 0; val < total; val++){
            int id = pos[val];
            int x = id / m;
            int y = id % m;
            
            for(int dir = 0; dir < 4; dir++){
                int nx = x + dx[dir];
                int ny = y + dy[dir];
                
                if(nx >=0 && nx < n && ny >=0 && ny < m){
                    int neighbor_id = nx * m + ny;
                    int neighbor_val = grid[neighbor_id];
                    
                    if(neighbor_val < val){
                        if(dp[neighbor_val] + 1 > dp[val]){
                            dp[val] = dp[neighbor_val] + 1;
                            predecessor[val] = neighbor_val;
                        }
                        else if(dp[neighbor_val] + 1 == dp[val]){
                            if(predecessor[val] == -1 || neighbor_val < predecessor[val]){
                                predecessor[val] = neighbor_val;
                            }
                        }
                    }
                }
            }
        }
        
        
        int max_len = 0;
        int end_val = 0;
        for(int val = 0; val < total; val++){
            if(dp[val] > max_len){
                max_len = dp[val];
                end_val = val;
            }
            else if(dp[val] == max_len && val < end_val){
                end_val = val;
            }
        }
        
        
        vector<int> path;
        int current = end_val;
        while(current != -1){
            path.push_back(current);
            current = predecessor[current];
        }
        reverse(path.begin(), path.end());
        
        
        cout << path.size() << "\n";
        for(int i = 0; i < path.size(); i++){
            if(i > 0) cout << " ";
            cout << path[i];
        }
        cout << "\n";
    }
}

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 1268kb

input:

10
3 3
0 1 2
3 4 5
6 7 8
3 3
0 1 2
3 4 5
6 8 7
3 3
0 1 2
3 4 5
7 6 8
3 3
0 1 2
3 4 5
7 8 6
3 3
0 1 2...

output:

5
0 1 2 5 8
6
0 1 2 5 7 8
5
0 1 4 6 7
6
0 1 2 5 6 8
5
0 1 2 5 7
7
0 1 2 5 6 7 8
5
0 1 2 6 8
6
0 1 2 ...

result:

wrong answer 17th numbers differ - expected: '2', found: '4'

Subtask #2:

score: 0
Wrong Answer

Test #3:

score: 0
Wrong Answer
time: 0ms
memory: 1300kb

input:

10
22 45
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33...

output:

990
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3...

result:

wrong answer 1003rd numbers differ - expected: '11', found: '114'

Subtask #3:

score: 0
Wrong Answer

Test #5:

score: 0
Wrong Answer
time: 282ms
memory: 6140kb

input:

10
2 100000
143604 106821 145034 44402 118718 156663 77133 28800 81890 12336 191537 118894 103331 75...

output:

13
11306 16007 49522 70570 76999 90088 97453 105217 116458 118241 145649 165150 168193
9
10999 12898...

result:

wrong answer 16th numbers differ - expected: '9688', found: '10999'

Subtask #4:

score: 0
Wrong Answer

Test #6:

score: 0
Wrong Answer
time: 306ms
memory: 6132kb

input:

10
1 200000
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32...

output:

200000
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3...

result:

wrong answer 200026th numbers differ - expected: '31', found: '32555'

Subtask #5:

score: 0
Wrong Answer

Test #7:

score: 0
Wrong Answer
time: 300ms
memory: 6140kb

input:

10
145 1379
140324 86968 96426 123781 39754 103720 60835 118904 114639 53717 27146 110309 39232 5608...

output:

14
8850 11113 25989 34151 36456 84869 93686 115053 144522 160408 175609 177211 186680 198340
14
7010...

result:

wrong answer 399960th numbers differ - expected: '2293', found: '9879'