【Codeforces 713C】Sonya and Problem Wihtout a Legend

题目传送门:http://codeforces.com/contest/713/problem/C

我™之前做过这原题!然而考试的时候没想到怎么做QAQ
V5[YANBJ`4%A2`%PIH$BH_F
I well vegatable are!

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 3000+9;
const LL INF = 1e15;

int arr[N],hash[N],n,tot;
LL f[N][N],MN[N]; 

inline int read(){
	char c=getchar(); int ret=0,f=1;
	while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
	while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
	return ret*f;
}

#define update(x,y) (x=((x)==-1||(y)<(x)?(y):(x)))
#define abs(x) ((x)>0?(x):-(x))

int main(){
	n = read(); 
	for (int i=1;i<=n;i++) hash[i] = arr[i] = read() - i;
	sort(hash+1,hash+1+n); tot = unique(hash+1,hash+1+n) - hash - 1;
	for (int i=1;i<=n;i++) memset(f[i],-1,sizeof(f[i])), MN[i] = -1;
	for (int i=1;i<=n;i++) {
		LL best = INF;
		for (int j=1;j<=tot;j++) {
			if (~f[i-1][j]) best = min(best, f[i-1][j]);
			update(f[i][j],best+abs(arr[i]-hash[j]));
		}
	} 
	LL vout = INF;
	for (int i=1;i<=tot;i++) if (~f[n][i])
		vout = min(vout, f[n][i]);
	cout<<vout; 
	return 0;
}

至于为什么减去下标答案不会变?
我们可以考虑差分序列啦!

—– UPD 2016.9.14 —–
这题看策爷的代码,可以用左偏树搞到O(nlogn)

原来这个题目可以贪心!
考虑最后的样子,一定长这样:
556487545
加入一个堆,其中位数不满足整体不减的话,即与左边的堆合并
为什么这么贪心的是对的呢?因为不管你砍哪一步分开,那一部分的差距都会更大,即不可能成为最优解

18 thoughts to “【Codeforces 713C】Sonya and Problem Wihtout a Legend”

  1. I think this is one of the most important info for me.
    And i’m glad reading your article. But want to remark on some general
    things, The website style is great, the articles is really great
    : D. Good job, cheers

  2. It is in reality a nice and useful piece of information. I’m
    happy that you just shared this helpful info with us. Please keep us informed like this.
    Thank you for sharing.

  3. I loved as much as you will receive carried out right here.
    The sketch is tasteful, your authored material stylish.
    nonetheless, you command get got an nervousness over that you wish be delivering the following.
    unwell unquestionably come further formerly again since exactly the same nearly
    very often inside case you shield this hike.

  4. Hello, I think your site might be having browser compatibility issues.
    When I look at your website in Ie, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that, amazing
    blog!

  5. Hi, I do believe this is an excellent site. I stumbledupon it 😉 I’m going to return yet again since i have bookmarked it.

    Money and freedom is the best way to change, may you be rich and continue to guide other people.

  6. My partner and I stumbled over here by a different website and thought I
    might check things out. I like what I see so i am just following
    you. Look forward to looking into your web page repeatedly.

  7. You have made some good points there. I looked on the
    net for more info about the issue and found most people will go along with your views on this web site.

Leave a Reply

Your email address will not be published. Required fields are marked *