acwing 第 130 场周赛 (前缀和,dfs,对不同边的处理)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<climits> using namespace std; typedef long long LL; const int N = 5010; int n; int a[N]; LL s[N]; LL get(int l, int r) { return s[r - 1] - s[l - 1]; } int main() { cin.tie(0); cin >> n; for(int i = 1; i <= n; i ++ ) cin >> a[i]; for(int i = 1; i <= n; i ++ ) s[i] = s[i - 1] + a[i]; LL mx = LONG_LONG_MIN; int x = 1, y = 1, z = 1; for(int yy = 1; yy <= n + 1; yy ++ ) { int tx = 1, tz = yy; LL res1 = LONG_LONG_MIN, res2 = LONG_LONG_MIN; for(int xx = 1; xx <= yy; xx ++ ) { auto tt = get(1, xx) - get(xx, yy); if(res1 < tt) { res1 = tt; tx = xx; } } for(int zz = yy; zz <= n + 1; zz ++ ) { auto tt = get(yy, zz) - get(zz, n + 1); if(res2 < tt) { res2 = tt; tz = zz; } } if(mx < res1 + res2) { mx = res1 + res2; x = tx, y = yy, z = tz; } } cout << x - 1 << ' ' << y - 1 << ' ' << z - 1 << endl; return 0; }
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<climits> #include<cmath> using namespace std; const int N = 3e5 + 10, M = N * 2; int n, m, s; int h[N], e[M], ne[M], w[M], idx; bool st[N]; int ans[N]; void add(int a, int b, int c) { e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++ ; } int dfs(int u, int type) { int res = 1; st[u] = true; for(int i = h[u]; ~i; i = ne[i]) { int j = e[i]; if(st[j]) continue; if(type == 0) { res += dfs(j, type); if(w[i]) ans[abs(w[i])] = w[i]; } else { if(w[i]) ans[abs(w[i])] = -w[i]; else res += dfs(j, type); } } return res; } int main() { cin.tie(0); memset(h, -1, sizeof h); cin >> n >> m >> s; for(int i = 1; i <= m; i ++ ) { int t, a, b; cin >> t >> a >> b; if(t == 1) add(a, b, 0); else add(a, b, i), add(b, a, -i), ans[i] = i; } printf("%d\n", dfs(s, 0)); for(int i = 1; i <= m; i ++ ) if(ans[i] > 0) printf("+"); else if(ans[i] < 0) printf("-"); puts(""); memset(st, 0, sizeof st); printf("%d\n", dfs(s, 1)); for(int i = 1; i <= m; i ++ ) if(ans[i] > 0) printf("+"); else if(ans[i] < 0) printf("-"); puts(""); return 0; }