UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#212679#3828. Beam253900ms1268kbC++112.8kb2024-10-20 09:28:342024-10-20 14:35:53

answer

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

struct Cell {
    int value;
    int x, y;
    int index;
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int T;
    cin >> T;
    while(T--){
        int n, m;
        cin >> n >> m;
        
        vector<Cell> cells(n * m);
        for(int i=0; i<n; ++i){
            for(int j=0; j<m; ++j){
                int val;
                cin >> val;
                int idx = i * m + j;
                cells[idx] = Cell{val, i, j, idx};
            }
        }
        
        sort(cells.begin(), cells.end(), [](const Cell &a, const Cell &b) -> bool{
            return a.value < b.value;
        });
        
        vector<int> length(n * m, 1);
        vector<int> predecessor(n * m, -1);
        
        vector<pair<int, int>> directions = { {-1,0}, {1,0}, {0,-1}, {0,1} };
        
        for(auto &cell : cells){
            int curr_idx = cell.index;
            for(auto &dir : directions){
                int dx = dir.first;
                int dy = dir.second;
                int nx = cell.x + dx;
                int ny = cell.y + dy;
                if(nx >=0 && nx < n && ny >=0 && ny < m){
                    int neighbor_idx = nx * m + ny;
                    if(cells[neighbor_idx].value < cell.value){
                        if(length[neighbor_idx] + 1 > length[curr_idx]){
                            length[curr_idx] = length[neighbor_idx] +1;
                            predecessor[curr_idx] = neighbor_idx;
                        }
                        else if(length[neighbor_idx] +1 == length[curr_idx]){
                            if(predecessor[curr_idx] == -1 || cells[neighbor_idx].value < cells[predecessor[curr_idx]].value){
                                predecessor[curr_idx] = neighbor_idx;
                            }
                        }
                    }
                }
            }
        }
        
        int max_len = 0;
        int end_idx = -1;
        for(int i=0; i<n*m; ++i){
            if(length[i] > max_len){
                max_len = length[i];
                end_idx = i;
            }
            else if(length[i] == max_len){
                if(cells[i].value < cells[end_idx].value){
                    end_idx = i;
                }
            }
        }
        
        vector<int> path;
        int current = end_idx;
        while(current != -1){
            path.push_back(cells[current].value);
            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";
    }
}

详细

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

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
5
0 1 2 5 8
5
0 1 2 5 8
5
0 1 2 5 8
5
0 1 4 7 6
5
0 1 4 7 6
5
0 1 2 5 8
5
0 1 2 5 8
5
0 ...

result:

wrong answer 7th numbers differ - expected: '6', found: '5'

Subtask #2:

score: 0
Memory Limit Exceeded

Test #3:

score: 0
Memory Limit Exceeded

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:


result:


Subtask #3:

score: 0
Memory Limit Exceeded

Test #5:

score: 0
Memory Limit Exceeded

input:

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

output:


result:


Subtask #4:

score: 0
Memory Limit Exceeded

Test #6:

score: 0
Memory Limit Exceeded

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:


Subtask #5:

score: 0
Memory Limit Exceeded

Test #7:

score: 0
Memory Limit Exceeded

input:

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

output:


result: