#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;
typedef pair <int,pii> piii;
#define forr(_a,_b,_c) for(int _a = (_b); _a <= int (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> int (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < int (_c); ++_a)
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
template<class X, class Y>
bool minz(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} return false;
}
template<class X, class Y>
bool maxz(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} return false;
}
const int N = 1e3 + 5;
const ll oo = (ll) 1e16;
const ll mod = 1e9 + 7; // 998244353;
int debug = 0;
int dp[N][N][2], n, t, u, uwu, f[N][N], g[N][N];
vector <int> a[N], ra[N];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef hqm
freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
#endif
cin >> n >> uwu;
forr (i, 1, n){
while (cin >> u && u != 0){
a[u].pb(i);
a[u + n].pb(i);
a[u].pb(i + n);
a[u + n].pb(i + n);
ra[i].pb(u);
ra[i + n].pb(u);
ra[i].pb(u + n);
ra[i + n].pb(u + n);
}
}
memset (dp, -1, sizeof dp);
forr (i, 1, 2 * n){
dp[i][i][0] = 0;
dp[i][i][1] = 0;
for (int u : a[i]){
if (u < i)
dp[u][i][0] = 1;
else dp[i][u][1] = 1;
}
}
int mx = 0, pos = 0;
forr (len, 1, n){
forr (i, 1, 2 * n - len + 1){
int u = i, v = i + len - 1;
forr (t, 0, 1){
int e = (t ? v : u);
if (dp[u][v][t] > mx){
mx = dp[u][v][t];
pos = e - n * (e > n);
}
if (dp[u][v][t] != -1){
for (int w : a[e]){
if (w < u){
maxz(dp[w][v][0], dp[u][v][t] + 1);
} else if (w > v){
maxz(dp[u][w][1], dp[u][v][t] + 1);
}
}
}
}
}
}
if (uwu){
forr (i, 1, 2 * n){
ford (j, i, 1){
g[i][j] = max (g[i][j + 1], dp[j][i][1]);
}
forr (j, i + 1, 2 * n)
g[i][j] = max (g[i][j - 1], dp[i][j][0]);
}
forr (i, 1, 2 * n){
forr (j, i, 2 * n){
for (int u : a[j])
if (u >= i && u < j){
maxz(f[i][j], f[i][u] + 1);
}
}
ford (j, i, 1){
for (int u : a[j])
if (u <= i && u > j){
maxz(f[i][j], f[i][u] + 1);
}
}
}
forr (i, 1, 2 * n){
int k = 0;
if (a[i].empty()) continue;
forr (j, i, 2 * n)
if (f[i][j]){
// cout << i << " " << j << " " << f[i][j] << " " << a[i][k] << " " << k << " " << a[i].size() << "\n";;
while (k < a[i].size() && a[i][k] <= j) k++;
if (k >= a[i].size() || a[i][k] <= j) break;
for (int u : ra[j]){
if (u < i && a[i][k] - u + 1 <= n){
// cout << i << " " << j << " " << a[i][k] << " " << u << " " << f[i][j] << " " << g[u][i - 1] << "\n";
if (maxz(mx, f[i][j] + g[u][i - 1] + 2))
pos = a[i][k];
}
if (u > a[i][k] && u - i + 1 <= n){
// cout << i << " " << j << " " << a[i][k] << " " << u << " " << f[i][j] << " " << g[u][a[i][k]] << "\n";
if (maxz(mx, f[i][j] + g[u][a[i][k] + 1] + 2))
pos = a[i][k];
}
}
}
}
forr (i, 1, 2 * n){
if (a[i].empty()) continue;
int k = a[i].size() - 1;
ford (j, i, 1)
if (f[i][j]){
// cout << i << " " << j << " " << f[i][j] << " " << a[i][k] << " " << k << " " << a[i].size() << "\n";;
while (k >= 0 && a[i][k] >= j) k--;
if (k < 0 || a[i][k] >= j) break;
for (int u : ra[j]){
if (u < a[i][k] && i - u + 1 <= n){
// cout << i << " " << j << " " << a[i][k] << " " << u << " " << f[i][j] << " " << g[u][a[i][k] - 1] << "\n";
if (maxz(mx, f[i][j] + g[u][a[i][k] - 1] + 2))
pos = a[i][k];
}
if (u > i && u - a[i][k] + 1 <= n){
//cout << i << " " << j << " " << a[i][k] << " " << u << " " << f[i][j] << " " << g[u][i + 1] << "\n";
if (maxz(mx, f[i][j] + g[u][i + 1] + 2))
pos = a[i][k];
}
}
}
}
}
cout << mx << " " << pos - n * (pos > n) << "\n";
return 0;
}
/*
*/
Compilation message
race.cpp: In function 'int main()':
race.cpp:137:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
137 | while (k < a[i].size() && a[i][k] <= j) k++;
| ~~^~~~~~~~~~~~~
race.cpp:138:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | if (k >= a[i].size() || a[i][k] <= j) break;
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8280 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
5 ms |
9052 KB |
Output is correct |
5 |
Correct |
4 ms |
8284 KB |
Output is correct |
6 |
Correct |
6 ms |
9308 KB |
Output is correct |
7 |
Correct |
6 ms |
8540 KB |
Output is correct |
8 |
Correct |
7 ms |
9564 KB |
Output is correct |
9 |
Correct |
7 ms |
8540 KB |
Output is correct |
10 |
Correct |
10 ms |
8716 KB |
Output is correct |
11 |
Correct |
10 ms |
8540 KB |
Output is correct |
12 |
Correct |
53 ms |
11844 KB |
Output is correct |
13 |
Correct |
97 ms |
13396 KB |
Output is correct |
14 |
Correct |
67 ms |
8892 KB |
Output is correct |
15 |
Incorrect |
559 ms |
17492 KB |
Output isn't correct |
16 |
Correct |
695 ms |
18260 KB |
Output is correct |
17 |
Incorrect |
553 ms |
17448 KB |
Output isn't correct |
18 |
Correct |
83 ms |
8796 KB |
Output is correct |
19 |
Correct |
830 ms |
18432 KB |
Output is correct |
20 |
Correct |
839 ms |
18400 KB |
Output is correct |