bzoj1096:dp+ slope optimization

Source: Internet
Author: User

Feel the slope optimization is relatively easy, just push the process is not difficult, converted into code when too long did not play a little slow, refueling!

------------------------------------------------------------------------------------------------------

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace Std;
#define REP (i,n) for (int i=1;i<=n;i++)
#define CLR (x,c) memset (x,c,sizeof (x))
#define LL Long Long
int read () {
int X=0,f=1;char C=getchar ();
while (!isdigit (c)) {
if (c== '-') f=-1;
C=getchar ();
}
while (IsDigit (c)) {
x=x*10+c-' 0 ';
C=getchar ();
}
return x*f;
}
ll sum[1000005],cnt[1000005],f[1000005];
int x[1000005],q[1000005];
Double PD (int i,int j) {
Return (double) (f[i]+cnt[i]-(F[J]+CNT[J))/(Sum[i]-sum[j]);
}
int main () {
int N=read ();
Rep (i,n) {
X[i]=read (), Sum[i]=read (), F[i]=read ();
cnt[i]=cnt[i-1]+ (LL) (X[i]) *sum[i];
SUM[I]+=SUM[I-1];
}
int ql=0,qr=1;q[0]=0;
Rep (i,n) {
while (QL+1&LT;QR&AMP;&AMP;PD (Q[QL+1],Q[QL]) <x[i]) ql++;
int TMP=Q[QL];
f[i]+=f[tmp]+x[i]* (Sum[i]-sum[tmp])-(cnt[i]-cnt[tmp]);
while (QL+1&LT;QR&AMP;&AMP;PD (I,q[qr-1]) &LT;=PD (q[qr-1],q[qr-2])) qr--;
Q[qr++]=i;
}
printf ("%lld\n", F[n]);
return 0;
}

------------------------------------------------------------------------------------------------------

1096: [ZJOI2007] Warehouse construction time limit:10 Sec Memory limit:162 MB
submit:3224 solved:1384
[Submit] [Status] [Discuss]

Description

L The company has n factories, which are spread from high to the bottom of a mountain. , factory 1 at the summit, Factory N at the foot of the hill. As the mountain is in the inland area of the plateau (dry and less rain), l companies generally put products directly in the open air to save costs. Suddenly one day, the president of L company Mr. L received a telephone call from the meteorological department, and was told that there would be a rainstorm after three days, so Mr. L decided to urgently set up some warehouses in some factories to prevent the products from getting badly soaked. Depending on the terrain, the cost of building warehouses in different factories may be different. The first plant currently has a finished pi, and the cost of establishing a warehouse at the location of the I plant is CI. For a factory that does not have a warehouse, its products should be transported to other warehouses for storage, and since the external sales department of the company's products are located at the foot of the factory N, the product can only be transported to the mountain (that is, only to the warehouse of the larger number of factories), of course, shipping products are also cost- Suppose the cost of transporting 1 units of a product is 1. Assume that the built-in warehouse capacity is large enough to accommodate all the products. You will get the following data:? Factory I distance from Plant 1 XI (where x1=0);? Factory I currently has a quantity of finished products pi; In the factory I set up the cost of the warehouse CI; Please help L company to find a warehouse construction plan, make the total cost (construction cost + transportation cost) the smallest.

Input

The first line contains an integer n, which indicates the number of factories. The next n rows each row contains two integers in the XI, Pi, Ci, meaning title described.

Output

Contains only one integer, which is the cost to find the best solution.

Sample Input

3
0 5 10
5 3 100
9 6

Sample Output

+

HINT

In the factory 1 and plant 3 to establish warehouses, the establishment of the cost of 10+10=20, transport costs (9-5) * * = 12, the total cost of 32. If the warehouse is established only at factory 3, the establishment cost is 10, the transportation cost is (9-0) *5+ (9-5) *3=57, the total cost 67, the former excellent.

"Data Size"

For 100% of data, n≤1000000. All XI, Pi, and CI are within a 32-bit signed integer, guaranteeing that the intermediate result does not exceed the 64-bit signed integer.

Source

[Submit] [Status] [Discuss]

bzoj1096:dp+ slope optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.