目录补一下题,
- c的状态方程都列出来了, 但没想通
- d只剩几个样例没过没验出来,
应该是连通性的问题,刚才换了个写法,都初始化成0x3f过了
C. 小竹关禁闭
C. 小竹关禁闭
DP
#include<iostream>
using namespace std;
const int N = 2010;
int dp[N];
int a[N];
inline int fmax(int a, int b) {
return a < b ? b : a;
}
int main()
{
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= n; ++i){
if(i < k+1) dp[i] = fmax(dp[i-1], a[i]);
else dp[i] = fmax(dp[i-1], dp[i-k-1]+a[i]);
}
int res = -1;
for(int i = 1; i <= n; ++i)
res = fmax(res, dp[i]);
cout << res;
return 0;
}
D 游戏购买!
D 游戏购买!
BFS
#include<iostream>
#include<queue>
#include<cstring>
#define x first
#define y second
using namespace std;
const int N = 2010;
typedef pair<int, int> PII;
int n, m, x;
int sx, sy, ex, ey;
int g[N][N];
int ds[N][N], de[N][N], d[N][N]; // 距离起点, 终点的距离
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
void e_bfs(){
memset(d, -1, sizeof d);
queue<PII> q;
d[ex][ey] = 0;
q.push({ex, ey});
while(!q.empty())
{
PII t = q.front();
q.pop();
for(int i = 0; i < 4; ++i) {
int nx = t.x+dx[i], ny = t.y+dy[i];
if(!nx || !ny || nx > n || ny > m || g[nx][ny]==-1) continue;
//已访问, 跳过
if(d[nx][ny] != -1) continue;
q.push({nx, ny});
d[nx][ny] = d[t.x][t.y]+1;
if(g[nx][ny] > x) de[nx][ny] = d[nx][ny];
}
}
}
long long s_bfs(){
memset(d, -1, sizeof d);
queue<PII> q;
d[sx][sy] = 0;
q.push({sx, sy});
long long res = 0x3f3f3f3f;
while(!q.empty())
{
PII t = q.front();
q.pop();
if(g[t.x][t.y] > x) res = min(res, (long long)(ds[t.x][t.y]+de[t.x][t.y]));
for(int i = 0; i < 4; ++i) {
int nx = t.x+dx[i], ny = t.y+dy[i];
if(!nx || !ny || nx > n || ny > m || g[nx][ny]==-1) continue;
//已访问, 跳过
if(d[nx][ny] != -1) continue;
q.push({nx, ny});
d[nx][ny] = d[t.x][t.y]+1;
if(g[nx][ny] > x) ds[nx][ny] = d[nx][ny];
}
}
return res == 0x3f3f3f3f ? -1 : res;
}
int main()
{
memset(ds, 0x3f, sizeof ds);
memset(de, 0x3f, sizeof de);
scanf("%d%d%d", &n, &m, &x);
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
scanf("%d", &g[i][j]);
//终点
e_bfs();
printf("%lld", s_bfs());
}
标签:nx,ny,int,res,d%,long,60,牛客,小白月赛
From: https://www.cnblogs.com/Knight02/p/niu-xiaobai60.html