HDU 5009 Paint Pearls(西安網路賽C題)

來源:互聯網
上載者:User

標籤:style   http   color   io   os   ar   for   sp   代碼   

HDU 5009 Paint Pearls

題目連結

題意:給定一個目標顏色,每次能選一個區間染色,染色的代價為這個區間不同顏色數的平方,問最小代價

思路:先預先處理,把相同顏色的一段合并成一個點,然後把顏色離散化掉,然後進行dp,dp[i]表示染到第i個位置的代價,然後往後轉移,轉移的過程記錄下不同個數,這樣就可以轉移了,注意加個剪枝,就是如果答案大於了dp[n]就不用往後繼續轉移了

代碼:

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int N = 50005;int n, vis[N];vector<int> save;struct Point {int val, id, rank;} p[N];bool cmpv(Point a, Point b) {return a.val < b.val;}bool cmpid(Point a, Point b) {return a.id < b.id;}const int INF = 0x3f3f3f3f;int dp[N];int main() {while (~scanf("%d", &n)) {for (int i = 1; i <= n; i++)scanf("%d", &p[i].val);int pn = 1;for (int i = 2; i <= n; i++) {if (p[i].val != p[i - 1].val)p[++pn] = p[i];}n = pn;for (int i = 1; i <= n; i++)p[i].id = i;sort(p + 1, p + n + 1, cmpv);p[1].rank = 0;int s = 0;for (int i = 2; i <= n; i++) {if (p[i].val != p[i - 1].val)++s;p[i].rank = s;}sort(p + 1, p + n + 1, cmpid);memset(dp, INF, sizeof(dp));dp[0] = 0;dp[n] = n;for (int i = 0; i < n; i++) {int cnt = 0;if (dp[i] > dp[i + 1]) continue;for (int j = i + 1; j <= n; j++) {if (vis[p[j].rank] == 0) {save.push_back(p[j].rank);cnt++;}vis[p[j].rank]++;if (dp[i] + cnt * cnt >= dp[n]) break;dp[j] = min(dp[j], dp[i] + cnt * cnt);}for (int i = 0; i < save.size(); i++)vis[save[i]] = 0;save.clear();}printf("%d\n", dp[n]);}return 0;}


HDU 5009 Paint Pearls(西安網路賽C題)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.