EOJ:The Baric Bovine

The Baric Bovine

Time Limit: 1000MS Memory Limit: 65536K
Total Submits: 76 Accepted: 17

Description

Following up on a journal article about increasing milk production, Bessie has become the Baric Bovine by studying atmospheric pressure in order to curry favor with Farmer John.

She takes N (1 <= N <= 100) measurements conveniently named M_1 through M_N during the day (1 <= M_i <= 1,000,000); these measurements are numbered in the order in which Bessie observed them.

In order to characterize the day's atmospheric pressure readings, she is interested in finding a subset of the measurements (given by the K (1 <= K <= N) indices s_j where 1 <= s_1 < s_2 < ... < s_K <= N) that accurately reflects the entire set, i.e., limits the error as described below.

In any subset of measurements, an error is incurred for each measurement (1) before the first member of the subset, (2) between two consecutive measurements in the subset, and (3) after the last member of the subset. The total error value for a given set of s_j values is the sum of each of the individual errors.

Specifically, for all measurements whose index i is not one of the s_j values:

* if i is less than s_1, then the sample error is: 2 * | M_i - M_(s_1) |

* if i is between s_j and s_(j+1), then the sample error is | 2 * M_i - Sum(s_j, s_(j+1)) | where Sum(x, y) = M_x + M_y;

* if i is greater than s_K, then the sample error is 2 * | M_i - M_(s_K) |

Given a maximum error value E (1 <= E <= 1,000,000), determine the size of the smallest subset of measurements that produces an error of at most E.

 

Input

* Line 1: Two space-separated integers: N and E
* Lines 2..N+1: Line i+1 contains a single integer: M_i

 

Output

* Line 1: Two space-separated integers: the size of the smallest subset of measurements that produces an error of at most E and the least possible error for the subset of that size.

 

Sample Input

4 201032040

Sample Output

2 17

Hint

Choosing the second and fourth measurements is the best option, giving an error of 17. The first term's error is 2*|10-3| = 14; the third term's error is |2*20 - (3+40)| = 3.
__________________________________________________________________________________________________
题解:
动归,主要是有个预处理
 

DP[I][J]表示取了I个数,且所取的最后一个数为第J个数时的最小ERROR

dp[i][j]=min(dp[i][j],dp[i-1][k]-error[k][k]+error[k][j])(i<=j<=n,i-1<-k<j)

Error为预处理的数组,ERROR[I][J]表示以IJ为所取的最后两个数,从IN中所有不在序列中数的ERROR之和。

 

代码
1 #include<stdio.h>
2 #include<math.h>
3  #define oo 1000000000;
4  long long dp[102][102],error[102][102],m[102];
5  int i,j,k,loc,ans,e,n,e;
6  int min(int a,int b)
7 {
8 return a>b?b:a;
9 }
10 int main()
11 {
12 scanf("%d%d",&n,&e);
13 for (i=1;i<=n;i++)
14 scanf("%d",&m[i]);
15 for (i=1;i<=n;i++)
16 for (j=i;j<=n;j++)
17 for (k=i+1;k<=n;k++)
18 {
19 if (k>j) error[i][j]+=2*abs(m[j]-m[k]);
20 if (k<i) error[i][j]+=2*abs(m[i]-m[k]);
21 if (k>i&&k<j) error[i][j]+=abs(2*m[k]-m[i]-m[j]);
22 }
23 for (i=1;i<=n;i++)
24 for (j=1;j<=n;j++)
25 dp[i][j]=oo;
26 for (i=1;i<=n;i++)
27 {
28 dp[1][i]=0;
29 for (j=1;j<=n;j++)
30 dp[1][i]+=2*abs(m[i]-m[j]);
31 }
32 for (i=2;i<=n;i++)
33 for (j=i;j<=n;j++)
34 for (k=i-1;k<j;k++)
35 dp[i][j]=min(dp[i][j],dp[i-1][k]-error[k][k]+error[k][j]);
36 for (i=1;i<=n;i++)
37 {
38 ans=oo;
39 for (j=i;j<=n;j++)
40 if (dp[i][j]<ans)
41 {
42 loc=i;
43 ans=dp[i][j];
44 }
45 if (ans<=e) break;
46 }
47 printf("%d %d\n",loc,ans);
48 return 0;
49 }

 

posted on 2010-07-28 13:51  风也轻云也淡  阅读(270)  评论(0编辑  收藏  举报