D题1750分,E1500,然而D题通过率比E2还低…
差两分钟没把D写完,早看到E的话就去写E1了…
A. Meximization
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
int n, m, x, y, z, k, t, p;
int a[N], Num[N];
int solve()
{
cin >> n;
int cnt = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (a[i] < n)
Num[a[i]]++;
}
for (int i = 0; i < n; i++, cnt = i)
{
if (Num[i])
Num[i]--;
else
break;
cout << i << ' ';
}
for (int i = 1; i <= n; i++)
{
if (a[i] >= n)
cout << a[i] << ' ';
else if (Num[a[i]])
{
Num[a[i]]--;
cout << a[i] << ' ';
}
}
cout << endl;
}
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--)
solve();
}
B. M-arrays
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
int n, m, x, y, z, k, t, p;
int a[N];
int solve()
{
cin >> n >> m;
int cnt = 0;
for (int i = 0; i < m; i++)
a[i] = 0;
for (int i = 1; i <= n; i++)
{
cin >> x;
a[x % m]++;
}
for (int i = 1; i < (m + 1) / 2; i++)
if (a[i] || a[m - i])
{
int x = max(a[i], a[m - i]);
int y = min(a[i], a[m - i]);
int t = x - y;
if (t <= 1)
cnt++;
else
cnt += t;
}
if (a[0])
cnt++;
if (m % 2 == 0 && a[m / 2])
cnt++;
cout << cnt << endl;
}
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--)
solve();
}
C1&C2. k-LCM
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
int n, m, x, y, z, k, t, p;
int a[N];
int solve()
{
cin >> n >> k;
for (int i = 1; i <= k - 3; i++)
cout << 1 << ' ';
n -= k - 3;
if (n % 2)
cout << 1 << ' ' << n / 2 << ' ' << n / 2 << endl;
else
{
n /= 2;
if (n % 2)
cout << n - 1 << ' ' << n - 1 << ' ' << 2 << endl;
else
cout << n << ' ' << n / 2 << ' ' << n / 2 << endl;
}
}
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--)
solve();
}
D. Genius
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long N = 2e5 + 5;
long long n, m, x, y, z, k, t, p;
int a[N];
string s;
long long Dp[5005], Tmp[5005];
//dpi 表示以i点为结束时的最优解
int Flag[5005];
int solve()
{
cin >> n;
long long ans = 0;
for (int i = 1; i <= n; i++)
Dp[i] = 0;
for (int i = 1; i <= n; i++)
cin >> Flag[i];
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 2; i <= n; i++)
{
for (int j = 1; j <= i; j++)
if (Flag[j] != Flag[i])
Tmp[j] = Dp[j] + abs(a[j] - a[i]);
else
Tmp[j] = 0;
//记录从不同的j跳跃到i点时当前路径的最优解
for (int j = i - 1; j >= 1; j--)
Tmp[j] = max(Tmp[j], Tmp[j + 1]);
for (int j = 1; j <= i - 1; j++)
if (Flag[i] != Flag[j])
Dp[j] = max(Dp[j], Tmp[j + 1] + abs(a[i] - a[j]));
//从i点往回跳到不同j点,并更新Dpj的值。
//回跳的距离要保证超过前一次跳跃。
Dp[i] = Tmp[1];
for (int j = 1; j <= i; j++)
ans = max(ans, Dp[j]);
//记录最大值
}
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--)
solve();
}