牛客周赛 Round 100

A小红的双排列

没什么好说的 直接 1 1 2 2 3 3 4 4......

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int n;
int main(){
	ios::sync_with_stdio(false);        // 禁用同步
    cin.tie(nullptr);                   // 解除cin与cout绑定
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cout << i << " " << i<< " ";
    }
    cout << endl;
    return 0;
}

B小红的双排列拆分

就要注意是子序列,顺序不能倒,然后就是标记已经打印的数,并且记住他的位置(或使他们变为-1)

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long 
int n;
using namespace std;
int a[200];
int b[100];
int main() {
    ios::sync_with_stdio(false);        // 禁用同步
    cin.tie(nullptr);                   // 解除cin与cout绑定
    int s;
    cin >> n;
    for (int i = 1; i <= n*2; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n*2; i++) {
        if (b[a[i]] == 0) {
            cout << a[i] << " ";
            b[a[i]]++;
            a[i] = -1;
        }
    }
    cout << endl;
    for (int i = 1; i <= n*2; i++) {
        if (a[i] != -1) {
            cout << a[i] << " ";
        }
    }
    cout << endl;
    return 0;
}

C小红的双排列删除

我一开始写烦了,就不讲之前的方法了,将一个简单的。

就是看从第一个开始能不能找到连续的使其消除 例 1 ......1 2......2 3......3

之前代码:

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int t, n;
int a[400005];
int main() {
    ios::sync_with_stdio(false);        // 禁用同步
    cin.tie(nullptr);                   // 解除cin与cout绑定
    cin >> t;
    while (t--) {
        cin >> n;
        for (int i = 0; i < n*2; i++) {
            cin >> a[i];
        }
        stack<int> q;
        vector<bool> f(n+1);
        for (int i = 1; i < n; i++) {
            f[i] = false;
        }
        for (int i = 0; i < n * 2; i++) {
            if (q.empty()) {
                q.push(a[i]);
                f[a[i]] = true;
            }
            else {
                if(f[a[i]]==false)
                {
                    f[a[i]] = true;
                    q.push(a[i]);
                }
                else
                {
                    while(!q.empty()&&q.top()!=a[i]) {
                        f[q.top()] = false;
                        q.pop();
                    }
                    if(!q.empty()){
                        f[q.top()] = false;
                        q.pop();
                    }
                }
            }
        }
        if (q.empty()) {
            cout << "Yes" << endl;
        }
        else {
            cout << "No" << endl;
        }
    }
    return 0;
}

之后简单代码:

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int t, n;
int a[400005];
int main() {
    ios::sync_with_stdio(false);        // 禁用同步
    cin.tie(nullptr);                   // 解除cin与cout绑定
    cin >> t;
    while (t--) {
        cin >> n;
        for (int i = 0; i < n * 2; i++) {
            cin >> a[i];
        }
        int j = 0;
        for (int i = 1; i < n * 2; i++) {
            if (a[j] == a[i]) {
                j = i + 1;
                i++;
            }
        }
        if (j>=n*2) {
            cout << "Yes" << endl;
        }
        else {
            cout << "No" << endl;
        }
    }
    return 0;
}

D小红的双排列权值

不难发现: 1 2 1 2/ 2 1 1 2 / 1 2 2 1/2 1 2 1 怎么交换权值都不会遍

只有:1 1 2 2 1 2怎样交换权值都是变多 (a[2]-a[1])*2

所以找到距离最远的两个就解决了

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int n;
int a[400005];
int b[200005];
bool c[200005];
ll sum = 0;
int main() {
    ios::sync_with_stdio(false);        // 禁用同步
    cin.tie(nullptr);                   // 解除cin与cout绑定
    cin >> n;
    for (int i = 1; i <= n*2; i++) {
        cin >> a[i];
    }
    int q = 0;
    for (int i = 1; i <= n*2; i++) {
        if (b[a[i]] == 0) {
            b[a[i]] = i;
        }
        else {
            sum += i - b[a[i]] - 1;
            if (q == 0) {
                q = i;
            }
        }
    }
    int qq = 0;
    for (int i = n * 2; i >=1; i--) {
        if(c[a[i]]==false){
            c[a[i]] = true;
        }
        else {
            qq = i;
            break;
        }
    }
    if (q < qq) {
        cout << (qq - q) * 2 + sum << endl;
    }
    else {
        cout << sum << endl;
    }
    return 0;
}

