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

· · 题解

题目大意

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

题目分析

虽然可以用 $\mathcal{O(n^2)}$ 的显然 $\verb!Dp!$ 水过,但是为了完成 $\rm P4597$ 和 $\rm P4331$,所以还是学了学 $\mathcal{O(n\log n)}$ 的优秀做法。 先把“严格递增”改成“非严格递增”。 具体地,$a_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$,即 $a_i\to a_i-i$。 然后用大根堆来维护。算到 $a[i]$ 时,取前面的最大数 $maxx$ 出来。因为要保持非严格递增,所以 $a[i]$ 小于 $maxx$ 时就必须做出一些决策了,这也就是 $|maxx-a[i]|$。 $\rm P4597$: 板子。 # 代码 $\rm CF713C$: ```cpp //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; } ``` $\rm P4597$: ```cpp //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; } ```