#include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x)); int tra[105][105]; intdfs(int x,int y) { if (x==1 && y==1) return tra[1][1]; if (y==1) return dfs(x-1,y)+tra[x][y]; elseif (x==y) return dfs(x-1,y-1)+tra[x][y]; elsereturn max(dfs(x-1,y),dfs(x-1,y-1))+tra[x][y]; } intmain() { int n; while (cin >> n) { for (int i=1;i<=n;i++) for (int j=1;j<=i;j++) scanf("%d",&tra[i][j]); int ret=-INF; for (int i=1;i<=n;i++) ret = max(ret, dfs(n, i)); cout << ret << endl; } }
#include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x)); int a[1010]; int ret[1010]; intmain() { int n; ret[1]=1; while (cin >> n) { for (int i=1;i<=n;i++) cin >> a[i]; memset(ret, 0, sizeof(ret)); ret[1]=1; int ans=1; for (int i=2;i<=n;i++) { for (int j=i-1;j>=1;j--) if (a[j]<a[i]) ret[i] = max(ret[i],ret[j]+1); if (ret[i]==0) ret[i]=1; ans = max(ans,ret[i]); } cout << ans << endl; } }
D004 最长公共子序列
Problem Description
我们称序列Z = < z1, z2, …, zk >是序列X = < x1, x2, …, xm >的子序列当且仅当存在严格上升的序列< i1, i2, …, ik >,使得对j = 1, 2, … ,k, 有xij = zj。比如Z = < a, b, f, c > 是X = < a, b,c, f, b, c >的子序列。 现在给出两个序列X和Y,你的任务是找到X和Y的最大公共子序列,也就是说要找到一个最长的序列Z,使得Z既是X的子序列也是Y的子序列。
#include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x)); int a[105][105]; int s[105]; int sub[105]; int n; intsubmax() { int ret = s[1]; sub[1] = s[1]; for (int i=2;i<=n;i++) { if (sub[i-1]<0) { sub[i] = s[i]; } else sub[i] = sub[i-1]+s[i]; ret = max(ret,sub[i]); } return ret; } intmain() { int ans; while (cin >> n) { ans = -INF; for (int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&a[i][j]); for (int i=1;i<=n;i++) { memset(s, 0, sizeof(s)); for (int j=i;j<=n;j++) { for (int k=1;k<=n;k++) s[k]+=a[j][k]; ans = max(ans,submax()); } } cout << ans << endl;; } }