P1070: Road Game — Solution
Idea
After poring over the solutions for a long time, I understood the ideas of the big shots. One dimension is enough for this problem, but the O(n^3) time complexity is truly staggering. Alas, I'm just too weak and could only understand this one, so all I can do is write an n^3 solution.
Transition equation: F[i] = max(F[i], F[i-k] + sum - cost[curt], where i is the current time, k is the number of steps of the robot currently set up, sum is the accumulated coins, and curt is the index of the current robot. When curt = 0, curt has to be set to n (the circular property). The answer is stored in F[m].
Code
// P1070.cpp
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 2020;
int table[maxn][maxn];
int cost[maxn];
int F[maxn];
int n, m, p;
int main()
{
cin >> n >> m >> p;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> table[i][j];
for (int i = 1; i <= n; i++)
cin >> cost[i];
fill(F, F + maxn, -(1e9));
F[0] = 0;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
{
int curt = j - 1;
if (curt == 0)
curt = n;
int sum = table[curt][i];
for (int k = 1; k <= p; k++)
{
if (i - k < 0)
break;
F[i] = max(F[i], F[i - k] + sum - cost[curt]);
curt--;
if (curt == 0)
curt = n;
sum += table[curt][i - k];
}
}
cout << F[m];
return 0;
}
写到这道题,巡回一下,感谢julao
这个xc,他就是逊啦!
这个cx就是巨啦!谢谢神仙