HDU6024 Simple Recursion - Candy House

DP Preliminary -
Candy House for Girls Competition -
Ah - Simple Recursion -

This first step is still to be taken by yourself. = =

(code above)

#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 3010;
typedef long long ll;
struct node {
	ll x;
	ll c;

}p[maxn];

bool cmp(node a, node b) {
	return a.x < b.x;
	//You can do it from childhood to adulthood!


}
int main() {
	int t; while (cin >> t) {
		for (int i = 1; i <= t; i++)
		{
			cin >> p[i].x >> p[i].c;

		}
		sort(p + 1, p + 1 + t, cmp);
		//....??? to sort
		ll dp[maxn][2];// = { 0 };
		dp[1][1] = p[1].c;
		dp[1][0] = 1e13-1;
		for (int i = 2; i <= t; i++)
		{
			dp[i][0] = 1e13-1;
			dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + p[i].c;

			//If j=i, they are equal, then what?
			//Oh....he wants to say, build a classroom before i, this j is therefore enumerated from i-1 to 1 (boundary)
			//.... But if you build a classroom, all previous possibilities are hacked to death! (Seriously!)
			ll k = 0;
			for (int j = i - 1; j > 0; j--)
			{
				//After this, enumerate from back to front
				k = k + (p[j + 1].x - p[j].x)*(i - j);
				//Then each time, p[j].xp[j-1]x *(ji) is added, which is repeated several times!
				// That's right! Then this is the one to write again!
				dp[i][0] = min(dp[i][0], dp[j][1] + k);
				//If it is not built in this location...
				//...this is just for recording!!!  
				//Just list it every time and get the minimum value... In addition, you can also write a new one
				//That is so much in the front, where is the most cost-effective place to build a new one, ... can withstand the o(n2) algorithm!


			}

		}
		ll ans = min(dp[t][0], dp[t][1]);
		cout << ans << endl;

	}
	return 0;
}

Whether or not to build a classroom in this place, to ensure that each step is the best, it is also necessary to decide according to the previous
state . The critical point of the state is that the house must be built at the beginning, and then dp[i][0] is not built, dp[i][1] is built.
Then , the record in dn is the best one,
that is,
do not build dp[i][0]=min(dp[i][0]+d[i][1] )
built dp[i][1]=min(dp[i-1][0]+d[i-1][1])+ci;
(just truncate it directly... built here, It doesn't matter if the one in front is not built/the one in front is built, I just want the largest one)
and then discuss whether it is not built here, and where should it be built? Enumerate all the places that were built.
How enumerate? J decreases from big to small.
Then, dp[j][1] plus the sum from j to the middle of i, and




then there is a Small point: if it is established at j , and i is not established, then
what is the
sum j) * (nodes[j+1].x - nodes[j].x) that is, 3 is built, 1 and 2 are not built. For example , if we want to calculate the sum of x3 - x1 and x2 - x1, then because of the guarantee x is in ascending order, so sum = (x3 - x2) + 2 * (x2 - x1). Also (x3-x1)+ x2-x1= x3-x2+x2+x2-x1-x1; 












That is, if you want to get the segment between x3 and x2, and the segment between x2 and
x1,
directly store all S, then S3-S2, S2-S1,
but if it is more troublesome,


[1 2 3 ]
get Is S1 S2 S3
2-3: How many are 3.x-2.x? Is ij i is the final one?
1-3 is counted twice later...because it is ij 
plus 2 plus 2.x-1 .x (because, this is s3-s1 plus s2-s1)
(that is, s3=(s3-s2)+(s2-s1))
After the recursion, it is like this = =...
Then it is , t+=(ij)(j+1- j )
until the front j is gone....
Well, there is a boundary problem at the end
ah , ah, dynamic programming problem


[there is a little pit That is, the data may not be so given, so we need to sort first]
[Also, you have to go to the last one... For example, if you start from 0, you must go to j>=0, 1 is j>0]


[Also, For 1e5, use ll.. use ll] and
use ll for everything else!! Otherwise, there will be errors when converting








The time is getting shorter and shorter. April is almost over. The blog has almost nothing to write. It really is your spring in April = =...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324637847&siteId=291194637