ICPC Southeastern Europe Contest 2019 BDFIJ
B(DP)
最重要的是状态转移对同一阶段的影响,
就像01背包优化为什么要倒叙,但这道题不光要倒叙,还要把这阶段的转移先存起来,最后统一保存
就避免了转移同一阶段相互影响
至于排序,当然希望升级多余的经验越多越好。
#include <bits/stdc++.h>
#define RE register
#define P pair<int, int>
#define PP pair<P, P>
#define ll long long
using namespace std;
int n, x, y;
ll f[501][501], ff[501][501];
PP a[501];
bool cmp(PP a, PP b)
{
return a.first.first < b.first.first;
}
int main()
{
scanf("%d%d%d", &n, &x, &y);
memset(f, 0x3f3f3f3f, sizeof f);
memset(ff, 0x3f3f3f3f, sizeof ff);
ll flag = f[0][0];
f[0][0] = 0;
for (RE int i = 1; i <= n; ++i)
scanf("%d%d%d%d", &a[i].first.first, &a[i].first.second, &a[i].second.first, &a[i].second.second);
sort(a + 1, a + 1 + n, cmp);
for (int i = 1; i <= n; ++i)
{
for (int c = 0; c <= x; ++c)
for (int d = 0; d <= y; ++d)
ff[c][d] = f[c][d];
for (int c = x; c >= 0; --c)
for (int d = y; d >= 0; --d)
{
if (f[c][d] == flag) continue;
if (c != x)
{
int nc = c + a[i].first.first;
int nd = d;
if (nc > x) nd += nc - x, nc = x;
nd = min(nd, y);
ff[nc][nd] = min(ff[nc][nd], f[c][d] + a[i].first.second);
}
int nd = min(d + a[i].second.first, y);
ff[c][nd] = min(ff[c][nd], f[c][d] + a[i].second.second);
}
for (int c = 0; c <= x; ++c)
for (int d = 0; d <= y; ++d)
f[c][d] = ff[c][d];
}
if (f[x][y] == flag) f[x][y] = -1;
printf("%lld", f[x][y]);
return 0;
}
D(造就完了)
#include <bits/stdc++.h>
using namespace std;
char s[1000005];
int a[26], len = 1, n;
vector<int> ve;
int main()
{
scanf("%s", s + 1);
for (int &i = len; s[i]; ++i) ++a[s[i] - 'a'];
--len; len >>= 1;
for (int i = 0; i < 26; ++i)
if (a[i] >= len) ve.emplace_back(i), ++n;
else if (a[i]) ++n;
if (ve.size() > 1 || (ve.size() == 1 && n < 3))
{
puts("NO");
return 0;
}
puts("YES");
if (!ve.empty())
{
a[ve[0]] -= len;
for (int i = 1; i <= len; ++i) printf("%c", ve[0] + 'a');
for (int i = 0; i < 26; ++i)
if (a[i] && i != ve[0])
{
--a[i]; printf("%c", 'a' +i);
break;
}
while (a[ve[0]]--) printf("%c", ve[0] + 'a');
}
for (int i = 0; i < 26; ++i)
while (a[i] > 0) printf("%c", i + 'a'), --a[i];
return 0;
}
F(树删边,nim)
#include <bits/stdc++.h>
using namespace std;
const int N=1e5 + 5;
vector<int> vec[N];
int n;
int dfs(int u, int fa)
{
int re = 0;
for (int i = 0; i < vec[u].size(); ++i)
if (vec[u][i] != fa)
re ^= 1 + dfs(vec[u][i], u);
return re;
}
int main()
{
scanf("%d", &n);
for (int i = 1, a, b; i < n; ++i)
{
scanf("%d%d", &a, &b);
vec[a].push_back(b);
vec[b].push_back(a);
}
printf(dfs(1,0)?"Alice":"Bob");
return 0;
}
I
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1005;
int a[maxn], b[maxn], n;
int main()
{
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> b[i];
int ans=0;
for (int i = 1; i<= n; ++i)
{
int tmp = 1e9 + 7;
for (int j = 1; j <= n; ++j)
tmp = min(tmp, abs(a[i] - b[j]));
ans = max(ans, tmp);
}
cout << ans << endl;
return 0;
}
J
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#define ll long long
#define RE register
#define FOR(i,a,b) for(RE int i = a; i <= b; ++i)
#define ROF(i,a,b) for(RE int i = a;i >= b; --i)
using namespace std;
const int maxn = 1005;
vector <int> e[maxn];
int n;
int main()
{
cin >> n;
FOR(i, 1, (ll)n * (n - 1) / 2)
{
int u, v, w;
cin >> u >> v >> w;
e[u].push_back(w);
e[v].push_back(w);
}
ll ans = 0;
FOR(i, 1, n)
{
sort(e[i].begin(), e[i].end());
FOR(j, 0, e[i].size() - 1)
ans += max(e[i][j], e[i][++j]);
}
cout << ans << endl;
return 0;
}

浙公网安备 33010602011771号