蓝桥杯15届省C

洛谷P10902回文数组

cpp 复制代码
#include<iostream>
#include<cmath>
using namespace std;
int n;
const int N = 100010;
int diff[N], a[N];
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++)cin >> a[i];
	for (int i = 1; i <= n / 2; i++) {
		diff[i] = a[i] - a[n - i + 1];
	}
	long long ans = 0;
	for (int i = 1; i <= n; i++) {
		ans += abs(diff[i]);
		if (diff[i] > 0 && diff[i + 1] > 0) {
			diff[i + 1] -= min(diff[i], diff[i + 1]);
		}
		else if (diff[i] < 0 && diff[i + 1] < 0) {
			diff[i + 1] -= max(diff[i], diff[i + 1]);
		}
	}
	cout << ans << endl;

	return 0;
}

洛谷P10903商品库存管理

cpp 复制代码
#include <iostream>
#include<vector>
using namespace std;
int n, m;
const int N = 300010;
int a[N], b[N];
typedef pair<int, int> PII;
vector<PII> query;
int main()
{
  cin >> n >> m;
  for(int i = 1; i <= n; i++){
    a[i] = 0;
    b[i] = a[i] - a[i - 1];
  }
  for(int i = 1; i <= m; i++){
    int l, r; cin >> l >> r;
    query.push_back({l, r});
    b[l] += 1;
    b[r + 1] -= 1;
  }
  for(int i = 1; i <= n; i++){
    a[i] = b[i] + a[i - 1];
  }
  int ans = 0;
  for(int i = 1; i <= n; i++){
    if(a[i] == 0)ans++;
    if(a[i] != 1)a[i] = 0;
  }
  for(int i = 1; i <= n; i++)a[i] += a[i - 1];
  for(auto item : query){
    cout << ans + a[item.second] - a[item.first - 1] << endl;
  }
  // 请在此输入您的代码
  return 0;
}
相关推荐
非优秀程序员5 小时前
第05章 播放器大战,暴风、快播与那一夜|《互联网产品恩仇录》
算法
武子康11 小时前
驾驶轨迹换个坐标,就能给移动机器人用吗?
人工智能·算法·机器学习
lisw0517 小时前
Dayhoff的PAM替换矩阵理论评介!
线性代数·算法·矩阵
一起学安全技术18 小时前
拆大模型 03|注意力的一句话版本:不用论文讲清 Attention
深度学习·算法·语言模型·transformer
Delightrs19 小时前
Rust LeetCode238 算法题解
开发语言·算法·rust
自信1504130575920 小时前
重生之从0开始学习c++之——vector-中篇
c++·学习·算法
GoAI1 天前
DeepSeek Harness Memory (一)主流记忆插件总结
后端·算法·面试
Pniubi1 天前
力扣54.螺旋矩阵
算法·leetcode·矩阵