使用C++的CCF-CSP满分解决方案 202112-1 序列查询

本文提供了一道关于计算特定序列累计增量的C++编程竞赛题解决方案。通过读取输入序列并计算每个元素与前一个元素之间的增量来解决这个问题,同时考虑到给定阈值N的情况。该解决方案使用了标准C++库中的功能,并禁用了标准I/O同步以提高效率。

在这里插入图片描述

满分代码实现

#include <map>
#include <set> 
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;

class Solution {
public:
	void solve();
};

void Solution::solve() {
	vector<int> seq;
	seq.push_back(0);
	int n = 0, N = 0;
	cin >> n >> N;
	int tmp = 0;
	for  (int i = 0; i < n; i++) {
		cin >> tmp;
		seq.push_back(tmp);
	}
	int ans = 0, i = 0;
	for (; i < n; i++) {
		if (seq[i + 1] > N) {
			ans += (N - seq[i]) * i;
			break;
		}
		ans += (seq[i + 1] - seq[i]) * i;
	}
	if (i == n && seq[i] <= N) {
		ans += (N - seq[i]) * i;
	}
	cout << ans;
}

int main() {
	// 提高cin,cout的速度 
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	Solution solution;
	solution.solve();
	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值