Skip to content

Commit

Permalink
Daily problems commit, 12/17/2016 Sat
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelam committed Dec 16, 2016
1 parent 1d7003d commit 64e4c93
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 0 deletions.
120 changes: 120 additions & 0 deletions CF742D/CF742D/OJtemplate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<string>
#include<cmath>
#include<cstdlib>
#include<ctime>
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<int> 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;
}
67 changes: 67 additions & 0 deletions CF742E/CF742E/OJtemplate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<string>
#include<cmath>
#include<cstdlib>
#include<ctime>
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]] <<endl;
}
}
return 0;
}
54 changes: 54 additions & 0 deletions poj1160/poj1160/OJtemplate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<string>
#include<cmath>
#include<cstdlib>
#include<ctime>
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;
}

0 comments on commit 64e4c93

Please sign in to comment.