数字三角形,dp入门题。
\(f_{x,y} = max(f_{x+1,y},f_{x+1,y+1}) + a_{x,y}\)
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<set>
#include<queue>
#include<vector>
#include<string>
using namespace std;
#define P system("pause");
#define B printf("Break\n");
#define A(x) cout << #x << " " << (x) << endl;
#define AA(x,y) cout << #x << " " << (x) << " " << #y << " " << (y) << endl;
#define ll long long
#define inf 1000000000
#define linf 10000000000000000
int read()
{
int x = 0,f = 1;
char c = getchar();
while(c < ‘0‘ || c > ‘9‘)
{
if(c == ‘-‘) f = -1;
c = getchar();
}
while(c >= ‘0‘ && c <= ‘9‘)
{
x = (x << 3) + (x << 1) + c - ‘0‘;
c = getchar();
}
return f * x;
}
#define N 150
int f[N][N],a[N][N],n;
int dfs(int x,int y)
{
if(f[x][y]) return f[x][y];
if(x == n) return f[n][y] = a[n][y];
else return f[x][y] = max(dfs(x + 1,y),dfs(x + 1,y + 1)) + a[x][y];
}
int main()
{
n = read();
for(int i = 1;i <= n;++i)
for(int j = 1;j <= i;++j)
a[i][j] = read();
printf("%d\n",dfs(1,1));
}
理性分析一下,发现这个人一定会跟着骑得最快的人一块骑,因此只需统计出最先到达终点的人的时间即可。注意在0时刻之前出发的,要么一定会被这个人超过,要么永远追不上,所以不予考虑。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<set>
#include<queue>
#include<vector>
#include<string>
using namespace std;
#define P system("pause");
#define B printf("Break\n");
#define A(x) cout << #x << " " << (x) << endl;
#define AA(x,y) cout << #x << " " << (x) << " " << #y << " " << (y) << endl;
#define ll long long
#define inf 1000000000
#define linf 10000000000000000
int read()
{
int x = 0,f = 1;
char c = getchar();
while(c < ‘0‘ || c > ‘9‘)
{
if(c == ‘-‘) f = -1;
c = getchar();
}
while(c >= ‘0‘ && c <= ‘9‘)
{
x = (x << 3) + (x << 1) + c - ‘0‘;
c = getchar();
}
return f * x;
}
int main()
{
int n;
while(~scanf("%d",&n) && n)
{
double ans = inf;
for(int i = 1;i <= n;++i)
{
double v,t,dt = inf;
scanf("%lf%lf",&v,&t);
if(t >= 0) dt = ceil(t + 4.5 / (v / 3600.0));
ans = min(ans,dt);
}
printf("%d\n",(int)ans);
}
}
原文:https://www.cnblogs.com/lijilai-oi/p/12759295.html