标签:com color getchar anti queue == target 模拟 href
dp,注意边界
1 #include<cstdio> 2 #include<cstdlib> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<map> 7 #include<set> 8 #include<queue> 9 #include<vector> 10 #define INF 0x7f7f7f7f 11 #define pii pair<int,int> 12 #define ll long long 13 #define MAXN 805 14 using namespace std; 15 16 int read(){ 17 int x=0,f=1;char ch=getchar(); 18 while(ch<‘0‘||ch>‘9‘){if(‘-‘==ch)f=-1;ch=getchar();} 19 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 20 return x*f; 21 } 22 int n,k; 23 double f[MAXN][85]; 24 double sumA[MAXN],sumB[MAXN]; 25 double cost(int x,int y){ 26 return fabs((sumA[y]-sumA[x-1])-(sumB[y]-sumB[x-1])); 27 } 28 int main() 29 { 30 // freopen("data.in","r",stdin); 31 scanf("%d%d",&n,&k); 32 for(int i=1;i<=n;i++){ 33 double t;scanf("%lf",&t); 34 sumA[i]=sumA[i-1]+t; 35 } 36 for(int i=1;i<=n;i++){ 37 double t;scanf("%lf",&t); 38 sumB[i]=sumB[i-1]+t; 39 } 40 for(int i=1;i<=k;i++){ 41 f[0][i]=-10000000; 42 } 43 for(int i=1;i<=n;i++){ 44 f[i][0]=-10000000; 45 } 46 f[0][0]=0; 47 for(int i=1;i<=n;i++){ 48 for(int j=1;j<=k;j++){ 49 for(int q=0;q<i;q++){ 50 f[i][j]=max(f[i][j],f[q][j-1]+cost(q+1,i)); 51 } 52 } 53 } 54 printf("%.6f\n",f[n][k]); 55 return 0; 56 }
标签:com color getchar anti queue == target 模拟 href
原文地址:http://www.cnblogs.com/w-h-h/p/7745488.html