标签:click sync ack one set printf style space 技术分享
297 二分
#include<bits/stdc++.h> 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<LL, LL> #define pii pair<int, int> #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 区间素数筛
#include<bits/stdc++.h> 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<LL, LL> #define pii pair<int, int> #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递归
#include<bits/stdc++.h> 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<LL, LL> #define pii pair<int, int> #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的情况
#include<bits/stdc++.h> 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<LL, LL> #define pii pair<int, int> #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同理
#include<bits/stdc++.h> 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<LL, LL> #define pii pair<int, int> #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; }
标签:click sync ack one set printf style space 技术分享
原文地址:https://www.cnblogs.com/widsom/p/9068570.html