蓝桥杯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;
}
相关推荐
闻闻不会编程16 分钟前
704. 二分查找 (力扣)
数据结构·算法·leetcode
AndrewHZ22 分钟前
【图像处理基石】立体匹配的经典算法有哪些?
图像处理·算法·计算机视觉·滤波·模式识别·立体匹配
AndrewHZ26 分钟前
【图像处理入门】4. 图像增强技术——对比度与亮度的魔法调节
图像处理·算法·计算机视觉·几何变换·图像增强·模式识别
不二狗1 小时前
每日算法 -【Swift 算法】查找字符串数组中的最长公共前缀
开发语言·算法·swift
不二狗1 小时前
每日算法 -【Swift 算法】将整数转换为罗马数字
开发语言·算法·swift
Moonbit1 小时前
双周报Vol.73:移除使用方法实现 trait 、新增了 “错误多态” 功能、.语法支持使用 _ 的匿名函数...
后端·算法
chao_7892 小时前
链表题解——反转链表【LeetCode】
开发语言·python·算法
Code_流苏2 小时前
Python趣学篇:从零打造智能AI井字棋游戏(Python + Tkinter + Minimax算法)
python·算法·游戏·tkinter·智能井字棋·minimax算法
理智的灰太狼2 小时前
题目 3230: 蓝桥杯2024年第十五届省赛真题-星际旅行
算法·职场和发展·蓝桥杯
wcjwdq2 小时前
“顶点着色器”和“片元着色器”是先处理完所有顶点再统一进入片元阶段,还是一个顶点处理完就去跑它的片元?
算法·着色器