- 问答
- 2016-05-25 16:40:18 @
4 条评论
-
-------- LV 8 @ 2016-06-02 13:52:13
#include<iostream> using namespace std; int shu[5001][5001]={0}; int dp[5001],Max[5001]; int n,h,delta,p,q; int main() { cin>>n>>h>>delta; for(int i=1;i<=n;i++) { cin>>p; for(int j=1;j<=p;j++) { cin>>q; shu[i][q]++; } } for(int y=h;y>=1;y--) { for(int k=1;k<=n;k++) { dp[k]=max(dp[k]+shu[k][y],shu[k][y]+Max[y+delta]); if(dp[k]>=Max[y]) Max[y]=dp[k]; } } cout<<Max[1]; return 0; }
-
2016-05-29 21:06:23@
DP:
f[i][j]表示高度为i在第j棵树上的情况
mx[i]表示该层最大值
转移方程:f[i][j]=max(mx[i+delta],f[i+1][j])+a[j][i]
Code:
```C++
//
// main.cpp
// 雷涛的小猫
//
// Created by stneng on 16/5/26.
// Copyright © 2016年 stneng. All rights reserved.
//#include <cstdio>
const int N=5000;
int n,h,delta,nn[N],a[N][N],f[N][N],mx[N];
inline int read(){
int x=0;char ch=getchar();
while (ch>'9'||ch<'0') ch=getchar();
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
int main() {
// insert code here...std::cout << "Hello, World!\n";
n=read();h=read();delta=read();
int x;
for (int i=1;i<=n;i++) {
nn[i]=read();
for (int j=1;j<=nn[i];j++) {
x=read();
a[i][x]++;
}
}
for (int i=h;i>=1;i--)
for (int j=1;j<=n;j++){
f[i][j]=mx[i+delta]>f[i+1][j]?mx[i+delta]:f[i+1][j];
f[i][j]+=a[j][i];
if (f[i][j]>mx[i]) mx[i]=f[i][j];
}
printf("%d\n",mx[1]);
return 0;
}
``` -
2016-05-29 21:06:21@
DP:
f[i][j]表示高度为i在第j棵树上的情况
mx[i]表示该层最大值
转移方程:f[i][j]=max(mx[i+delta],f[i+1][j])+a[j][i]
Code:
```C++
//
// main.cpp
// 雷涛的小猫
//
// Created by stneng on 16/5/26.
// Copyright © 2016年 stneng. All rights reserved.
//#include <cstdio>
const int N=5000;
int n,h,delta,nn[N],a[N][N],f[N][N],mx[N];
inline int read(){
int x=0;char ch=getchar();
while (ch>'9'||ch<'0') ch=getchar();
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
int main() {
// insert code here...std::cout << "Hello, World!\n";
n=read();h=read();delta=read();
int x;
for (int i=1;i<=n;i++) {
nn[i]=read();
for (int j=1;j<=nn[i];j++) {
x=read();
a[i][x]++;
}
}
for (int i=h;i>=1;i--)
for (int j=1;j<=n;j++){
f[i][j]=mx[i+delta]>f[i+1][j]?mx[i+delta]:f[i+1][j];
f[i][j]+=a[j][i];
if (f[i][j]>mx[i]) mx[i]=f[i][j];
}
printf("%d\n",mx[1]);
return 0;
}
``` -
2016-05-25 16:44:53@
大神帮忙
- 1