-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP116.cpp
152 lines (142 loc) · 3.54 KB
/
P116.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <iostream>
#include <stack>
int main() {
// Read input:
int m, n;
int matrix[1000] = {0};
int scores[1000] = {0};
int back[1000] = {0};
int lexScores[1000];
while(std::cin >> m >> n) {
for(int i = 0; i < m*n; ++i)
lexScores[i] = -1;
int v;
for(int i = 0; i < m*n; ++i) {
std::cin >> v;
matrix[i] = v;
}
/*
std::cerr << "MxN " << m << " x " << n << ":" << std::endl;
for(int y = 0; y < m; ++y) {
for(int x = 0; x < n; ++x) {
std::cerr << matrix[y*n+x] << " ";
}
std::cerr << std::endl;
}//*/
// Build scores:
// First row:
for(int i = 0; i < m; ++i) {
scores[i*n] = matrix[i*n];
lexScores[i*n] = i;
}
// subsequent rows:
for(int x = 1; x < n; ++x) {
// build active:
for(int y = 0; y < m; ++y) {
int self = matrix[x+y*n];
int prevY = (y+m-1)%m; // up
int best = self + scores[prevY*n-1+x];
int bestBack = prevY;
for(int i = 0; i <= 1; ++i) {
prevY = (y+m+i)%m;
int candidate = self + scores[prevY*n-1+x];
if(candidate < best) {
best = candidate;
bestBack = prevY;
}
else if(candidate == best) {
// If lex score is better, choose it!
int bestLex = lexScores[bestBack*n-1+x];
int candidateLex = lexScores[prevY*n-1+x];
if(bestLex > candidateLex) {
best = candidate;
bestBack = prevY;
}
}
}
scores[y*n+x] = best;
back[y*n+x] = bestBack;
}
// Update lexScores:
int lexScore = 0;
for(int i = 0; i < m; ++i) {
// Find repvious row with score i:
int prevYWithScore = 0;
for(; prevYWithScore < m; ++prevYWithScore) {
if(lexScores[prevYWithScore*n+x-1] == i) {
break;
}
}
// check current row for users:
int initPos;
if(prevYWithScore == 0)
initPos = 1;
else if(prevYWithScore == m-1)
initPos = 2;
else
initPos = 0;
for(int j = 0; j < 3; ++j) {
int tri = ((j + initPos + 3) % 3)-1;
int k = (tri+m+prevYWithScore)%m;
int index = k*n+x;
if(lexScores[index] == -1 && back[index] == prevYWithScore) {
//std::cerr << "setting for x=" << x << ", prev y=" << prevYWithScore << "-> y=" << k << " score=" << lexScore << "... j=" << j << ", k=" << k << ", tri=" << tri << std::endl;
lexScores[index] = lexScore++;
}
}
if(lexScore == m)
break;
}
}
// debug info:
/*
std::cerr << "scores:" << std::endl;
for(int y = 0; y < m; ++y) {
for(int x = 0; x < n; ++x) {
std::cerr << scores[y*n+x] << " ";
}
std::cerr << std::endl;
}
std::cerr << "lexScores:" << std::endl;
for(int y = 0; y < m; ++y) {
for(int x = 0; x < n; ++x) {
std::cerr << lexScores[y*n+x] << " ";
}
std::cerr << std::endl;
}
std::cerr << "back:" << std::endl;
for(int y = 0; y < m; ++y) {
for(int x = 0; x < n; ++x) {
std::cerr << back[y*n+x] << " ";
}
std::cerr << std::endl;
}//*/
// Write output:
// find best in last row:
int row = 0;
for(int i = 1; i < m; ++i) {
if(scores[i*n+n-1] < scores[row*n+n-1])
row = i;
else if(scores[i*n+n-1] == scores[row*n+n-1]) {
if(lexScores[i*n+n-1] < lexScores[row*n+n-1]) {
row = i;
}
}
}
int score = scores[row*n+n-1];
std::stack<int> stack;
for(int i = n-1; i >= 0; --i) {
stack.push(row);
row = back[row*n+i];
}
std::cout << (stack.top()+1);
stack.pop();
while(!stack.empty()) {
std::cout << " " << (stack.top()+1);
stack.pop();
}
std::cout << std::endl << score << std::endl;
}
return 0;
}