HDU 5009 Paint Pearls(西安网络赛C题)

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;
}
时间: 2024-10-20 17:23:06

HDU 5009 Paint Pearls(西安网络赛C题)的相关文章

HDU 5011 Game(西安网络赛E题)

HDU 5011 Game 题目链接 思路:其实就求一个Nim和即可,要推也不难推,和为0下一个必然是胜态,因为至少取走一个,在怎么分也达不到原来那个值了,如果是非0值,就和原来Nim一样必然可以取一堆使得变成0 代码: #include <cstdio> #include <cstring> const int N = 100005; int n; long long a, sum; int main() { while (~scanf("%d", &

HDU 5017 Ellipsoid(西安网络赛K题)

HDU 5017 Ellipsoid 题目链接 思路:模拟退火大法好! 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int D[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; dou

HDU 5009 Paint Pearls _(:зゝ∠)_2014 ACM/ICPC Asia Regional Xi&#39;an Online

呵呵 #include <cstdio> #include <algorithm> #include <iostream> #include <cstring> typedef long long ll; using namespace std; const int N = 5 * 10000 + 5; int xval[N], dep; int n, a[N], pre[N]; ll d[N]; int pos[300], dd; void work()

HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化

转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 Accepted 5009 1265MS 1980K 2290 B G++ czy   Paint Pearls Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Subm

HDU 5008西安网络赛B题:后缀数组求第k小子串

思路:尼玛,这题搞了一天了,比赛的时候用了n^2的方法绝对T了,然后今天看别人代码看了一天才知道.后面感觉也挺容易的,就是没想到,之前做过SPOJ 694 705求过不同子串了,知道怎么求不同子串个数了,但是比赛的时候这个技巧竟然抛在脑后了,然后就不会了. 但是今天自己用了自己的两个后缀数组的模板(倍增和DC3)的都WA了,搞得自己真想跳楼去了!! 到现在都不知道到底是哪里错了,处理的方法和标准做法都一样,但是就是WA,然后用了别人的模板,再用自己的处理方法就过了,怀疑自己的两个模板是不是哪里错

HDU 5009 Paint Pearls (动态规划)

Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans to color the pearls to make it more fascinating. He drew his ideal pattern of the string on a paper and asks for your help. In eac

HDU 5009 Paint Pearls 双向链表优化DP

Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans to color the pearls to make it more fascinating. He drew his ideal pattern of the string on a paper and asks for your help. In eac

hdu 5009 Paint Pearls (动态规划)

Paint Pearls Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2359    Accepted Submission(s): 761 Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color

HDU 5003 Osu!(鞍山网络赛G题)

HDU 5003 Osu! 题目链接 就一签到题,排序之后for一遍计算出答案即可 代码: #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <vector> #include <set> #include <map> #include <algorithm> #include <cmat