这种简单的斜率都写不明白了Harbin regional hdu3669

我的假期基本残废了= =!尼玛做管理员太累了……自己的训练基本停滞了……提高太少了,我的梦想还能实现吗……

贴的代码完事吧

HDU 3669
 1 #include <iostream>
2 #include <cstring>
3 #include <cstdio>
4 #include <algorithm>
5 using namespace std;
6 typedef long long LL;
7 const int N = 500011, K = 101;
8 LL dp[K][N];
9 int q[N], head, tail;
10 struct person
11 {
12 int h, w;
13 bool operator < (const person& a)const
14 {
15 return h == a.h ? w < a.w : h > a.h;
16 }
17 } persons[N];
18 LL DP(int n, int m)
19 {
20 head = tail = 0;
21 for(int i = 1; i <= n; i++)
22 {
23 dp[1][i] = 1ll * persons[i].w * persons[1].h;
24 }
25 for(int j = 2; j <= m; j++)
26 {
27 head = tail = 0;
28 q[tail++] = j - 1;
29 for(int i = j;i <= n; i++)
30 {
31 while(head + 1 < tail)
32 {
33 int p1 = q[head], p2 = q[head + 1];
34 LL y1 = dp[j - 1][p1], y2 = dp[j - 1][p2];
35 LL x1 = persons[p1 + 1].h, x2 = persons[p2 + 1].h;
36 if(y1 - y2 + persons[i].w * (x1 - x2) >= 0)
37 head++;
38 else
39 break;
40 }
41 int p = q[head];
42 dp[j][i] = dp[j - 1][p] + 1ll * persons[p + 1].h * persons[i].w;
43 while(head + 1 < tail)
44 {
45 int p1 = q[tail - 2], p2 = q[tail - 1], p3 = i;
46 LL y1 = dp[j - 1][p1], y2 = dp[j - 1][p2], y3 = dp[j - 1][p3];
47 LL x1 = persons[p1 + 1].h, x2 = persons[p2 + 1].h, x3 = persons[p3 + 1].h;
48 if((y1 - y2) * (x2 - x3) > (y2 - y3) * (x1 - x2))
49 break;
50 else
51 tail--;
52 }
53 q[tail++] = i;
54 }
55 }
56 LL ret = dp[1][n];
57 for(int i = 2;i <= m;i++)
58 ret = min(ret, dp[i][n]);
59 return ret;
60 }
61 int main()
62 {
63 int n, m;
64 while(scanf("%d %d", &n, &m) == 2)
65 {
66 for(int i = 1; i <= n; i++)
67 scanf("%d%d",&persons[i].w, &persons[i].h);
68 sort(persons + 1, persons + n + 1);
69 int i = 1, j = 0;
70 for(; i <= n; i++)
71 {
72 if(j == 0 || persons[i].w > persons[j].w)
73 persons[++j] = persons[i];
74 }
75 cout << DP(j, m) << endl;
76 }
77 return 0;
78 }

你可能感兴趣的:(HDU)