蓝桥杯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;
}
相关推荐
AI科技星4 分钟前
质量定义方程常数k = 4π m_p的来源、推导与意义
服务器·数据结构·人工智能·科技·算法·机器学习·生活
摇摆的含羞草22 分钟前
哈希(hash)算法使用特点及常见疑问解答
算法·哈希算法
仰泳的熊猫1 小时前
1077 Kuchiguse
数据结构·c++·算法·pat考试
LYFlied1 小时前
【每日算法】LeetCode 19. 删除链表的倒数第 N 个结点
算法·leetcode·链表
踏浪无痕1 小时前
计算机算钱为什么会算错?怎么解决?
后端·算法·面试
夏乌_Wx2 小时前
练题100天——DAY28:找消失的数字+分发饼干
数据结构·算法
studytosky2 小时前
深度学习理论与实战:反向传播、参数初始化与优化算法全解析
人工智能·python·深度学习·算法·分类·matplotlib
WolfGang0073212 小时前
代码随想录算法训练营Day48 | 108.冗余连接、109.冗余连接II
数据结构·c++·算法
努力学算法的蒟蒻3 小时前
day35(12.16)——leetcode面试经典150
算法·leetcode·面试
cccc来财3 小时前
角点检测算法:Harris 和 FAST 方法
算法·计算机视觉·特征提取