按点赞数降序
297 二分
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 1e5 + 5; int a[N], c, n; bool check(int d) { int pre = a[1], cnt = 1; for(int i = 2; i <= n; i++) { if((a[i] - pre) >= d) { pre = a[i]; cnt++; } } return cnt >= c; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &c); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a+1, a+1+n); int l = 1, r = 1e9+5, m = (l+r) >> 1; while(l < r) { if(check(m)) l = m; else r = m - 1; m = (l+r+1) >> 1; //cout << l <<" " << r << endl; } printf("%d\n", m); } return 0; }
2 区间素数筛
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 1e5 + 5; bool notp_big[N], notp_small[N]; void prime(LL a, LL b) { mem(notp_big, false); if(a==1) notp_big[0] = true; for (LL i = 2; i*i <= b; i++) { if(!notp_small[i]) { for (LL j = i+i; j*j <= b; j += i) notp_small[j] = true; for (LL j = max(2LL, (a+i-1)/i)*i; j <= b; j += i)notp_big[j-a] = true; } } } int main() { int T, n, m; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); prime(n, m); for (int i = n; i <= m; i++) if(!notp_big[i-n])printf("%d\n", i); } return 0; }
346 dp 小于5e6预处理,大于5e6递归
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 5e6+5; int dp[N]; map<int, LL>mp; LL f(int n) { if(n <= 5e6) return dp[n]; else if(mp[n] != 0) return mp[n]; return max((LL)n, f(n/2)+f(n/3)+f(n/4)); } int main() { int n; dp[0]=0; dp[1]=1; for (int i = 2; i < N; i++) { dp[i] = max(i, dp[i/2]+dp[i/3]+dp[i/4]); } while(~ scanf("%d", &n)) { mp.clear(); printf("%lld\n", f(n)); } return 0; }
394 dp 注意0的情况
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 5555; char s[N]; LL dp[N]; int main() { while(~scanf("%s", s+1)) { //printf("%s\n", s+1); if(s[1] == '0')break; dp[0] = dp[1] = 1; int n = strlen(s+1); for (int i = 2; i <= n; i++) { if(s[i-1] != '0') { int t = (s[i-1]-'0')*10 + s[i]-'0'; //cout << t << endl; if(t <= 26) { if(s[i] != '0')dp[i] = dp[i-1] + dp[i-2]; else dp[i] = dp[i-2]; } else dp[i] = dp[i-1]; } else dp[i] = dp[i-1]; } printf("%lld\n", dp[n]); } return 0; }
1043 线段数区间合并,lv记录这段区间以左端点为起点的最大和,rv同理
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 5e4 + 5; const int INF = 0x7f7f7f7f; struct Tree { int sum, lv, rv, ans; }tree[N<<2]; void push_up(int rt) { tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum; tree[rt].lv = max(tree[rt<<1].lv, tree[rt<<1].sum + tree[rt<<1|1].lv); tree[rt].rv = max(tree[rt<<1|1].rv, tree[rt<<1|1].sum + tree[rt<<1].rv); tree[rt].ans = max(tree[rt<<1].rv + tree[rt<<1|1].lv, max(tree[rt<<1].ans, tree[rt<<1|1].ans)); } void build(int rt, int l, int r) { if(l == r) { scanf("%d", &tree[rt].ans); tree[rt].lv = tree[rt].rv = tree[rt].sum = tree[rt].ans; return ; } int m = (l+r) >> 1; build(ls); build(rs); push_up(rt); } Tree query(int L, int R, int rt, int l, int r) { if(L <= l && r <= R) return tree[rt]; int m = (l + r) >> 1; if(R <= m) return query(L, R, ls); else if(L > m) return query(L, R, rs); Tree lft = query(L, R, ls); Tree rit = query(L, R, rs); int sum = lft.sum+rit.sum; int lv = max(lft.lv, lft.sum+rit.lv); int rv = max(rit.rv, rit.sum+lft.rv); int ans = max(lft.rv+rit.lv, max(lft.ans, rit.ans)); return Tree{sum, lv, rv, ans}; } int main() { int n, m, x, y; scanf("%d", &n); build(1, 1, n); scanf("%d", &m); while(m --) { scanf("%d%d", &x, &y); Tree res = query(x, y, 1, 1, n); printf("%d\n", res.ans); } return 0; }
6256 普通的逆序数,不过我使用了最近学的方法,从大到小加入位置,跑的比较优秀
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 2e5 + 5; pii a[N]; int bit[N], n; void add(int x, int a) { while(x <= n) bit[x] += a, x += x&-x; } int query(int x) { int ans = 0; while(x) ans += bit[x], x -= x&-x; return ans; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i].fi); a[i].se = i; } sort(a+1, a+1+n); mem(bit, 0); LL ans = 0; for(int i = n; i >= 1; i--) { ans += query(a[i].se); //cout <<".............."<< ans << endl; add(a[i].se, 1); } printf("%lld\n", ans); } return 0; }
5 纯暴力,从中间加
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 1e6 + 10; char s[N]; int num[N], t[N], n; bool cmp() { for(int i = 1;i <= n; i++) { if(t[i] < num[i]) return true; else if(t[i] > num[i]) return false; } return true; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%s", s+1); n = strlen(s+1); for (int i = 1; i <= n; i++) num[i] = s[i] - '0'; for (int i = 1; i <= (n+1)/2; i++) { t[i] = t[n-i+1] = num[i]; } if(n==1) { if(num[1] == 9) printf("11\n"); else printf("%d\n", num[1]+1); continue; } while(cmp()) { t[(n+1)/2]++; for (int i = (n+1)/2; i >= 2; i--) { t[i-1] += t[i]/10; t[i] %= 10; } for (int i = 1; i <= (n+1)/2; i++) { t[n-i+1] = t[i]; } } for (int i = 1; i < n; i++) printf("%d", t[i]); if(t[n] == 10) printf("1\n");//样例999 else printf("%d\n", t[n]); } return 0; }
8002 树状数组区间更新,区间查询
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 1e5 + 5; int a[N], n; LL sum[N], c1[N], c2[N]; void add(int x, int a) { LL t = x; while(x <= n) c1[x] += a, c2[x] += t*a, x += x&-x; } LL query(int x) { LL ans = 0; LL t = x; while(x) ans += (t+1) * c1[x] - c2[x], x -= x&-x; return ans; } int main() { int T, op, c, p, q, v; scanf("%d", &T); while(T--) { mem(c1, 0); mem(c2, 0); scanf("%d%d", &n, &c); while(c--) { scanf("%d", &op); if(op == 1) { scanf("%d%d", &p, &q); printf("%lld\n", query(q) - query(p-1)); } else { scanf("%d%d%d", &p, &q, &v); add(p, v); add(q+1, -v); } } } return 0; }
3267 离线+树状数组,每次保证这段区间每个数只更新一次,或者主席树(不会,等我学了在a)
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 3e5 + 5, M = 2e5 + 5; int a[N], nxt[N], bit[N], mp[M*5], n; struct node { int l, r, id; bool operator < (const node & t) const { if(l == t.l) return r < t.r; else return l < t.l; } }Q[M]; int ans[N]; void add(int x, int a) { while(x <= n) bit[x] += a, x += x&-x; } int query(int x) { int ans = 0; while(x) ans += bit[x], x -= x&-x; return ans; } int main() { int q; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) { if(mp[a[i]] == 0) { add(i, 1); mp[a[i]] = i; } } mem(mp, 0); for (int i = n; i; i--) { if(mp[a[i]] == 0) { nxt[i] = n+1; mp[a[i]] = i; } else { nxt[i] = mp[a[i]]; mp[a[i]] = i; } } scanf("%d", &q); for (int i = 0; i < q; i++) scanf("%d%d", &Q[i].l, &Q[i].r), Q[i].id = i; sort(Q, Q+q); int t = 1; for (int i = 0; i < q; i++) { while(t <= n && t < Q[i].l) add(nxt[t++], 1); ans[Q[i].id] = query(Q[i].r) - query(Q[i].l-1); } for (int i = 0; i < q; i++) printf("%d\n", ans[i]); return 0; }
主席树(2019.3更新),感觉这个没有离线优秀,不过思想还行,就当练练可持久化
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include#include #include #include using namespace std; #define y1 y11 #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pli pair #define pii pair #define piii pair #define pdd pair #define mem(a, b) memset(a, b, sizeof(a)) #define debug(x) cerr << #x << " = " << x << "\n"; #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); //head const int N = 3e4 + 5, M = 1e6 + 5; int root[N], lson[M], rson[M], tree[M], tot = 0; void build(int &x, int l, int r) { x = ++tot; if(l == r) return ; int m = l+r >> 1; build(lson[x], l, m); build(rson[x], m+1, r); tree[x] = tree[lson[x]] + tree[rson[x]]; } void update(int old, int &x, int p, int v, int l, int r) { x = ++tot; lson[x] = lson[old], rson[x] = rson[old], tree[x] = tree[old] + v; if(l == r) return ; int m = l+r >> 1; if(p <= m) update(lson[x], lson[x], p, v, l, m); else update(rson[x], rson[x], p, v, m+1, r); } int query(int L, int R, int x, int l, int r) { if(L <= l && r <= R) return tree[x]; int m = l+r >> 1, ans = 0; if(L <= m) ans += query(L, R, lson[x], l, m); if(R > m ) ans += query(L, R, rson[x], m+1, r); return ans; } int n, a, mp[M], tmp, q, l, r; int main() { scanf("%d", &n); build(root[0], 1, n); for (int i = 1; i <= n; ++i) { scanf("%d", &a); if(mp[a]) { update(root[i-1], tmp, mp[a], -1, 1, n); update(tmp, root[i], i, 1, 1, n); } else update(root[i-1], root[i], i, 1, 1, n); mp[a] = i; } scanf("%d", &q); while(q--) { scanf("%d %d", &l, &r); printf("%d\n", query(l, r, root[r], 1, n)); } return 0; }
1437 树的直径,两遍dfs
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 1e4 + 5; vector<int>g[N]; int mx = 0, ed = 0; void dfs(int o, int u, int st) { if(st > mx) { mx = st; ed = u; } for (int i = 0; i < g[u].size(); i++) { if(g[u][i] != o) { dfs(u, g[u][i], st+1); } } } int main() { int n, u, v; scanf("%d", &n); for (int i = 0; i < n-1; i++) { scanf("%d%d", &u, &v); g[u].pb(v); g[v].pb(u); } dfs(0, 1, 0); dfs(0, ed, 0); printf("%d\n", mx); return 0; }
1436 dfs
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 1e4 + 5; vector<int>g[N]; bool vis[N]; bool f = true; void dfs(int o, int u) { vis[u] = true; for (int i = 0; i < g[u].size(); i++) { if(g[u][i] != o) { if(!vis[g[u][i]])dfs(u, g[u][i]); else f = false; } } } int main() { int n, m, u, v; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); g[u].pb(v); g[v].pb(u); } dfs(0, 1); for (int i = 1; i <= n; i++) if(!vis[i]) f = false; if(f) puts("YES"); else puts("NO"); return 0; }
6219 普通dp, 与LCS相似
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 2e3 + 5; char s[N], t[N]; int dp[N][N]; int main() { int T; scanf("%d", &T); while(T--) { scanf("%s", s+1); scanf("%s", t+1); int n = strlen(s+1), m = strlen(t+1); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if(i||j){ if(i == 0) dp[i][j] = j; else if(j == 0) dp[i][j] = i; else if(s[i] == t[j]) dp[i][j] = dp[i-1][j-1]; else dp[i][j] = min(dp[i-1][j-1]+1, min(dp[i-1][j]+1, dp[i][j-1]+1)); } } } printf("%d\n", dp[n][m]); } return 0; }
3923 普通dp
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 105; int a[N][N], dp[N][N]; int main() { int T, n, m; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf("%d", &a[i][j]); } } mem(dp, 0); for (int i = 1; i <= m; i++) dp[1][i] = a[1][i]; for (int i = 2; i <= n; i++) { for (int j = 1; j <= m; j++) { dp[i][j] = max(dp[i-1][j], max(dp[i-1][j-1], dp[i-1][j+1])) + a[i][j]; } } int ans = 0; for (int i = 1; i <= m; i++) ans = max(ans, dp[n][i]); printf("%d\n", ans); } return 0; }
95 栈模拟
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head stack<int>s; int main() { int n, t; while(~scanf("%d", &n) && n) { while(!s.empty()) s.pop(); int now = 1; for (int i = 1; i <= n; i++) { scanf("%d", &t); if(t == now) now++; else { s.push(t); } while(!s.empty() && s.top() == now) { s.pop(); now++; } } //cout << now << endl; if(now == n+1) puts("yes"); else puts("no"); } return 0; }
1021 原串与反串的LCS就是原串的最长回文子序列
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 6e3 + 105; char s[N], t[N]; int dp[N][N]; int main() { int T; scanf("%d", &T); while(T--) { scanf("%s", s+1); int n = strlen(s+1); for (int i = 1; i <= n; i++) t[i] = s[n-i+1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if(s[i] == t[j]) dp[i][j] = max(dp[i-1][j-1]+1, max(dp[i-1][j], dp[i][j-1])); else dp[i][j] = max(dp[i-1][j], dp[i][j-1]); } } printf("%d\n", n-dp[n][n]); } return 0; }
14930 记忆化搜索
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 1e4 + 5; LL dp[N][2]; int a[N], n; LL dfs(int pos, int sta) { if(pos == n) { if(sta == 0) return 0; else return a[n]; } if(~dp[pos][sta]) return dp[pos][sta]; if(sta == 1) return dp[pos][sta] = a[pos] + dfs(pos+1, 0); else return dp[pos][sta] = max(dfs(pos+1, 1), dfs(pos+1, 0)); } int main() { int T; scanf("%d", &T); for (int cs = 1; cs <= T; cs++) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); mem(dp, -1); LL ans = max(dfs(1, 0), dfs(1, 1)); printf("Case %d: %lld\n", cs, ans); } return 0; }
1841 bfs
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 1e4 + 5; bool notp[N]; bool vis[N]; int d[10], dd[10]; int p[10] = {0, 1, 10, 100, 1000}; int a, b, ans; bool bfs() { mem(vis, false); queue q; q.push(mp(a, 0)); vis[a] = true; while(!q.empty()) { pii p = q.front(); q.pop(); int t = p.fi; if(t == b) { ans = p.se; return true; } for (int i = 1; i <= 4; i++) { d[i] = t%10; t /= 10; } for (int i = 1; i <= 4; i++) { int t = 0; if(i == 4) t = 1; for (int j = t; j < 10; j++) { if(j == d[i]) continue; for (int k = 1; k <= 4; k++) { if(k == i) continue; dd[k] = d[k]; } dd[i] = j; int n = 0; for (int k = 4; k >=1; k--) n = n *10 + dd[k]; if(!notp[n] && !vis[n]) { vis[n] = true; q.push(mp(n, p.se+1)); } } } } return false; } int main() { int T; scanf("%d", &T); for (int i = 2; i < N; i++) { if(!notp[i]) { for (int j = i+i; j < N ;j += i) notp[j] = true; } } for (int cs = 1; cs <= T; cs++) { scanf("%d%d", &a, &b); if(bfs())printf("%d\n", ans); else printf("Impossible\n"); } return 0; }
97 背包dp
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 555; int dp[N], c[N], v[N]; int main() { int n, m; while(~scanf("%d%d", &n, &m)) { if(!n && !m) return 0; int ans; mem(dp, 0); for (int i = 1; i <= m; i++) scanf("%d%d", &c[i], &v[i]); for (int i = 1; i <= m; i++) { for (int j = n; j >= c[i]; j--) { dp[j] = max(dp[j-c[i]]+v[i], dp[j]); } } for (int i = n; i >= 0; i--) if(dp[i] == dp[n]) ans = i; printf("%d %d\n", ans, dp[n]); } return 0; }
740 区间dp, 小区间推大区间,dp[i][j] = max(dp[i+1][j] + (n+i-j) * a[i], dp[i][j-1] + (n+i-j) * a[j])
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 2e3 + 5; int dp[N][N], a[N]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = n; i >=1; i--) { dp[i][i] = a[i] * n; for (int j = i+1; j <= n; j++) { dp[i][j] = max(dp[i+1][j] + (n+i-j) * a[i], dp[i][j-1] + (n+i-j) * a[j]); } } printf("%d\n", dp[1][n]); return 0; }
206 每次向外扩展一次,做多扩展max(n,m)次,所以复杂度为max(n,m)*n*m
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 222; const int INF = 0x3f3f3f3f; char s[N][N]; int ans[N][N]; int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; int main() { int T, n, m; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); mem(ans, INF); for (int i = 1; i <= n; i++) scanf("%s", s[i]+1); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if(s[i][j] == '1') ans[i][j] = 0; } } for (int i = 1; i <= max(n, m); i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= m; k++) { if(ans[j][k] != INF) for (int l = 0; l < 4; l++) { int x = j + dir[l][0]; int y = k + dir[l][1]; if(1 <= x && x <= n && 1 <= y && y <= m) { ans[x][y] = min(ans[x][y], ans[j][k] + 1); } } } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { printf("%d%c", ans[i][j], " \n"[j==m]); } } } return 0; }
9861 双指针
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 3e5 + 5; int a[N]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } int ans = 0; int l = 0, r = 0, sum = 0; while(true) { while(sum <= m && r < n) { ans = max(ans, sum); sum += a[++r]; } if(sum <= m) ans = max(ans, sum); while(sum > m) { sum -= a[++l]; } if(sum <= m) ans = max(ans, sum); if(r == n) break; } printf("%d\n", ans); return 0; }
3377 带权并查集或普通并查集
带权
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 2e3 + 5; int fa[N], rnk[N]; void init(int n) { for (int i = 0; i <= n; i++) { fa[i] = i; rnk[i] = 0; } } int find(int x) { if(fa[x] == x) return x; else{ int tmp = fa[x]; fa[x] = find(fa[x]); rnk[x] = (rnk[x] + rnk[tmp]) % 2; return fa[x]; } } void merge(int x, int y) { int rx = find(x), ry = find(y); if(rx == ry) return; fa[rx] = ry; rnk[rx] = (rnk[y] + 1 -rnk[x]) % 2; } int main() { int T, n, m, u, v; scanf("%d", &T); for (int cs = 1; cs <= T; cs++) { scanf("%d%d", &n, &m); printf("Scenario #%d:\n", cs); init(n); bool f = false; for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); if(find(u) == find(v) && rnk[u] == rnk[v]) f = true; else merge(u, v); } if(f) printf("Suspicious bugs found!\n"); else printf("No suspicious bugs found!\n"); } return 0; }
普通
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 2e3 + 5; int fa[N*2]; void init(int n) { for (int i = 0; i <= n*2; i++) { fa[i] = i; } } int find(int x) { if(x == fa[x]) return x; else return fa[x] = find(fa[x]); } void merge(int x, int y) { int rx = find(x), ry = find(y); if(rx == ry) return; fa[rx] = ry; } int main() { int T, n, m, u, v; scanf("%d", &T); for (int cs = 1; cs <= T; cs++) { scanf("%d%d", &n, &m); printf("Scenario #%d:\n", cs); init(n); bool f = false; for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); if(find(u) == find(v) || find(u+n) == find(v+n)) f = true; else merge(u, v+n), merge(v, u+n); } if(f) printf("Suspicious bugs found!\n"); else printf("No suspicious bugs found!\n"); } return 0; }
10582 multiset维护(注意删一个元素需要s.erase(s.find(x)),否则会把所有的x都会删掉),顺便复习了一下st表,用st表写了一发
multiset
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 1e6 + 5; int a[N]; multiset<int>s; int main() { int n, k; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); scanf("%d", &k); for (int i = 1; i < k; i++) s.insert(a[i]); for (int i = k; i <= n; i++) { s.insert(a[i]); auto it = s.end(); it --; printf("%d%c", *it, " \n"[i==n]); s.erase(s.find(a[i-k+1])); } return 0; }
st表
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 1e6 + 5; int a[N], st[N][20]; int query(int l, int r) { int k = floor(log(r-l+1)/log(2)); return max(st[l][k], st[r - (1 << k) + 1][k]); } int main() { int n, k; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); scanf("%d", &k); for (int i = n; i >= 1; i--) { st[i][0] = a[i]; for (int j = 1; j + (1 << j-1) <= n; j++) { st[i][j] = max(st[i][j-1], st[i+(1 << j-1)][j-1]); } } for (int i = k; i <= n; i++) printf("%d%c", query(i-k+1, i), " \n"[i==n]); return 0; }
4 stack
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 1e4 + 5; char s[N]; bool is(char c) { if(c == '+' || c == '-' || c == '*' || c == '/' || c == '^') return true; else return false; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%s", s+1); int n = strlen(s+1); stack<char>st; for (int i = 1; i <= n; i++) { if(isalpha(s[i])) putchar(s[i]); else if(is(s[i])) st.push(s[i]); else if(s[i] == ')') putchar(st.top()), st.pop(); } puts(""); } return 0; }
61 线段树区间合并,左区间的左括号和右区间的有括号抵消,看有没有剩下的没匹配的括号
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 3e4 + 5; char s[N]; struct sg_tree{ int L[N<<2], R[N<<2]; void push_up(int rt) { L[rt] = L[rt<<1] + L[rt<<1|1] - min(L[rt<<1], R[rt<<1|1]); R[rt] = R[rt<<1] + R[rt<<1|1] - min(L[rt<<1], R[rt<<1|1]); } void build(int rt, int l,int r) { if(l == r) { if(s[l] == '(') L[rt] = 1, R[rt] = 0; else R[rt] = 1, L[rt] = 0; return ; } int m = (l+r) >> 1; build(ls); build(rs); push_up(rt); } void update(int p, int rt, int l, int r) { if(l == r) { L[rt] ^= 1; R[rt] ^= 1; return ; } int m = (l+r) >> 1; if(p <= m) update(p, ls); else update(p, rs); push_up(rt); } }sg; int main() { int n, m, t, cs = 0; while(~ scanf("%d", &n)) { scanf("%s", s+1); scanf("%d", &m); printf("Test %d:\n", ++cs); sg.build(1, 1, n); while(m--) { scanf("%d", &t); if(!t) { if(sg.L[1] || sg.R[1]) printf("NO\n"); else printf("YES\n"); } else sg.update(t, 1, 1, n); } } return 0; }
1716 线段树区间合并,同1043
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 5e4 + 5; const int INF = 0x7f7f7f7f; struct Tree { int sum, lv, rv, ans; }tree[N<<2]; void push_up(int rt) { tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum; tree[rt].lv = max(tree[rt<<1].lv, tree[rt<<1].sum + tree[rt<<1|1].lv); tree[rt].rv = max(tree[rt<<1|1].rv, tree[rt<<1|1].sum + tree[rt<<1].rv); tree[rt].ans = max(tree[rt<<1].rv + tree[rt<<1|1].lv, max(tree[rt<<1].ans, tree[rt<<1|1].ans)); } void build(int rt, int l, int r) { if(l == r) { scanf("%d", &tree[rt].ans); tree[rt].lv = tree[rt].rv = tree[rt].sum = tree[rt].ans; return ; } int m = (l+r) >> 1; build(ls); build(rs); push_up(rt); } void update(int p, int x, int rt, int l ,int r) { if(l == r) { tree[rt].lv = tree[rt].rv = tree[rt].sum = tree[rt].ans = x; return ; } int m = (l+r) >> 1; if(p <= m) update(p, x, ls); else update(p, x, rs); push_up(rt); } Tree query(int L, int R, int rt, int l, int r) { if(L <= l && r <= R) return tree[rt]; int m = (l + r) >> 1; if(R <= m) return query(L, R, ls); else if(L > m) return query(L, R, rs); Tree lft = query(L, R, ls); Tree rit = query(L, R, rs); int sum = lft.sum+rit.sum; int lv = max(lft.lv, lft.sum+rit.lv); int rv = max(rit.rv, rit.sum+lft.rv); int ans = max(lft.rv+rit.lv, max(lft.ans, rit.ans)); return Tree{sum, lv, rv, ans}; } int main() { int n, m, x, y, op; scanf("%d", &n); build(1, 1, n); scanf("%d", &m); while(m --) { scanf("%d%d%d", &op, &x, &y); if(op) { Tree res = query(x, y, 1, 1, n); printf("%d\n", res.ans); } else { update(x, y, 1, 1, n); } } return 0; }
345 区间dp,经典石子归并问题
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 105; const int INF = 0x3f3f3f3f; int a[N], sum[N], dp[N][N]; int main() { int n; while(~scanf("%d", &n)) { for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) sum[i] = sum[i-1] + a[i]; for (int l = 2; l <= n; l ++) { for (int i = 1; i+l-1 <= n; i++) { int j = i+l-1; dp[i][j] = INF; for (int k = i; k < j; k++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + ((sum[k]-sum[i-1])%100) * ((sum[j]-sum[k])%100)); } } } printf("%d\n", dp[1][n]); } return 0; }
4580 折半枚举
#includeusing namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 105, M = 1e6 + 10; int a[N], b[M]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int cnt = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { b[++cnt] = a[i] * a[j] + a[k]; } } } sort(b+1, b+1+cnt); LL ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { if(a[i] != 0) { int t1 = lower_bound(b+1, b+1+cnt, a[i]*(a[j]+a[k])) - b; int t2 = upper_bound(b+1, b+1+cnt, a[i]*(a[j]+a[k])) - b; ans += t2-t1; } } } } printf("%d\n", ans); return 0; }
302 数学
#include#include #include #include #include #include #include using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair #define pii pair #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head int main() { int T, n; scanf("%d", &T); while(T--) { scanf("%d", &n); int nn = n; int t = sqrt(2*n); t++; if(t*(t-1)/2 >= n) t--; n -= t*(t-1)/2; if(t%2==0)printf("TERM %d IS %d/%d\n", nn, n, t-n+1); else printf("TERM %d IS %d/%d\n", nn, t-n+1, n); } return 0; }
423 状压dp,一开始写了个n*n*(1< 3266 离线+树状数组,先按询问的k排序,然后把从大到小(我的代码是从小到大,求的是小于等与k的)加入这些数的位置,然后只需要维护前缀和就可以了 3946 线段树每个节点开个vector, 然后二分求区间第k大,单次询问复杂度(logn)^3,二分一个logn,线段树一个logn,每个节点二分一个logn 16487 差分数组 4300 暴力水题 14932 lca水题 32 kmp 或者 字符串hash kmp 字符串hash 11 我的傻逼写法:离线+指针,复杂度:Maxa * log(a) + n 正解:数学 7742 水题 54 java大数 3442 快速幂 11515 贪心,每次取这个位置之后能达到的最近的位置,先把每个l和r标记为a[l] = r,然后求个后缀最小值,预处理或者树状数组都可以 39 完全背包 3273 离散化+树状数组+二分 5449 stack 12471 dp+记忆化搜索 24 java大数 9921 bfs水题 总结1:按点赞数降序第一页都是水题,不过复习了之前学过的算法(线段树合并、区间dp之类的),学会了各种离线操作,所以不打算刷简单题了。#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
BigInteger d = new BigInteger("2");
while(reader.hasNext()){
BigInteger a = reader.nextBigInteger();
BigInteger b = reader.nextBigInteger();
BigInteger c = a.subtract(b);
c = c.divide(d);
System.out.println(c.add(b));
System.out.println(c);
}
}
}
#include
#include
#include
#include
#include
#include
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int t;
long n;
t = reader.nextInt();
for (int i = 1; i <= t; i++) {
n = reader.nextLong();
BigInteger ans = new BigInteger("1");
for (long j = 1; j <= n; j++) {
ans = ans.multiply(BigInteger.valueOf(j));
}
System.out.println(ans);
}
}
}
#include