# [Noip模拟赛] Power

POWER

POWER.IN

4

3

2 2

5 8

6 1

8 7

POWER.OUT

56

【题解】

f[i,j]表示关闭[i,j]且停在i的min
g[i,j]表示关闭[i,j]且停在j的min
h[i,j]表示除了[i,j]其他功率

f[i][j]=min(f[i+1][j]+h[i+1][j]*(d[i+1]-d[i]),g[i+1][j]+h[i+1][j]*(d[j]-d[i]));
g[i][j]=min(f[i][j-1]+h[i][j-1]*(d[j]-d[i]),g[i][j-1]+h[i][j-1]*(d[j]-d[j-1]));

 1 #include <stdio.h>
2 #include <string.h>
3 using namespace std;
4 long long f[1010][1010],g[1010][1010],h[1010][1010],d[1010],w[1010],p[1010];
5 // f i,j表示关闭[i,j]且停在i的min
6 // g i,j表示关闭[i,j]且停在j的min
7 // h i,j表示除了[i,j]其他功率
8 int n,st;
9 inline void IO() {
10     freopen("power.in","r",stdin);
11     freopen("power.out","w",stdout);
12 }
13 inline int min(int a,int b) {return a<b?a:b;}
14 inline void RS() {
15     scanf("%d%d",&n,&st);
16     for (int i=1;i<=n;++i) {
17         scanf("%d%d",&d[i],&w[i]);
18         p[i]=p[i-1]+w[i];
19     }
20     for (int i=1;i<=n;++i)
21         for (int j=i;j<=n;++j)
22              h[i][j]=p[n]+p[i-1]-p[j];
23     for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) {
24         f[i][j]=g[i][j]=2333333333333;
25     }
26     f[st][st]=g[st][st]=0;
27     for (int j=st;j<=n;++j)
28         for (int i=j-1;i>=1;--i) {
29             f[i][j]=min(f[i+1][j]+h[i+1][j]*(d[i+1]-d[i]),g[i+1][j]+h[i+1][j]*(d[j]-d[i]));
30             g[i][j]=min(f[i][j-1]+h[i][j-1]*(d[j]-d[i]),g[i][j-1]+h[i][j-1]*(d[j]-d[j-1]));
31         }
32     printf("%lld\n",min(f[1][n],g[1][n]));
33 }
34 int main() {
35     IO(),
36     RS();
37     return 0;
38 }
View Code

posted @ 2015-07-08 08:29  TonyFang  阅读(497)  评论(0编辑  收藏  举报