蓝桥杯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;
}
相关推荐
迷途之人不知返8 小时前
链表相关的算法题(2)
数据结构·算法·链表
nju_spy8 小时前
力扣每日一题(四)线段树 + 树状数组 + 差分
数据结构·python·算法·leetcode·面试·线段树·笔试
xie0510_8 小时前
排序算法
数据结构·算法·排序算法
guygg888 小时前
基于自适应傅里叶分解(AFD)及其改进算法的信号分解与重构实现
算法
黑岚樱梦8 小时前
代码随想录打卡day25:56.合并区间
数据结构·算法
自由生长20248 小时前
科普-BOM是什么?和UTF-8什么关系?
算法
小年糕是糕手8 小时前
【数据结构】常见的排序算法 -- 插入排序
c语言·开发语言·数据结构·学习·算法·leetcode·排序算法
墨染点香9 小时前
LeetCode 刷题【142. 环形链表 II】
算法·leetcode·链表
海琴烟Sunshine9 小时前
leetcode 263. 丑数 python
python·算法·leetcode
信仰_2739932439 小时前
Guava Cache淘汰算法
算法·guava