#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, -63, 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;
}
}
memset (f, -63, sizeof f);
memset (g, -63, sizeof g);
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){
f[i][i] = 0;
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){
sort(all(a[i]));
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){
// if (f[i][j] + g[u][a[i][k] - 1] + 2 == 44)
// 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){
// if (f[i][j] + g[u][i + 1] + 2 == 44)
// 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 - (pos > n) * n << "\n";;
return 0;
}
/*
*/
Compilation message
race.cpp: In function 'int main()':
race.cpp:142:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | while (k < a[i].size() && a[i][k] <= j) k++;
| ~~^~~~~~~~~~~~~
race.cpp:143:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
143 | if (k >= a[i].size() || a[i][k] <= j) break;
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16220 KB |
Output is correct |
2 |
Correct |
6 ms |
16180 KB |
Output is correct |
3 |
Correct |
6 ms |
16220 KB |
Output is correct |
4 |
Correct |
7 ms |
16220 KB |
Output is correct |
5 |
Correct |
7 ms |
16372 KB |
Output is correct |
6 |
Correct |
8 ms |
16320 KB |
Output is correct |
7 |
Correct |
9 ms |
16476 KB |
Output is correct |
8 |
Correct |
9 ms |
16376 KB |
Output is correct |
9 |
Correct |
11 ms |
16496 KB |
Output is correct |
10 |
Correct |
13 ms |
16732 KB |
Output is correct |
11 |
Correct |
14 ms |
16472 KB |
Output is correct |
12 |
Correct |
63 ms |
16644 KB |
Output is correct |
13 |
Correct |
112 ms |
16476 KB |
Output is correct |
14 |
Correct |
82 ms |
16732 KB |
Output is correct |
15 |
Correct |
637 ms |
17500 KB |
Output is correct |
16 |
Correct |
774 ms |
18524 KB |
Output is correct |
17 |
Correct |
613 ms |
17652 KB |
Output is correct |
18 |
Correct |
113 ms |
16728 KB |
Output is correct |
19 |
Correct |
884 ms |
18520 KB |
Output is correct |
20 |
Correct |
927 ms |
18520 KB |
Output is correct |