【Codeforces 711C】Coloring Trees

题目传送门:http://codeforces.com/contest/711/problem/C
数据生成器:http://paste.ubuntu.com/23124114/
官方数据:http://codeforces.com/blog/entry/46830

O(n^4)的DP还是很好想,但这货的细节比较多,我调了3个小时FS~3Z]@~(4B0{S~)AKZ2G88
然后是可以优化到O(n^3),因为只有相不相等才影响答案,所以只用记录最小的两种颜色即可
嘴巴上AC还是很容易的,但代码写起来更恶心,所以弃疗QRRXCIEEY4QTA4`}]CL@RVL

#include<bits/stdc++.h>
#define LL long long
#define abs(x) ((x)>0?(x):-(x))
using namespace std;

const int N = 100+9;
const int INF = 1000000000+9;

int q[N][N],n,m,K,arr[N],cur,w,p;
LL f[2][N][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;
}

inline void update(LL &w, LL sta) {
	if (!w || sta < w) w = sta;
}

int main(){
	n = read(); m = read(); K = read();
	for (int i=1;i<=n;i++) {
		arr[i] = read();
		if (!arr[i-1]) cur += (arr[i] > 0);
		else cur += (arr[i] != arr[i-1] && arr[i] != arr[i+1]);
	} 
	for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) q[i][j] = read();
	if (!arr[1]) for (int j=1;j<=m;j++) { 
		if (j != arr[2]) f[p][j][cur+1] = q[1][j]+1; 
		else f[p][j][cur] = q[1][j]+1;
	} else f[p][arr[1]][cur] = 1; w = 1;
	for (int i=2;i<=n;i++,w^=1,p^=1) {
		memset(f[w],0,sizeof(f[w]));
		if (arr[i]) for (int j=1;j<=m;j++) for (int k=1;k<=n;k++) 
			{if (f[p][j][k]) update(f[w][arr[i]][k],f[p][j][k]);}
		else for (int j=1;j<=m;j++) {
			for (int k=1;k<=n;k++) for (int t=1;t<=m;t++) if (f[p][t][k])
				if (j != t) update(f[w][j][k+(j != arr[i+1])],f[p][t][k] + q[i][j]);
				else update(f[w][j][k-(j == arr[i+1])],f[p][t][k] + q[i][j]);
		}
	} 
	LL vout = 0; 
	for (int i=1;i<=m;i++) if (f[p][i][K])
		update(vout, f[p][i][K]);
	if (!vout && cur != K) cout<<-1;
	else cout<<vout-1;
	return 0;
}

85 thoughts to “【Codeforces 711C】Coloring Trees”

  1. I am really impressed with your writing skills and also with the layout on your weblog.
    Is this a paid theme or did you modify it yourself? Either
    way keep up the nice quality writing, it’s rare to see a great blog like this one
    these days.

  2. whoah this blog is magnificent i like reading your articles.
    Keep up the good work! You recognize, many individuals are searching round for this information, you could aid
    them greatly.

  3. Thank you, I have recently been searching for info about this subject for ages
    and yours is the greatest I have discovered till now. But, what concerning the conclusion? Are you
    positive concerning the source?

  4. Very good website you have here but I was wanting to know if you knew of any message boards
    that cover the same topics talked about here?
    I’d really like to be a part of group where I can get opinions from other
    experienced individuals that share the same interest. If you have any
    recommendations, please let me know. Bless you!

  5. With havin so much content and articles do you ever run into any
    problems of plagorism or copyright violation? My website has a lot of exclusive
    content I’ve either written myself or outsourced but it appears a lot of it
    is popping it up all over the web without my authorization. Do you know any solutions to help reduce content from being ripped off?
    I’d definitely appreciate it.

  6. Hello, Neat post. There is a problem along with your
    website in internet explorer, could check this? IE still is the marketplace leader and a huge
    component to other folks will pass over your magnificent writing because of this problem.

  7. Generally I don’t learn article on blogs, but I
    would like to say that this write-up very forced me to
    check out and do so! Your writing taste has been amazed me.
    Thank you, very great article.

  8. Wow, superb blog structure! How lengthy have you ever been blogging for?
    you made blogging glance easy. The total look of your web site is magnificent, let
    alone the content!

  9. I’d like to thank you for the efforts you’ve put
    in writing this blog. I am hoping to see the same high-grade blog posts from you in the future as well.
    In fact, your creative writing abilities has encouraged me
    to get my very own website now 😉

  10. Hey there, I think your site might be having browser compatibility issues.
    When I look at your website in Safari, 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, wonderful blog!

  11. of course like your web site however you need to test the spelling
    on several of your posts. A number of them are rife
    with spelling problems and I to find it very bothersome to inform the truth
    on the other hand I will definitely come again again.

  12. Hello I am so glad I found your weblog, I really found you by accident, while I was browsing on Askjeeve for something else,
    Nonetheless I am here now and would just like to say thank you for a tremendous post and a
    all round exciting blog (I also love the theme/design),
    I don’t have time to look over it all at the moment but I have
    bookmarked it and also added in your RSS feeds, so when I have time I
    will be back to read a lot more, Please do keep up the fantastic
    work. plenty of fish natalielise

  13. Good site you have got here.. It’s hard to find high-quality writing like yours nowadays.

    I honestly appreciate people like you! Take care!!

  14. Hey there! I know this is kind of off topic but I was wondering which blog platform are you using
    for this website? I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking at options for
    another platform. I would be fantastic if you could point me in the direction of a good platform.

  15. Thanks for your marvelous posting! I really enjoyed reading it,
    you’re a great author. I will remember to bookmark
    your blog and will eventually come back at some point. I want to encourage you to
    definitely continue your great posts, have a nice morning!

  16. It’s appropriate time to make some plans for the future and it’s time
    to be happy. I’ve read this post and if I could I want to suggest you
    few interesting things or advice. Maybe you could write next articles referring to this article.
    I want to read even more things about it!

  17. Heya i’m for the first time here. I found this board and I to find It truly useful & it helped me out
    a lot. I’m hoping to provide something back and help others like you helped me.

  18. you’re actually a just right webmaster. The web site loading pace is amazing.
    It sort of feels that you are doing any distinctive trick.
    Furthermore, The contents are masterpiece. you have performed a wonderful activity in this subject!

  19. I’m pretty pleased to uncover this site. I
    need to to thank you for your time for this fantastic read!!
    I definitely really liked every part of it and i also have you saved to fav to see new
    things in your web site.

  20. Hi! I know this is somewhat off-topic but I needed to ask.

    Does running a well-established website like yours take
    a large amount of work? I am brand new to blogging however I do write in my diary daily.

    I’d like to start a blog so I can easily share my personal experience
    and feelings online. Please let me know if you have any ideas or tips for new aspiring
    blog owners. Thankyou!

  21. hello there and thank you for your information – I have certainly picked up anything new from right here.
    I did however expertise several technical issues using this web site, as
    I experienced to reload the site a lot of times previous to I could get it to
    load correctly. I had been wondering if your web host is OK?
    Not that I’m complaining, but slow loading instances times will
    sometimes affect your placement in google and can damage your quality score
    if ads and marketing with Adwords. Anyway I’m adding this
    RSS to my email and can look out for a lot more of your respective exciting content.

    Ensure that you update this again soon.

  22. I know this if off topic but I’m looking into
    starting my own blog and was curious what all is needed to get setup?
    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very web savvy so I’m not 100% sure. Any suggestions or advice would be greatly appreciated.
    Thanks

  23. Just want to say your article is as amazing. The clarity in your post is just great and i could assume you are an expert on this subject.
    Well with your permission let me to grab your RSS feed to
    keep up to date with forthcoming post. Thanks a million and please keep up the rewarding work.

  24. I’m amazed, I have to admit. Rarely do I encounter a blog that’s
    both equally educative and interesting, and without a doubt, you have hit the nail
    on the head. The problem is something which too few people are
    speaking intelligently about. I’m very happy I came across this during my
    hunt for something concerning this.

  25. After I originally left a comment I seem to have clicked on the -Notify me
    when new comments are added- checkbox and now whenever a comment is added I get 4 emails with the same comment.
    There has to be a way you are able to remove me from that
    service? Appreciate it!

  26. Today, I went to the beach with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell
    to her ear and screamed. There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is completely
    off topic but I had to tell someone!

  27. Do you have a spam issue on this blog; I also am
    a blogger, and I was wondering your situation; we have created some nice procedures and we are looking to swap methods with other folks,
    please shoot me an e-mail if interested.

  28. What’s Taking place i’m new to this, I stumbled
    upon this I have found It positively useful and it has helped me out loads.
    I am hoping to contribute & assist other customers like its helped me.
    Great job.

  29. Great post. I was checking constantly this blog and I’m impressed!
    Very useful info specially the remaining part :
    ) I care for such information much. I used to be seeking this particular information for a long time.
    Thank you and good luck.

  30. Great weblog here! Additionally your website loads up very fast!
    What host are you the usage of? Can I get your associate hyperlink on your host?
    I desire my web site loaded up as quickly as yours lol

  31. Hello, i read your blog from time to time and i own a similar one and i was just curious if you get a lot of
    spam feedback? If so how do you prevent it, any plugin or anything you
    can suggest? I get so much lately it’s driving me insane so any assistance is very much appreciated.

  32. Link exchange is nothing else however it is only placing
    the other person’s web site link on your page at appropriate place and other person will also do similar in support of you.

  33. You could certainly see your skills in the work you write.
    The arena hopes for more passionate writers like you who are not
    afraid to say how they believe. All the time go after your heart.

  34. Today, while I was at work, my sister stole my iphone and tested to see if it can survive a twenty five foot drop,
    just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views.
    I know this is completely off topic but I had to share it with someone!

  35. Hey I know this is off topic but I was wondering if you
    knew of any widgets I could add to my blog that automatically tweet my newest
    twitter updates. I’ve been looking for a plug-in like this
    for quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading
    your blog and I look forward to your new updates.

  36. Very nice post. I just stumbled upon your blog and wished to say that I have really enjoyed surfing around your blog posts.
    In any case I will be subscribing to your rss feed and
    I hope you write again soon!

  37. Link exchange is nothing else but it is simply placing the other person’s webpage link
    on your page at suitable place and other person will also do
    same in support of you.

  38. Heya i am for the first time here. I came across this board and
    I to find It truly helpful & it helped me out much. I hope to
    offer one thing again and aid others such as you aided me.

  39. I was wondering if you ever thought of changing the page
    layout of your site? Its very well written; I love what youve
    got to say. But maybe you could a little more in the way of content so people could connect with it
    better. Youve got an awful lot of text for only having
    one or 2 images. Maybe you could space it out better?

  40. Great weblog right here! Additionally your website a lot up fast!
    What host are you the use of? Can I am getting your affiliate hyperlink for your host?
    I want my website loaded up as fast as yours lol

  41. Hey I know this is off topic but I was wondering if you knew
    of any widgets I could add to my blog that automatically tweet
    my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe
    you would have some experience with something like this. Please let
    me know if you run into anything. I truly enjoy reading your blog
    and I look forward to your new updates.

Leave a Reply

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