He is a fan of \ (dp \) .
thanklove without borderand
The beginning of the two-dimensional state adopted but not on)
Then the state into a one-dimensional \ (AC \)
\(Solution\):
Status: Start thinking of \ (F [I] [J] \) , showing that the arrival \ (I \) time in \ (J \) the maximum number of coins at a point.
Transfer equation set: \ (F [I] [J] = max \ {F [I-. 1] [JK] + SUM-Val [JK] \} \) where \ (SUM \) represents the \ (JK \ ) went \ (j \) the number of earned gold
But this is wrong, because the title:
That is, a robot does not have to disappear place to buy a robot, but you can change another place to buy.
If insist designed location, it should be \ (f [i-1] [jk] \) becomes \ (max \ {f [i -1] [1,2, ...... n] \} \)
This time complexity is \ (O (n ^ 4) \) , and thought the question might be thinking \ (peach \)
On reflection, we find that the location is no use, it will remove the second dimension, \ (f [i] \) represents the time \ (i \) the maximum number of coins that can be obtained when;
Transfer equation: \ (F [I] = max \ {F [IK] + SUM-Val [D], D = (JK) \% + n-n-\} \)
He said in \ (d \) point of purchase of a robot from (d \) \ point came \ (j \) the number of points earned gold, enumerate \ (J, d \) , made \ (f [i] \ ) the maximum value of
\ (sum \) represents from \ (d \) went \ (j \) the number of coins obtained, the easiest way to solve the violence:
int pay(int a,int b,int x,int y) {
int rtn=0,j=a;
for(int i=x+1;i<=y;i++) {
rtn+=cost[j][i];
j++;
if(j>n) j=1;
}
return rtn;
}
//表示从a走到b,时间从x变成y所获得的金币数
\(Code\):
#include<bits/stdc++.h>
using namespace std;
inline int read() {
int ans=0;
char last=' ',ch=getchar();
while(ch>'9'||ch<'0') last=ch,ch=getchar();
while(ch>='0'&&ch<='9') ans=(ans<<1)+(ans<<3)+ch-'0',ch=getchar();
if(last=='-') ans=-ans;
return ans;
}
int n,m,p;
int cost[1010][1010];
int val[1010];
int f[1010];
int pay(int a,int b,int x,int y) {
int rtn=0,j=a;
for(int i=x+1;i<=y;i++) {
rtn+=cost[j][i];
j++;
if(j>n) j=1;
}
return rtn;
}
int main() {
n=read();
m=read();
p=read();
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
cost[i][j]=read();
for(int i=1;i<=n;++i)
val[i]=read();
memset(f,0x9f,sizeof(f));
f[0]=0;
for(int i=1;i<=m;++i) { // time i
for(int j=1;j<=n;++j) { // node j
for(int k=1;k<=p&&k<=i;++k) { // k
int d=j-k;
if(d<=0)
d=d%n+n;
int sum=pay(d,j,i-k,i);
f[i]=max(f[i],f[i-k]+sum-val[d]);
}
}
}
printf("%d",f[m]);
return 0;
}
This will \ (TLE \) , seeking \ (pay \) is too slow.
Since \ (k \) is a small to large enumeration, that is, only from the \ (j \) take a step back, to go back \ (p \) step, the number of coins earned just is cumulative.
Therefore, the definition of a \ (sum = 0 \) each enumeration \ (K \) when, \ (= cost + SUM [JK] [I-K +. 1] \)
#include<bits/stdc++.h>
using namespace std;
inline int read() {
int ans=0;
char last=' ',ch=getchar();
while(ch>'9'||ch<'0') last=ch,ch=getchar();
while(ch>='0'&&ch<='9') ans=(ans<<1)+(ans<<3)+ch-'0',ch=getchar();
if(last=='-') ans=-ans;
return ans;
}
int n,m,p;
int cost[1010][1010];
int val[1010];
int f[1010];
int main() {
n=read();
m=read();
p=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cost[i][j]=read();
for(int i=1;i<=n;i++)
val[i]=read();
memset(f,0x9f,sizeof(f));
f[0]=0;
for(int i=1;i<=m;i++) { // time i
for(int j=1;j<=n;j++) { // node j
int sum=0;
for(int k=1;k<=p&&k<=i;k++) { // zoule k
int d=j-k;
if(d<=0)
d=d%n+n;
sum+=cost[d][i-k+1];
f[i]=max(f[i],f[i-k]+sum-val[d]);
}
}
}
printf("%d",f[m]);
return 0;
}