#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<string.h>
#define inf 0x3f3f3f
using namespace std;
__int64 a,ans;
int m,vis[100001],day[100001],test,n,b;
int main()
{
while(scanf("%d",&test) != EOF)
{
for(int i = 1 ;i <= test ;i++)
{
scanf("%d%I64d%d",&n,&a,&b);
memset(vis,0,sizeof(vis));
for(int j = 1 ;j <= n ;j++)
scanf("%d",&day[j]);
ans = a;
m = 0;
for(int j = 1 ;j < n ;j++)
{
if((day[j+1]-day[j]-1)*b< 2*a)
vis[j] = 1;
// printf("%d %d\n",(day[j+1]-day[j]-1)*b,2*a);
// printf("%d %d\n",j,vis[j]);
}
if(n == 1)ans = 2*a+b;
else
{
for(int j = 1 ;j < n ;j++)
{
if(m == 0 && vis[j] == 1)
{
m = j;
//continue;
}
if(vis[j] == 1 && vis[j+1] == 0)
{
ans = ans + (day[j+1]-day[m]+1)*b+a;
m = 0;
continue;
}
if(vis[j] == 0 && vis[j+1] == 0)
{
ans = ans + 2*a + b;
continue;
}
if(vis[j] == 0 && vis[j+1] == 1)
{
ans =ans + a;continue;
}
}
}
printf("Case #%d: %I64d\n",i,ans);
}
}
return 0;
}
自己做的时候是分了几种情况来讨论的.后来看了解题报告,原来这个题目是一维的DP问题.哎 还是太水了啊..
今天写的DP#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<string.h>
#define inf 0x3f3f3f
using namespace std;
__int64 a,ans,dp[100001][2];
int m,vis[100001],day[100001],test,n,b;
int main()
{
while(scanf("%d",&test) != EOF)
{
for(int i = 1 ;i <= test ;i++)
{
scanf("%d%I64d%d",&n,&a,&b);
memset(vis,0,sizeof(vis));
for(int j = 1 ;j <= n ;j++)
scanf("%d",&day[j]);
dp[1][0] = a + b;
dp[1][1] = 2*a + b;
for(int j = 2 ;j < n ;j++)
{
dp[j][0] = min( dp[j-1][0]+(day[j]-day[j-1])*b,dp[j-1][1]+a+b);
dp[j][1] = min( dp[j-1][0]+(day[j]-day[j-1])*b+a,dp[j-1][1]+2*a+b );
}
printf("Case #%d: %I64d\n",i,min(dp[n-1][0]+(day[n]-day[n-1])*b+a,dp[n-1][1]+2*a+b));
}
}
return 0;
}
在这个DP里面,不用考虑要加1的情况.因为如果是连续的话,在前一次已经加上去了.其实还是有点朦朦胧胧的.