Codeforces Round #Pi (Div. 2) (ABCD)

本文解析了四道编程题目,包括求序列中等比子序列的数量、找出日志中最拥挤时刻的人数、确定战船游戏中首次出现矛盾的时间点以及计算序列中元素间的最大最小距离。
部署运行你感兴趣的模型镜像

A

解析:

水题,就是求每个数之间的距离的最大值和最小值。

my code

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
int n;
int x[N];

int main() {
    while(scanf("%d", &n) != EOF) {
        for(int i = 1; i <= n; i++) {
            scanf("%d", &x[i]);
        }
        int minv, maxv;
        minv = abs(x[2] - x[1]);
        maxv = abs(x[n] - x[1]);
        printf("%d %d\n", minv, maxv);

        for(int i = 2; i <= n - 1; i++) {
            minv = min(abs(x[i] - x[i-1]), abs(x[i+1] - x[i]));
            maxv = max(abs(x[n] - x[i]), abs(x[i] - x[1]));
            printf("%d %d\n", minv, maxv);
        }

        minv = abs(x[n] - x[n-1]);
        maxv = abs(x[n] - x[1]);
        printf("%d %d\n", minv, maxv);

    }
    return 0;
}

B

题意

给你一个日志文件系统
+ 进入的编号
- 出去的编号
问最多人的时候是几个人。

解析:

用数组模拟,每个id的出入情况。

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>
using namespace std;
const int N = 1e6 + 10;

int n;
char oper[5];
int r;
int A[N];

int main() {
    while(scanf("%d", &n) != EOF) {
        memset(A, 0, sizeof(A));
        int cur = 0, maxv = 0;
        for(int i = 0; i < n; i++) {
            scanf("%s%d", oper, &r);
            if(oper[0] == '-') {
                if(!A[r]) {
                    maxv++;
                    maxv = max(maxv, cur);
                }else {
                    A[r]--, cur--;
                }
            }else {
                if(!A[r]) {
                    cur++;
                    maxv = max(maxv, cur);
                    A[r]++;
                }
            }
        }
        printf("%d\n", maxv);
    }
    return 0;
}

C

题意:

给出一个序列的公比,问再这个序列中,存在着多少个 a,ak,akk这样的等比序列。

解析:

简单的dp问题,dp1[i]表示从前到后第i个位置A[i]/k有多少个,
dp2[i]表示从后到前第i个A[i]k有多少个。
那么最后的总和就是 dp1[i]dp2[i]

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll dp[N], dp2[N];
map<ll, int> mp;

ll n, k;
ll a[N];


int main() {
    while(scanf("%lld%lld", &n, &k) != EOF) {
        memset(dp, 0, sizeof(dp));
        memset(dp2, 0, sizeof(dp2));
        mp.clear();
        for(int i = 1; i <= n; i++) {
            scanf("%lld", &a[i]);
            if(a[i] % k == 0) {
                dp[i] = mp[a[i]/k];
            }
            mp[a[i]]++;
        }
        mp.clear();
        for(int i = n; i >= 1; i--) {
            dp2[i] = mp[a[i]*k];
            mp[a[i]]++;
        }
        ll ans = 0;
        for(int i = 1; i <= n; i++) {
            ans += dp[i] * dp2[i];
        }
        printf("%lld\n", ans);
    }
    return 0;
}

D

题意:

A和B在1n的平面上面玩一个战船游戏,这个游戏先由A放置战船再由B来击落战船,现在由B来执行击落战船m次,最早哪次和之前给出的条件矛盾。

解析:

二分最小的不符合条件的答案。

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define pb push_back
using namespace std;
const int N = 2e5 + 10;

int n, m, K, a;
int x[N];

bool check(int q) {
    vector<int> xx;
    for(int i = 0; i < q; i++)
        xx.pb(x[i]);
    xx.pb(0); xx.pb(n+1);
    sort(xx.begin(), xx.end());
    int cnt = 0;
    for(int i = 1; i < xx.size(); i++) {
        int len = xx[i] - xx[i-1];
        cnt += len / (a + 1);
    }
    return cnt >= K;
}

int main() {
    while(scanf("%d%d%d", &n, &K, &a) != EOF) {
        scanf("%d", &m);
        for(int i = 0; i < m; i++) {
            scanf("%d", &x[i]);
        }
        int L = 0, R = m;
        while(L < R) {
            int M = (L + R + 1) >> 1;
            if(check(M)) L = M;
            else R = M - 1;
        }
        if(L == m) printf("%d\n", -1);
        else printf("%d\n", L+1);
    }
    return 0;
}

您可能感兴趣的与本文相关的镜像

AutoGPT

AutoGPT

AI应用

AutoGPT于2023年3月30日由游戏公司Significant Gravitas Ltd.的创始人Toran Bruce Richards发布,AutoGPT是一个AI agent(智能体),也是开源的应用程序,结合了GPT-4和GPT-3.5技术,给定自然语言的目标,它将尝试通过将其分解成子任务,并在自动循环中使用互联网和其他工具来实现这一目标

内容概要:本文介绍了一种基于蒙特卡洛模拟和拉格朗日优化方法的电动汽车充电站有序充电调度策略,重点针对分时电价机制下的分散式优化问题。通过Matlab代码实现,构建了考虑用户充电需求、电网负荷平衡及电价波动的数学模【电动汽车充电站有序充电调度的分散式优化】基于蒙特卡诺和拉格朗日的电动汽车优化调度(分时电价调度)(Matlab代码实现)型,采用拉格朗日乘子法处理约束条件,结合蒙特卡洛方法模拟大量电动汽车的随机充电行为,实现对充电功率和时间的优化分配,旨在降低用户充电成本、平抑电网峰谷差并提升充电站运营效率。该方法体现了智能优化算法在电力系统调度中的实际应用价值。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事新能源汽车、智能电网相关领域的工程技术人员。; 使用场景及目标:①研究电动汽车有序充电调度策略的设计与仿真;②学习蒙特卡洛模拟与拉格朗日优化在能源系统中的联合应用;③掌握基于分时电价的需求响应优化建模方法;④为微电网、充电站运营管理提供技术支持和决策参考。; 阅读建议:建议读者结合Matlab代码深入理解算法实现细节,重点关注目标函数构建、约束条件处理及优化求解过程,可尝试调整参数设置以观察不同场景下的调度效果,进一步拓展至多目标优化或多类型负荷协调调度的研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值