每日算法 - day 44
每日算法
those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out
那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~
2020.3.30
今天网太炸 一天都没打开cfQAQ
luogu-P1434 [SHOI2002]滑雪
重温经典好题
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 105;
int a[N][N] , f[N][N] , n , m;
int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
bool inmap(int x,int y)
{
return x >= 1 && x <= n && y >= 1 && y <= m;
}
int dfs(int x,int y)
{
if(f[x][y] != 0)return f[x][y];
int ans = 0;
for(int i = 0;i < 4 ;i ++)
{
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if(inmap(nx,ny) && a[x][y] < a[nx][ny])
{
ans = max(dfs(nx,ny),ans);
}
}
if(ans == 0)return f[x][y] = 1;
else return f[x][y] = ans + 1;
}
int main()
{
cin >> n >> m;
for(int i = 1;i <= n ;i ++)
{
for(int j = 1;j <= m ;j ++)
scanf("%d",&a[i][j]);
}
int ans = 0;
for(int i = 1;i <= n ;i ++)
{
for(int j = 1;j <= n ;j ++)
{
if(!f[i][j])
{
ans = max(dfs(i , j),ans);
}
}
}
cout << ans << endl;
return 0;
}
luogu-P2196 挖地雷
也许是我还没领悟到dp得精髓我觉得爆搜真舒服
tips:当成有向图+记忆化轻而易举AC
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 25;
int n , w[N] , f[N];
bool vis[N];
vector<int> a[N];
int path[N];
// u : 当前
//
int ans = 0;
int dfs(int u)
{
if(f[u] != 0)return f[u];
int t = 0, cur = 0;
for(int i = 0;i < a[u].size();i ++)
{
int x = dfs(a[u][i]) + w[a[u][i]];
if(x > t)
{
t = x;
cur = a[u][i];
}
}
path[u] = cur;
return f[u] = t;
}
int main()
{
cin >> n;
for(int i = 1;i <= n ;i ++)scanf("%d",&w[i]);
int x = 0;
for(int i = 1;i <= n - 1;i ++)
{
for(int j = i + 1;j <= n;j ++)
{
scanf("%d",&x);
if(x == 1){
a[i].push_back(j);
}
}
}
int ans = 0, now = 0;
for(int i = 1;i <= n ;i ++)
{
int t = dfs(i) + w[i];
if(t > ans)
{
ans = t;now = i;
}
}
cout << now << " ";
while(now <= n)
{
if(path[now]){
cout << path[now] << " ";
now = path[now];
}else break;
}
cout << endl << ans << endl;
return 0;
}