牛客周赛 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,会的大佬可以留言告诉我

相关推荐
Mr_Xuhhh4 分钟前
算法刷题笔记:从滑动窗口到哈夫曼编码,我的算法进阶之路
开发语言·算法
MicroTech202510 分钟前
突破虚时演化非酉限制:MLGO微算法科技发布可在现有量子计算机运行的变分量子模拟技术
科技·算法·量子计算
hssfscv16 分钟前
软件设计师下午题六——Java的各种设计模式
java·算法·设计模式
珂朵莉MM25 分钟前
第七届全球校园人工智能算法精英大赛-算法巅峰赛产业命题赛第3赛季优化题--多策略混合算法
人工智能·算法
罗西的思考32 分钟前
【OpenClaw】通过 Nanobot 源码学习架构---(6)Skills
人工智能·深度学习·算法
枫叶林FYL36 分钟前
【自然语言处理 NLP】7.2 红队测试与对抗鲁棒性(Red Teaming & Adversarial Robustness)
人工智能·算法·机器学习
qiqsevenqiqiqiqi37 分钟前
字符串模板
算法
Fcy6481 小时前
算法基础详解(六)倍增思想与离散化思想
算法·快速幂·离散化·倍增算法
wuweijianlove1 小时前
算法调度问题中的代价模型与优化方法的技术5
算法
Dxy12393102161 小时前
Python路径算法简介
开发语言·python·算法