跳到正文

题解

CF713C Sonya and Problem Wihtout a Legend/P4597 序列 sequence

Coros_Trusds约 8 分钟
原文
文章信息与版本

文章信息

发布
2022年5月3日
收录
2026年2月10日
更新
2026年11月8日
篇幅
约 8 分钟
题目
P4597

版本

  1. v1当前

题目大意

给定一个有 nn 个正整数的数组,一次操作中,可以把任意一个元素加一或减一。求使得原序列严格递增的求最小操作次数。

题目分析

CF713C\rm CF713C:

虽然可以用 O(n2)\mathcal{O(n^2)} 的显然 Dp\verb!Dp! 水过,但是为了完成 P4597\rm P4597 和 P4331\rm P4331,所以还是学了学 O(nlog⁡n)\mathcal{O(n\log n)} 的优秀做法。

先把“严格递增”改成“非严格递增”。

具体地,a1<a2<a3→a1≤a2−1≤a3−2→a1−1≤a2−2≤a3−3a_1\lt a_2\lt a_3\to a_1\le a_2-1\le a_3-2\to a_1-1\le a_2-2\le a_3-3,即 ai→ai−ia_i\to a_i-i。

然后用大根堆来维护。算到 a[i]a[i] 时,取前面的最大数 maxxmaxx 出来。因为要保持非严格递增,所以 a[i]a[i] 小于 maxxmaxx 时就必须做出一些决策了,这也就是 ∣maxx−a[i]∣|maxx-a[i]|。

P4597\rm P4597:

板子。

代码

CF713C\rm CF713C:

C++
//2022/5/3
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <climits>//need "INT_MAX","INT_MIN"
#include <cstring>//need "memset"
#include <numeric>
#include <algorithm>
#include <queue>
#define int long long
#define enter putchar(10)
#define debug(c,que) std::cerr << #c << " = " << c << que
#define cek(c) puts(c)
#define blow(arr,st,ed,w) for(register int i = (st);i <= (ed); ++ i) std::cout << arr[i] << w;
#define speed_up() ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define mst(a,k) memset(a,k,sizeof(a))
#define Abs(x) ((x) > 0 ? (x) : -(x))
#define stop return(0)
const int mod = 1e9 + 7;
inline int MOD(int x) {
	if(x < 0) x += mod;
	return x % mod;
}
namespace Newstd {
	char buf[1 << 21],*p1 = buf,*p2 = buf;
	inline int getc() {
		return p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1 << 21,stdin),p1 == p2) ? EOF : *p1 ++;
	}
	inline int read() {
		int ret = 0,f = 0;char ch = getc();
		while (!isdigit(ch)) {
			if(ch == '-') f = 1;
			ch = getc();
		}
		while (isdigit(ch)) {
			ret = (ret << 3) + (ret << 1) + ch - 48;
			ch = getc();
		}
		return f ? -ret : ret;
	}
	inline void write(int x) {
		if(x < 0) {
			putchar('-');
			x = -x;
		}
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
	}
}
using namespace Newstd;

const int N = 3005;
int a[N];
std::priority_queue<int>q;
int n;
#undef int
int main(void) {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
	#define int long long
	n = read();
	for (register int i = 1;i <= n; ++ i) a[i] = read() - i;
	int ans = 0;
	for (register int i = 1;i <= n; ++ i) {
		q.push(a[i]);
		if (q.top() > a[i]) {
			ans = ans + Abs(q.top() - a[i]);
			q.pop();
			q.push(a[i]);
		}
	}
	printf("%lld\n",ans);

	return 0;
}

P4597\rm P4597:

C++
//2022/5/3
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <climits>//need "INT_MAX","INT_MIN"
#include <cstring>//need "memset"
#include <numeric>
#include <algorithm>
#include <queue>
#define int long long
#define enter putchar(10)
#define debug(c,que) std::cerr << #c << " = " << c << que
#define cek(c) puts(c)
#define blow(arr,st,ed,w) for(register int i = (st);i <= (ed); ++ i) std::cout << arr[i] << w;
#define speed_up() ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define mst(a,k) memset(a,k,sizeof(a))
#define Abs(x) ((x) > 0 ? (x) : -(x))
#define stop return(0)
const int mod = 1e9 + 7;
inline int MOD(int x) {
	if(x < 0) x += mod;
	return x % mod;
}
namespace Newstd {
	char buf[1 << 21],*p1 = buf,*p2 = buf;
	inline int getc() {
		return p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1 << 21,stdin),p1 == p2) ? EOF : *p1 ++;
	}
	inline int read() {
		int ret = 0,f = 0;char ch = getc();
		while (!isdigit(ch)) {
			if(ch == '-') f = 1;
			ch = getc();
		}
		while (isdigit(ch)) {
			ret = (ret << 3) + (ret << 1) + ch - 48;
			ch = getc();
		}
		return f ? -ret : ret;
	}
	inline void write(int x) {
		if(x < 0) {
			putchar('-');
			x = -x;
		}
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
	}
}
using namespace Newstd;

const int N = 5e5 + 5;
int a[N];
std::priority_queue<int>q;
int n;
#undef int
int main(void) {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
	#define int long long
	n = read();
	for (register int i = 1;i <= n; ++ i) a[i] = read();
	int ans = 0;
	for (register int i = 1;i <= n; ++ i) {
		q.push(a[i]);
		if (q.top() > a[i]) {
			ans += Abs(q.top() - a[i]);
			q.pop();
			q.push(a[i]);
		}
	}
	printf("%lld\n",ans);

	return 0;
}
原文