Gym - 101669部分题题解

A - Concerts(动规)

题意是给你一个子串b和一个主串a,问在满足条件下a串可以有多少种b的规划方式。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 100;
const int mod = 1e9 + 7;
ll f[2][N];
    int k, n;
    int num[30];
    char a[10010], b[N];

int main()
{
    cin >> k >> n;
    for(int i = 0; i < 26; i ++)
    {
       cin >> num[i];
    } cin >> a + 1 >> b + 1;


    for(int i = 0; i <= n; i ++) f[0][i] = 1;
    for(int i = 1; i <= k; i ++){
        f[i & 1][0] = 0;
        for(int j = 1; j <= n; j ++)
        {
            f[i & 1][j] = f[i & 1][j - 1];
            if (a[i] == b[j] && j - 1 - num[a[i - 1] - 'A'] >= 0)
                f[i & 1][j] += f[i - 1 & 1][j - 1 - num[a[i - 1] - 'A']];
            f[i & 1][j] %= mod;
        }
    }
    cout << f[k & 1][n] << endl;
}

F - Binary Transformations

题意是给一个a串和一个b串,问从a串转化成b串最少需要多少代价。
每次可以转化一个字符。代价是转化完之后a串剩下的1的代价之和。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e3 + 10;
vector<int> v;
multiset<int> s1, s2;
int n;
char a[N], b[N];
int num[N];
ll sum, ans; 
bool cmp(int x, int y)
{
    return x > y; 
}
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> num[i];
    cin >> a >> b; 
    for (int i = 0; i < n; ++i) {
        if (a[i] == '1' && b[i] == '1')
            v.push_back(num[i]);
        else if (a[i] == '1' && b[i] == '0')
            s1.insert(num[i]);
        else if (a[i] == '0' && b[i] == '1')
            s2.insert(num[i]);
        if (a[i] == '1') sum += (ll)num[i];
    }
    sort(v.begin(), v.end(), cmp);
    int len = v.size();
    ans = INF;
    for (int i = 0; i <= len; i ++) {
        ll sum1 = sum, res = 0;
        if (i) {
            s1.insert(v[i - 1]);
            s2.insert(v[i - 1]);
        }
        for (auto it2 = s1.rbegin(); it2 != s1.rend(); it2 ++) {
            sum1 -= (*it2);
            res += sum1;
        }
        for (auto it1 = s2.begin(); it1 != s2.end(); it1 ++) {
            sum1 += (*it1);
            res += sum1;
        }
        ans = min(res, ans);
    }
    cout << ans << endl;
    return 0;
}

G - Robots

水题

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N = 1e4 + 10, M = 1 << 10;
const ll INF = 0x3f3f3f3f;

struct node
{
    int a, t;
} load[N];

bool cmp(node a, node b)
{
    return a.a > b.a;
}
int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i ++)
    {
        cin >> load[i].a >> load[i].t;
    }
    int v1 = 0;
    ll sum1 = 0;
    for(int i = 0; i < n; i ++)
    {
        sum1 += v1 * load[i].t + 0.5 * load[i].a * load[i].t * load[i].t;
        v1 = v1 + load[i].a * load[i].t;
    }
    sort(load, load + n, cmp);
    int v2 = 0;
    ll sum2 = 0;
    for(int i = 0; i < n; i ++)
    {
        sum2 += v2 * load[i].t + 0.5 * load[i].a * load[i].t * load[i].t;
        v2 = v2 + load[i].a * load[i].t;
    }
    double sum = sum2-sum1;
    printf("%.1lf",sum);
}

J - Cunning Friends

三个人博弈,打表找出规律。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 10;
int a[N];
int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    if (n == 1) {
        printf("Win\n");
        return 0;
    }
    if (n == 2) {
        if (a[1] == 1 || a[2] == 1)
            printf("Win\n");
        else
            printf("Lose\n");
        return 0;
    }
    sort(a + 1, a + 1 + n);
    if (a[n] == 1) {
        if (n % 3 == 0)
            printf("Lose\n");
        else
            printf("Win\n");
    }

    else if (a[n - 1] == 1)
        printf("Win\n");
    else if (a[n - 2] == 1) {
        if (a[n - 1] == 2 && (n - 2) % 3 != 0)
            printf("Win\n");
        else
            printf("Lose\n");
    } else
        printf("Lose\n");
}

K - Escape Room

水题

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct node {
    int num;
    int value;
    int last;
} a[100010];

bool cmp(struct node a, struct node b) {
    if (a.value != b.value)
        return a.value < b.value;
    else
        return a.num < b.num;
}

bool cmp1(struct node a, struct node b) { return a.num < b.num; }

int main(int argc, char const *argv[]) {
    int n, i;
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        scanf("%d", &a[i].value);
        a[i].num = i;
    }
    sort(a + 1, a + 1 + n, cmp);
    for (i = 1; i <= n; i++) a[i].last = n - i + 1;
    sort(a + 1, a + 1 + n, cmp1);
    for (i = 1; i < n; i++) printf("%d ", a[i].last);
    printf("%d\n", a[i].last);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值