diff --git a/CF742D/CF742D/OJtemplate.cpp b/CF742D/CF742D/OJtemplate.cpp new file mode 100644 index 0000000..b509f2f --- /dev/null +++ b/CF742D/CF742D/OJtemplate.cpp @@ -0,0 +1,120 @@ +#pragma comment(linker, "/STACK:1024000000,1024000000") +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +const int maxn = 1000 + 50; +int n; +int par[maxn]; +int rk[maxn]; +void init() +{ + for (int i = 0; i < n; i++) + { + par[i] = i; + rk[i] = 1; + } +} +int fd(int u) +{ + if (par[u] == u) + { + return u; + } + return par[u] = fd(par[u]); +} +void join(int u, int v) +{ + int pu = fd(u), pv = fd(v); + if (pu == pv) + return; + if (rk[pv] == rk[pu]) + { + par[pv] = pu; + rk[pu]++; + } + if (rk[pu] > rk[pv]) + { + par[pv] = pu; + return; + } + if (rk[pv] > rk[pu]) + { + par[pu] = pv; + } +} +int bi[maxn]; +int wi[maxn]; +int dp[maxn][maxn]; +bool vis[maxn]; +int wsum[maxn]; +int bsum[maxn]; +int groupno[maxn]; +vector group[maxn]; +int main() +{ + int m, w; + while (cin >> n >> m >> w) + { + for (int i = 0; i < n; i++) + { + cin >> wi[i]; + } + for (int i = 0; i < n; i++) + { + cin >> bi[i]; + } + init(); + int uu, vv; + for (int i = 0; i < m; i++) + { + cin >> uu >> vv; + join(uu - 1, vv - 1); + } + int cnt = 0; + memset(vis, 0, sizeof(vis)); + memset(bsum, 0, sizeof(bsum)); + memset(wsum, 0, sizeof(wsum)); + for (int i = 0; i < n; i++) + { + if (!vis[fd(i)]) + { + group[cnt].clear(); + vis[fd(i)] = true; + groupno[fd(i)] = cnt++; + } + wsum[groupno[fd(i)]]+= wi[i]; + bsum[groupno[fd(i)]] += bi[i]; + group[groupno[fd(i)]].push_back(i); + } + memset(dp, 0, sizeof(dp)); + for (int i = 0; i < cnt; i++) + { + for (int j = w; j >= 0; j--) + { + dp[i + 1][j] = dp[i][j]; + for (int k = 0; k < group[i].size(); k++) + { + if (j >= wi[group[i][k]]) + dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - wi[group[i][k]]] + bi[group[i][k]]); + } + if (j >= wsum[i]) + { + dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - wsum[i]] + bsum[i]); + } + } + } + cout << dp[cnt][w] << endl; + } + return 0; +} \ No newline at end of file diff --git a/CF742E/CF742E/OJtemplate.cpp b/CF742E/CF742E/OJtemplate.cpp new file mode 100644 index 0000000..a62e064 --- /dev/null +++ b/CF742E/CF742E/OJtemplate.cpp @@ -0,0 +1,67 @@ +#pragma comment(linker, "/STACK:1024000000,1024000000") +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +const int maxn = 100000 + 100; +int pairr[maxn * 2]; +bool vis[maxn * 2]; +int meal[maxn * 2]; +int pa[maxn], pb[maxn]; +int main() +{ + int n; + int u, v; + while (cin >> n) + { + for (int i = 0; i < n; i++) + { + cin >> u >> v; + u--; + v--; + pa[i] = u; + pb[i] = v; + pairr[u] = v; + pairr[v] = u; + } + memset(vis, 0, sizeof(vis)); + int N = n * 2; + for (int i = 0; i < N; i++) + { + if (!vis[i]) + { + int p = i; + int flip = 0; + while (!vis[p]) + { + vis[p] = true; + meal[p] = flip + 1; + flip ^= 1; + if (!vis[pairr[p]]) + { + p = pairr[p]; + } + else + { + p = p ^ 1; + } + } + } + } + for (int i = 0; i < n; i++) + { + cout << meal[pa[i]] << " " << meal[pb[i]] < +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +const int maxv = 300 + 10; +const int maxp = 30 + 5; +int V, P; +int dp[maxp][maxv]; +int cost[maxv][maxv]; +int x[maxv]; +const int inf = 0x3f3f3f3f; +int main() +{ + while (cin >> V >> P) + { + memset(dp, inf, sizeof(dp)); + for (int i = 0; i < V; i++) + { + cin >> x[i]; + } + memset(cost, 0, sizeof(cost)); + for (int i = 0; i < V; i++) + { + for (int j = i + 1; j < V; j++) + { + cost[i][j] = cost[i][j - 1] + x[j] - x[(i + j) / 2]; + } + } + dp[0][0] = 0; + for (int k = 1; k <= P; k++) + { + for (int i = 0; i < V; i++) + { + for (int j = 0; j <= i; j++) + { + dp[k][i + 1] = min(dp[k - 1][j] + cost[j][i], dp[k][i + 1]); + } + } + } + cout << dp[P][V] << endl; + } + return 0; +} \ No newline at end of file