Submission #3665018
Source Code Expand
#include <cassert>
#include <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <queue>
using namespace std;
struct BoolName : numpunct<char> {
string t, f;
BoolName (string t = "Yes", string f = "No") : t(t), f(f) {}
string do_truename() const {return t;}
string do_falsename() const {return f;}
};
struct Initializer {
Initializer() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(15) << boolalpha;
cout.imbue(locale(cout.getloc(), new BoolName));
}
} initializer;
template<typename T> istream& operator>>(istream &s, vector<T> &v) {
for (T &t : v) s >> t;
return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
for (const T &t : v) s << t << endl;
return s;
}
void set_bool_name(string t, string f) {
cout.imbue(locale(cout.getloc(), new BoolName(t, f)));
}
template<typename T> bool chmin(T& a, T b) {return a > b ? a = b, true : false;}
template<typename T> bool chmax(T& a, T b) {return a < b ? a = b, true : false;}
vector<int> solve(const vector<int>& c) {
int n = c.size();
vector<int> res(n + 1, -1);
{
vector<vector<int>> dp1(n + 1, vector<int>(2, -1));
vector<vector<int>> dp2(n + 1, vector<int>(2, -1));
dp1[0][0] = 0;
for (int i = 1; i < n; ++i) {
for (int j = 0; j <= n; ++j) dp2[j][0] = dp2[j][1] = -1;
for (int j = 0; j <= i; ++j) {
if (dp1[j][0] != -1) {
chmax(dp2[j + 1][0], dp1[j][0] + c[i]);
chmax(dp2[j + 1][1], dp1[j][0] + 2 * c[i]);
}
if (dp1[j][1] != -1) {
chmax(dp2[j][0], dp1[j][1]);
chmax(dp2[j + 1][1], dp1[j][1] + c[i]);
}
}
swap(dp1, dp2);
}
/*
for (int i = 0; i <= n; ++i) {
for (int j = 0; j < 2; ++j) {
cerr << dp1[i][j] << " ";
}
cerr << endl;
}
*/
for (int i = 0; i <= n; ++i) {
if (dp1[i][0] != -1) chmax(res[i + 1], dp1[i][0] + c[0]);
if (dp1[i][1] != -1) chmax(res[i], dp1[i][1]);
}
}
{
vector<vector<int>> dp1(n + 1, vector<int>(2, -1));
vector<vector<int>> dp2(n + 1, vector<int>(2, -1));
dp1[1][1] = c[0];
for (int i = 1; i < n; ++i) {
for (int j = 0; j <= n; ++j) dp2[j][0] = dp2[j][1] = -1;
for (int j = 0; j <= i; ++j) {
if (dp1[j][0] != -1) {
chmax(dp2[j + 1][0], dp1[j][0] + c[i]);
chmax(dp2[j + 1][1], dp1[j][0] + 2 * c[i]);
}
if (dp1[j][1] != -1) {
chmax(dp2[j][0], dp1[j][1]);
chmax(dp2[j + 1][1], dp1[j][1] + c[i]);
}
}
swap(dp1, dp2);
}
for (int i = 0; i <= n; ++i) {
if (dp1[i][0] != -1) chmax(res[i], dp1[i][0] + c[0]);
if (dp1[i][1] != -1) chmax(res[i], dp1[i][1]);
}
}
return res;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> a(n), b(n);
cin >> a >> b;
vector<vector<int>> g(n);
for (int i = 0; i < n; ++i) {
g[a[i] - 1].emplace_back(b[i] - 1);
g[b[i] - 1].emplace_back(a[i] - 1);
}
vector<vector<int>> c;
vector<bool> used(n);
for (int i = 0; i < n; ++i) {
if (used[i]) continue;
int v = i, p = -1;
vector<int> circle;
do {
used[v] = true;
circle.emplace_back(v + 1);
auto u = g[v][0];
if (u == p) u = g[v][1];
p = v;
v = u;
} while (v != i);
c.emplace_back(circle);
}
vector<int> dp(n + 1, -1);
dp[0] = 0;
for (int i = 0; i < int(c.size()); ++i) {
auto v = solve(c[i]);
//cerr << v;
vector<int> dp2(n + 1, -1);
for (int j = 0; j <= n; ++j) {
if (dp[j] == -1) continue;
for (int x = c[i].size() / 2; x < int(v.size()) && j + x <= n; ++x) {
if (v[x] == -1) continue;
chmax(dp2[j + x], dp[j] + v[x]);
}
}
dp = dp2;
}
int res = 0;
for (int i = k; i <= n; ++i) chmax(res, dp[i]);
cout << res << endl;
}
Submission Info
Submission Time |
|
Task |
G - Sum of Cards |
User |
not |
Language |
C++11 (GCC 4.8.1) |
Score |
500 |
Code Size |
4055 Byte |
Status |
AC |
Exec Time |
168 ms |
Memory |
1152 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
500 / 500 |
Status |
|
|
Set Name |
Test Cases |
Sample |
s1.txt, s2.txt, s3.txt |
All |
01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, s1.txt, s2.txt, s3.txt |
Case Name |
Status |
Exec Time |
Memory |
01.txt |
AC |
1 ms |
256 KB |
02.txt |
AC |
1 ms |
256 KB |
03.txt |
AC |
1 ms |
256 KB |
04.txt |
AC |
1 ms |
256 KB |
05.txt |
AC |
1 ms |
256 KB |
06.txt |
AC |
1 ms |
256 KB |
07.txt |
AC |
2 ms |
256 KB |
08.txt |
AC |
2 ms |
256 KB |
09.txt |
AC |
1 ms |
256 KB |
10.txt |
AC |
1 ms |
256 KB |
11.txt |
AC |
1 ms |
256 KB |
12.txt |
AC |
1 ms |
256 KB |
13.txt |
AC |
2 ms |
256 KB |
14.txt |
AC |
1 ms |
256 KB |
15.txt |
AC |
1 ms |
256 KB |
16.txt |
AC |
110 ms |
1024 KB |
17.txt |
AC |
38 ms |
768 KB |
18.txt |
AC |
68 ms |
960 KB |
19.txt |
AC |
73 ms |
932 KB |
20.txt |
AC |
50 ms |
804 KB |
21.txt |
AC |
86 ms |
904 KB |
22.txt |
AC |
127 ms |
1076 KB |
23.txt |
AC |
168 ms |
1152 KB |
24.txt |
AC |
52 ms |
768 KB |
25.txt |
AC |
114 ms |
1024 KB |
26.txt |
AC |
135 ms |
1064 KB |
27.txt |
AC |
45 ms |
788 KB |
28.txt |
AC |
32 ms |
768 KB |
29.txt |
AC |
135 ms |
1064 KB |
30.txt |
AC |
134 ms |
1068 KB |
31.txt |
AC |
14 ms |
640 KB |
32.txt |
AC |
16 ms |
640 KB |
33.txt |
AC |
17 ms |
640 KB |
34.txt |
AC |
12 ms |
640 KB |
35.txt |
AC |
17 ms |
640 KB |
36.txt |
AC |
70 ms |
896 KB |
37.txt |
AC |
82 ms |
1048 KB |
38.txt |
AC |
40 ms |
768 KB |
39.txt |
AC |
54 ms |
896 KB |
40.txt |
AC |
72 ms |
948 KB |
s1.txt |
AC |
1 ms |
256 KB |
s2.txt |
AC |
1 ms |
256 KB |
s3.txt |
AC |
1 ms |
256 KB |