跳到正文

个人记录

Fuck_Karl

legendtim约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1
C++
#include<bits/stdc++.h>
using namespace std;
using ll = long long;

const int N = 1e6 + 5;
ll n;
int m;
ll a[N]; // 1~m
struct Interval
{
	ll l, r, len;
};
priority_queue<Interval> q;

struct node
{
	int id;
	ll pos;
}p[N];
bool operator < (const Interval &x, const Interval &y)
{
	return x.len < y.len;
}
int cur;

bool cmp(node x, node y)
{
	return x.pos < y.pos;
}
int main()
{
	freopen("parking2.in", "r", stdin);
	freopen("parking.out", "w", stdout);
	cin.tie(nullptr)->sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 1; i <= m; i++) cin >> a[i];
	q.push({0, n + 1, n + 1});
	for (int i = 1; i <= m; i++)
	{
		ll l = q.top().l, r = q.top().r, len = q.top().len;
		q.pop();
		p[i] = {i, l + r >> 1};
		ll l1 = l, r1 = p[i].pos - 1, len1 = r1 - l1 + 1;
		ll l2 = p[i].pos + 1, r2 = r, len2 = r2 - l2 + 1;
		q.push({l1, r1, len1});
		q.push({l2, r2, len2});
	}
	sort(p + 1, p + m + 1, cmp);
	for (int i = 1; i <= m; i++) cout << p[i].pos << ' ';
	ll res = 0;
//	if (p[2].pos - p[1].pos <= a[1])
//	{
//		ll d = p[2].pos - p[1].pos;
//		if (d <= a[1])
//		res += a[1] - d + 1;
//	}
//	if (p[m].pos - p[m - 1].pos <= a[m])
//	{
//		ll d = p[m].pos - p[m - 1].pos;
//		if (d <= a[m]) res += a[m] - d + 1;
//	}
	for (int i = 1; i <= m - 1; i++)
	{
		ll dl = p[i].pos - p[i - 1].pos;
		ll dr = p[i + 1].pos - p[i].pos;
		ll d = min(dl, dr);
		if (d > a[i]) continue;
		else res += a[i] - d + 1;
	}
	cout << res << endl;
	return 0;
}
原文