如何用最小代价将序列转换为非严格递增序列的codeforces13C问题中,如何应用DP、离散化和滚动数组?
- 内容介绍
- 文章标签
- 相关推荐
本文共计562个文字,预计阅读时间需要3分钟。
伪原创以下内容:
题目:传送门:.Sequence 描述:.Sequence 传送门:.Sequence 描述:.Sequence 时间限制:每题1秒,内存限制:每题64MB 输入:标准输入 输出:标准输出 小佩蒂亚非常喜欢玩游戏。并且她非常喜欢玩mos。
传送门:C.Sequence描述:C.Sequence传送门:C. Sequence
描述:
C. Sequence time limit per test 1 second memory limit per test 64 megabytes input standard input output standard outputLittle Petya likes to play very much. And most of all he likes to play the following game:
He is given a sequence ofNinteger numbers. At each step it is allowed to increase the value of any number by1or to decrease it by1. The goal of the game is to make the sequence non-decreasing with the smallest number of steps. Petya is not good at math, so he asks for your help.
The sequenceais called non-decreasing ifa1 ≤ a2 ≤ ... ≤ aNholds, whereNis the length of the sequence.
InputThe first line of the input contains single integerN(1 ≤ N ≤ 5000) — the length of the initial sequence. The followingNlines contain one integer each — elements of the sequence. These numbers do not exceed109by absolute value.
OutputOutput one integer — minimum number of steps required to achieve the goal.
Examples input 53 2 -1 2 11 output 4 input 52 1 1 1 1 output 1题意:
给你n个数,每次操作都可以使某个数加1或者减1,求至少多少次操作使得这n个数是非递减的。非递减定义: a1 ≤ a2 ≤ ... ≤ aN
思路:
这题是个大原题
原题是POJ3666题解见Here
不过这题卡空间,需要滚动数组优化一下,秒过_(:зゝ∠)_
还有一个严格递增的版本:CF 714 E 题解见Here
CF改原题真是不亦乐乎,还是要多做题啊
代码:
#include #include #include #define ll __int64using namespace std;const int N=5005;int n,a[N],b[N];ll dp[2][N];template void read(T bool F=false; for(CH=getchar();CH'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'num=num*10+CH-'0',CH=getchar()); F }void solve(){ for(int i=1; i<=n; i++){ ll mn=dp[(i-1) for(int j=1; j<=n; j++){ mn=min(mn, dp[(i-1) dp[i } } ll ans=dp[n for(int i=2; i<=n; i++){ ans=min(ans, dp[n } printf("%I64d\n",ans);}int main(){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif read(n); for(int i=1; i<=n; i++){ read(a[i]); b[i]=a[i]; } sort(b+1, b+n+1); solve(); return 0;}本文共计562个文字,预计阅读时间需要3分钟。
伪原创以下内容:
题目:传送门:.Sequence 描述:.Sequence 传送门:.Sequence 描述:.Sequence 时间限制:每题1秒,内存限制:每题64MB 输入:标准输入 输出:标准输出 小佩蒂亚非常喜欢玩游戏。并且她非常喜欢玩mos。
传送门:C.Sequence描述:C.Sequence传送门:C. Sequence
描述:
C. Sequence time limit per test 1 second memory limit per test 64 megabytes input standard input output standard outputLittle Petya likes to play very much. And most of all he likes to play the following game:
He is given a sequence ofNinteger numbers. At each step it is allowed to increase the value of any number by1or to decrease it by1. The goal of the game is to make the sequence non-decreasing with the smallest number of steps. Petya is not good at math, so he asks for your help.
The sequenceais called non-decreasing ifa1 ≤ a2 ≤ ... ≤ aNholds, whereNis the length of the sequence.
InputThe first line of the input contains single integerN(1 ≤ N ≤ 5000) — the length of the initial sequence. The followingNlines contain one integer each — elements of the sequence. These numbers do not exceed109by absolute value.
OutputOutput one integer — minimum number of steps required to achieve the goal.
Examples input 53 2 -1 2 11 output 4 input 52 1 1 1 1 output 1题意:
给你n个数,每次操作都可以使某个数加1或者减1,求至少多少次操作使得这n个数是非递减的。非递减定义: a1 ≤ a2 ≤ ... ≤ aN
思路:
这题是个大原题
原题是POJ3666题解见Here
不过这题卡空间,需要滚动数组优化一下,秒过_(:зゝ∠)_
还有一个严格递增的版本:CF 714 E 题解见Here
CF改原题真是不亦乐乎,还是要多做题啊

