10 条题解
-
0
最长不下降子序列:
这种问题都要先排序:
struct node { int x,y; bool operator<(const node &w/*引用,常量,加快速度*/) const/*返回值常量*/ //定义“<” { if(x==w.x)return y<w.y; return x<w.x; } }a[502]; sort(a+1,a+n+1);
`
#include <iostream> #include <algorithm> using namespace std; struct node { int x,y; bool operator<(const node &w) const { if(x==w.x)return y<w.y; return x<w.x; } }a[502]; int f[502][102]; int main() { int n,k; cin >> n >> k; for(int i=1;i<=n;i++) { cin >> a[i].x >> a[i].y; } sort(a+1,a+n+1); for(int i=1;i<=n;i++) { f[i][k]=1; for (int j=0;j<=k;j++) { for(int t=1;t<i;t++) { if(a[t].x>a[i].x||a[t].y>a[i].y) { continue; } int dx=abs(a[i].x-a[t].x); int dy=abs(a[i].y-a[t].y); if(j+dx+dy-1>k) { continue; } f[i][j]=max(f[i][j],f[t][j+dx+dy-1]+dx+dy); } } } int ans=0; for(int i=1;i<=n;i++) { for (int j=0;j<=k;j++) { ans=max(ans,j+f[i][j]); } } cout << ans; return 0; }
信息
- ID
- 2919
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 8
- 标签
- 递交数
- 111
- 已通过
- 36
- 上传者