E小红的双排列删除得分

动态规划题: dp[i] = max(dp[i], dp[c[a[i]]] + b[i] - b[c[a[i]] - 1]); (有点小难)

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int n;
ll dp[200005];
int h = 0;
ll a[400005], b[400005], c[200005];
int main() {
    ios::sync_with_stdio(false);        // 禁用同步
    cin.tie(nullptr);                   // 解除cin与cout绑定
    cin >> n;
    a[0] = 0;
    for (int i = 1; i <= 2*n; i++) {
        cin >> a[i];
        b[i] = b[i - 1] + a[i];
    }
    for (int i = 1; i <=2* n; i++) {
        dp[i] = dp[i - 1];
        if (c[a[i]]) {
            dp[i] = max(dp[i], dp[c[a[i]]] + b[i] - b[c[a[i]] - 1]);
        }
        c[a[i]] = i;
    }
    cout << dp[n * 2] << endl;
    return 0;
}

F小红的双排列期望(easy)

这题好像是dp题,但我没有用dp来写

我是直接算,唯一要知道的就是除法的逆序元

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
ll mod = 1e9 + 7;
int n;
ll chu(ll w, int k) {
    ll sum0 = 1;
    while (k>0) {
        if (k % 2 != 0) {
            sum0 = (sum0 * w) % mod;
        }
        w = (w * w) % mod;
        k /= 2;
    }
    return sum0;
}
ll b[102];
int main() {
    ios::sync_with_stdio(false);        // 禁用同步
    cin.tie(nullptr);                   // 解除cin与cout绑定
    cin >> n;
    vector<int>a(n*2);
    for (int i = 0; i < n*2; i++) {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    b[1] = 1;
    for (int i = 2; i <=100; i++) {
        b[i] = chu(i, mod - 2);
    }
    ll sum = 0;
    for (int i = 0; i < n * 2; i++) {
        sum = (sum + (i + 2) / 2 * 100 * b[a[i]] % mod) % mod;
    }
    cout << sum << endl;
    return 0;
}

G 小红的双排列期望(hard)

dp题,但我有点小菜,目前还=没有学过这种dp,会的大佬可以留言告诉我

相关推荐
顾你&25 分钟前
机器学习之无监督学习算法大总结
学习·算法·机器学习
神龙斗士2401 小时前
Java 数组的定义与使用
java·开发语言·数据结构·算法
Y.O.U..1 小时前
力扣HOT100-跳跃游戏II
算法·leetcode
hn小菜鸡1 小时前
LeetCode 3132.找出与数组相加的整数 II
算法·leetcode·职场和发展
微笑尅乐1 小时前
数组模拟加法——力扣66.加一
算法·leetcode·职场和发展
_不会dp不改名_1 小时前
leetcode_146 LRU缓存
算法·leetcode·缓存
帅帅爱数学2 小时前
DeepMimic论文详细解析:基于示例引导的深度强化学习实现物理仿真角色技能
算法·强化学习
IT成长日记3 小时前
【LVS入门宝典】LVS调度算法轮询(RR)深度解析:从原理到实战的公平调度之道
算法·lvs·rr·轮询调度算法
NAGNIP3 小时前
一文搞懂量化、剪枝和知识蒸馏都是什么?
算法
点云SLAM3 小时前
GTSAM 中自定义因子(Custom Factor)的详解和实战示例
算法·机器人·slam·后端优化·gtsam·gtsam自定义因子·因子图