Codeforces Round 960 (Div. 2)解题报告(B.C)

Codeforces Round 960 (Div. 2)解题报告(B.C)

B. Array Craft

链接B. Array Craft

题目:在这里插入图片描述
解题思路:根据题目我们显然可以知道x>y,若要使x前缀和最大则x之后的值到x的前缀和必定小于0,故可以-1,1,-1,1交替出现,y同理,让y之前的数-1,1交替出现,
然后只需x,y在[y,x]区间内分别为前,后缀和最大就行了,只需要在剩下位置都为1就可以满足了
代码:

#include <bits/stdc++.h>
using namespace std;

#define ll long long

void solve() {
    int n, x, y;
    cin >> n >> x >> y;
    vector<int> a(n, 1);
    x--; y--;

    int t = -1;
    for (int i = y-1; i >= 0; i--) {
        a[i] = t;
        t = -t;
    }

    t = -1;
    for (int i = x+1; i < n; i++) {
        a[i] = t;
        t = -t;
    }

    for (int i = 0; i < n; i++) {
        cout << a[i];
        if (i < n-1) cout << ' ';
        else cout << '\n';
    }
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

C. Mad MAD Sum

链接:C. Mad MAD Sum

题目:在这里插入图片描述
解题思路:通过打表发现经过两次变换后之后的数组的值就已经基本确定,剩下的变换都是数组的值不断右移罢了
例如:
11522533(初始数组)
01112555
00111155
00011115
00001111
00000111
00000011
00000001
00000000
代码:

#include <bits/stdc++.h>
using namespace std;

#define ll long long

void solve() {
    int n;
    cin >> n;
    ll a[n], sum = 0;
    map<ll,ll> m;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        if (a[i] > 0) {
            sum += a[i];
        }
    }

    int max = 0;
    for (int i = 0; i < n; i++) {
        if (m.end() != m.find(a[i])) m[a[i]]++;
        else m.insert(pair<ll,ll>(a[i], 1));
        if ((m[a[i]] >= 2) && a[i] > max) {
            max = a[i];
        }
        a[i] = max;
    }
    for (int i = 0; i < n; i++) {
        if (a[i] > 0) {
            sum += a[i];
        }
    }
    m.clear();
    max = 0;
    for (int i = 0; i < n; i++) {
        if (m.end() != m.find(a[i])) m[a[i]]++;
        else m.insert(pair<ll,ll>(a[i], 1));
        if ((m[a[i]] >= 2) && a[i] > max) {
            max = a[i];
        }
        a[i] = max;
    }
    for (int i = 0; i < n; i++) {
        if (a[i] > 0) {
            sum += a[i];
        }
    }
    
    for (int i = 0; i < n; i++) {
        if (a[i] > 0) {
            sum += a[i] * (n-1-i);
        }
    }
    cout << sum << '\n';
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

题解代码(cpp):

#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 200005;

int n;
int a[N];

bool c[N];
void doit()
{
    for (int i = 1; i <= n; ++i)
        c[i] = false;
    int maxu = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (c[a[i]])
            maxu = max(maxu, a[i]);
        c[a[i]] = true;
        a[i] = maxu;
    }
}

void solv()
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];

    ll ans = 0;
    for (int i = 1; i <= n; ++i)
        ans += a[i];
    doit();
    for (int i = 1; i <= n; ++i)
        ans += a[i];
    doit();

    for (int i = 1; i <= n; ++i)
        ans += (n - i + 1) * 1LL * a[i];
    cout << ans << "\n";
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    ios_base::sync_with_stdio(false), cin.tie(0);

    int tt = 1;
    cin >> tt;
    while (tt--)
    {
        solv();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值