【Codeforces Round #708 (Div. 2) 】A~D题题解

在这里插入图片描述
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();
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值