SPOJ 刷题记录

按点赞数降序

297 二分 

#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

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;
}
View Code

2 区间素数筛

#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

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;
}
View Code

346 dp 小于5e6预处理,大于5e6递归

#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

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;
}
View Code

394 dp 注意0的情况

#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

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;
}
View Code

1043 线段数区间合并,lv记录这段区间以左端点为起点的最大和,rv同理

#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

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;
}
View Code

 6256 普通的逆序数,不过我使用了最近学的方法,从大到小加入位置,跑的比较优秀

#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

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;
}
 
View Code

 5 纯暴力,从中间加

#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

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;
}
 
View Code

 8002 树状数组区间更新,区间查询

#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

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;
}
View Code

3267 离线+树状数组,每次保证这段区间每个数只更新一次,或者主席树(不会,等我学了在a)

#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

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;
}
View Code

主席树(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;
}
View Code

1437 树的直径,两遍dfs

#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

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;
}
View Code

1436 dfs

#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

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;
}
View Code

6219  普通dp, 与LCS相似

#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

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;
}
View Code

 3923 普通dp

#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

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;
}
View Code

 95 栈模拟

#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

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;
}
View Code

1021 原串与反串的LCS就是原串的最长回文子序列 

#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

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;
}
View Code

14930 记忆化搜索

#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

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;
}
View Code

1841 bfs

#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

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);
    queueq;
    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;
}
View Code

97 背包dp

#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

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;
}
View Code

740 区间dp, 小区间推大区间,dp[i][j] = max(dp[i+1][j] + (n+i-j) * a[i], dp[i][j-1] + (n+i-j) * a[j])

#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

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;
}
View Code

206 每次向外扩展一次,做多扩展max(n,m)次,所以复杂度为max(n,m)*n*m

#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

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;
}
View Code

9861 双指针

#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

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;
}
View Code

 3377 带权并查集或普通并查集

带权

#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

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;
}
View Code

普通

#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

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;
}
View Code

10582 multiset维护(注意删一个元素需要s.erase(s.find(x)),否则会把所有的x都会删掉),顺便复习了一下st表,用st表写了一发

multiset

#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

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;
}
View Code

st表

#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

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;
}
View Code

4 stack

#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

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;
}
View Code

61 线段树区间合并,左区间的左括号和右区间的有括号抵消,看有没有剩下的没匹配的括号

#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

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;
}
View Code

1716 线段树区间合并,同1043

#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

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;
}
View Code

345 区间dp,经典石子归并问题

#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

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;
}
View Code

4580 折半枚举

#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

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;
}
View Code

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;
}
View Code

423 状压dp,一开始写了个n*n*(1<

#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

const int N = 22;
LL dp[(1<<20) + 5][N];
int a[N][N];
bool vis[(1<<20) + 5];
vector<int>sta[2];
int main() {
    int T, n;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
        }
        mem(dp, 0);
        dp[0][0] = 1;
        int cur = 0;
        sta[0].clear();
        sta[1].clear();
        sta[cur].pb(0);
        for (int i = 1; i <= n; i++) {
            cur ^= 1;
            sta[cur].clear();
            mem(vis, false);
            for (int j = 1; j <= n; j++) {
                if(a[i][j]) {
                    for (int k = 0; k < sta[1-cur].size(); k++) {
                        if(!(sta[1-cur][k] & (1 << (j-1)))) {
                            dp[sta[1-cur][k]|(1 << (j-1))][i] += dp[sta[1-cur][k]][i-1];
                            if(!vis[sta[1-cur][k]|(1 << (j-1))])sta[cur].pb(sta[1-cur][k]|(1 << (j-1))), vis[sta[1-cur][k]|(1 << (j-1))] = true;
                        }
                    }
                }
            }
        }
        printf("%lld\n", dp[(1<1][n]);
    }
    return 0;
}
View Code

3266 离线+树状数组,先按询问的k排序,然后把从大到小(我的代码是从小到大,求的是小于等与k的)加入这些数的位置,然后只需要维护前缀和就可以了

#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

const int N = 3e4 + 5, M = 2e5 + 5;
int id[N], a[N], bit[N], ans[M], n;
struct node {
    int l, r, k, id;
    bool operator < (const node & t) const {
        return k < t.k;
    }
}b[M];
bool cmp(int x, int y) {
    return a[x] < a[y];
}
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]), id[i] = i;
    scanf("%d", &q);
    for (int i = 1; i <= q; i++) scanf("%d%d%d", &b[i].l, &b[i].r, &b[i].k), b[i].id = i;
    sort(b+1, b+1+q);
    sort(id+1, id+1+n, cmp);
    int t = 1;
    for (int i = 1; i <= q; i++) {
        while(t <= n && a[id[t]] <= b[i].k) add(id[t], 1), t++;
        ans[b[i].id] = b[i].r - b[i].l + 1 - query(b[i].r) + query(b[i].l-1);
    }
    for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
    return 0;
}
View Code

3946 线段树每个节点开个vector, 然后二分求区间第k大,单次询问复杂度(logn)^3,二分一个logn,线段树一个logn,每个节点二分一个logn

#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

const int N = 1e5 + 5;
int a[N];
vector<int>vc[N<<2];
vector<int>node;
void build(int rt, int l, int r) {
    if(l == r) {
        scanf("%d", &a[l]);
        vc[rt].pb(a[l]);
        return ;
    }
    int m = (l+r) >> 1;
    build(ls);
    build(rs);
    for (int i = l; i <= r; i++) vc[rt].pb(a[i]);
    sort(vc[rt].begin(), vc[rt].end());
}
void query(int L, int R, int rt, int l, int r) {
    if(L <= l && r <= R) {
        node.pb(rt);
        return ;
    }
    int m = (l+r) >> 1;
    if(L <= m) query(L, R, ls);
    if(R > m) query(L, R, rs);
}
int cal(int x) {
    int ans = 0;
    for (int i = 0; i < node.size(); i++) {
        ans += lower_bound(vc[node[i]].begin(), vc[node[i]].end(), x) - vc[node[i]].begin();
    }
    return ans;
}
int main() {
    int n, m, l, r, k;
    scanf("%d%d", &n, &m);
    build(1, 1, n);
    sort(a+1, a+1+n);
    while(m--) {
        scanf("%d%d%d", &l, &r, &k);
        node.clear();
        query(l, r, 1, 1, n);
        l = 1, r = n;
        int md = (l+r+1) >> 1;
        while(l < r) {
            int cnt = cal(a[md]);
            if(cnt < k) l = md;
            else r = md - 1;
            md = (l+r+1) >> 1;
        }
        printf("%d\n", a[md]);
    }
    return 0;
}
View Code

16487 差分数组

#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

const int N = 1e4 + 5;
int a[N];
int main() {
    int T, n, m, q, v, l, r;
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &n, &m);
        mem(a, 0);
        for (int i = 1; i <= m; i++) scanf("%d%d%d", &l, &r, &v), l++, r++, a[l] += v, a[r+1] -= v;
        for (int i = 1; i <= n; i++) a[i] += a[i-1];
        scanf("%d", &q);
        for (int i = 1; i <= q; i++) scanf("%d", &l), printf("%d\n", a[l+1]);
    }
    return 0;
}
View Code

4300 暴力水题

#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 n;
    scanf("%d", &n);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j * j <= i; j++) {
            if(i%j == 0) ans++;
        }
    }
    printf("%d\n", ans);
    return 0;
}
View Code

14932 lca水题

#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

const int N = 1e3 + 5;
int anc[N][15], deep[N];
vector<int>g[N];
bool vis[N];
void dfs(int o, int u) {
    deep[u] = deep[o] + 1;
    for (int i = 0; i < g[u].size(); i++) {
        anc[g[u][i]][0] = u;
        for (int j = 1; j < 15; j++) anc[g[u][i]][j] = anc[anc[g[u][i]][j-1]][j-1];
        dfs(u, g[u][i]);
    }
}
int lca(int u, int v) {
    if(deep[u] > deep[v]) swap(u, v);
    for (int i = 14; i >= 0; i--) if(deep[anc[v][i]] >= deep[u]) v = anc[v][i];
    if(u == v) return u;
    for (int i = 14; i >= 0; i--) if(anc[u][i] != anc[v][i]) u = anc[u][i], v= anc[v][i];
    return anc[u][0];
}
int main() {
    int T, n, t, u, v, m;
    scanf("%d", &T);
    for (int cs = 1; cs <= T; cs++) {
        mem(vis, false);
        mem(deep, 0);
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) g[i].clear();
        for (int i = 1; i <= n; i++) {
            scanf("%d", &t);
            while(t--) scanf("%d", &u), vis[u] = true, g[i].pb(u);
        }
        int rt = 0;
        for (int i = 1; i <= n; i++) if(!vis[i]) rt = i;
        dfs(0, rt);
        scanf("%d", &m);
        printf("Case %d:\n", cs);
        while(m--) {
            scanf("%d%d", &u, &v);
            printf("%d\n", lca(u, v));
        }
    }
    return 0;
}
View Code

32 kmp 或者 字符串hash

kmp

#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

const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
int nxt[N];
char a[N], b[N];
vector<int>ans;
int main() {
    int n, m;
    while(~scanf("%d", &n)){
        scanf("%s", a);
        scanf("%s", b);
        m = strlen(b);
        nxt[0] = -1;
        for (int i = 1; i < n; i++) {
            int j = nxt[i-1];
            while(a[j+1] != a[i] && j >= 0) j = nxt[j];
            if(a[j+1] == a[i]) nxt[i] = j+1;
            else nxt[i] = -1;

        }
        int i = 0, j = 0;
        ans.clear();
        while(j < m) {
            if(b[j] == a[i]) {
                i++;
                j++;
                if(i == n) ans.pb(j - n), i = nxt[i-1]+1;
            }
            else {
                if(i == 0) j++;
                else i = nxt[i-1]+1;
            }
        }
        if(ans.size() == 0) puts("");
        else {
            for (int i = 0; i < ans.size(); i++) printf("%d\n", ans[i]);
        }
    }
    return 0;
}
View Code

字符串hash

#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

const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
const int base = 23;
char a[N], b[N];
int H[N], p[N];
int get_H(int l, int r) {
    return (H[r] - 1LL * H[l-1] * p[r-l+1] % MOD + MOD)%MOD;
}
vector<int>ans;
int main() {
    int n, m;
    p[0] = 1;
    for (int i = 1; i < N; i++)p[i] = (1LL * p[i-1] * base)%MOD;
    while(~scanf("%d", &n)){
        scanf("%s", a+1);
        int f = 0;
        for (int i = 1; i <= n; i++) f = (1LL * f * base + a[i] - 'a')%MOD;
        scanf("%s", b+1);
        m = strlen(b+1);
        for (int i = 1; i <= m; i++) H[i] = (1LL * H[i-1] * base + (b[i] - 'a'))%MOD;
        ans.clear();
        for (int i = 1; i+n-1 <= m; i++) {
            if(get_H(i, i+n-1) == f) ans.pb(i);
        }
        if(ans.size() == 0) puts("");
        else {
            for (int i = 0; i < ans.size(); i++) printf("%d\n", ans[i]-1);
        }
    }
    return 0;
}
View Code

 11

我的傻逼写法:离线+指针,复杂度:Maxa * log(a) + n

#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

const int N = 1e5 + 5;
pii Q[N];
int ans[N];
int main() {
    int T;
    scanf("%d", &T);
    for (int i = 1; i <= T; i++) scanf("%d", &Q[i].fi), Q[i].se = i;
    sort(Q+1, Q+1+T);
    int l = 0, c2 = 0, c5 = 0;
    for (int i = 1; i <= T; i++) {
        while(l < Q[i].fi) {
            l++;
            int t = l;
            while(t%2 == 0) c2++, t /= 2;
            while(t%5 == 0) c5++, t /= 5;
        }
        ans[Q[i].se] = min(c2, c5);
    }
    for (int i = 1; i <= T; i++) printf("%d\n", ans[i]);
    return 0;
}
View Code

正解:数学

#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);
    for (int i = 1; i <= T; i++) {
        scanf("%d", &n);
        int ans = 0;
        for (LL i = 5; i <= n; i *= 5) ans += n/i;
        printf("%d\n", ans);
    }
    return 0;
}
View Code

7742 水题

#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

const int N = 5e5 + 5;
int a[N];
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    sort(a+1, a+1+n);
    int c = 0;
    for (int i = 1; i <= n; i++) {
        if(a[i] != a[i-1]) {
            if(c == 1) return 0 * printf("%d\n", a[i-1]);
            c = 1;
        }
        else c++;
    }
    if(c == 1) printf("%d\n", a[n]);
    return 0;
}
View Code

54 java大数

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);
        }
    }
}
View Code

3442 快速幂

#include
using namespace std;
#define LL long long
LL q_pow(LL n, LL k) {
    LL ans = 1;
    while(k) {
        if(k&1) ans = (ans * n) % 10;
        n = (n*n) % 10;
        k >>= 1;
    }
    return ans;
}
int main() {
    int T;
    LL a, b;
    scanf("%d", &T);
    while(T--) {
        scanf("%lld%lld", &a, &b);
        printf("%lld\n", q_pow(a, b));
    }
    return 0;
}
View Code

11515 贪心,每次取这个位置之后能达到的最近的位置,先把每个l和r标记为a[l] = r,然后求个后缀最小值,预处理或者树状数组都可以

#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

const int N = 1e5 + 5, M = 1e6 + 5;
const int INF = 0x3f3f3f3f;
pii a[N];
int b[M], bit[M];
int mx;
void add(int x, int a) {
    while(x) bit[x] = min(bit[x], a), x -= x&-x;
}
int query(int x) {
    int ans = INF;
    while(x <= mx) ans = min(ans, bit[x]), x += x&-x;
    return ans;
}
int main() {
    int T, n;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        mx = 0;
        for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].fi, &a[i].se), a[i].fi++, a[i].se++, mx = max(mx, a[i].se);
        //sort(a+1, a+1+n);
        mem(b, INF);
        mem(bit, INF);
        for (int i = 1; i <= n; i++) b[a[i].fi] = min(b[a[i].fi], a[i].se);
        for (int i = 1; i < mx; i ++) {
            if(b[i] != INF) add(i, b[i]); //cout << i << " " << b[i] << endl;
        }
        int st = 1, ans = 0;
        while(st <= mx) {
            st = query(st);
            if(st == INF) break;
            else ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}
View Code

39 完全背包

#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

const int N = 555, M = 1e4 + 10;
const int INF = 0x3f3f3f3f;
pii a[N];
int dp[M];
int main() {
    int T, n, e, f, w;
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &e, &f);
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)scanf("%d%d", &a[i].fi, &a[i].se);
        mem(dp, INF);
        dp[0] = 0;
        w = f - e;
        for (int i = 1; i <= n; i++) {
            for (int j = a[i].se; j <= w; j++) {
                dp[j] = min(dp[j], dp[j-a[i].se]+a[i].fi);
            }
        }
        if(dp[w] == INF) printf("This is impossible.\n");
        else printf("The minimum amount of money in the piggy-bank is %d.\n", dp[w]);
    }
    return 0;
}
View Code

3273 离散化+树状数组+二分

#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

const int N = 2e5 + 5;
pair<int, char> Q[N];
int a[N], bit[N], cnt[N], q;
char s[10];
void add(int x, int a) {
    while(x <= q) bit[x] += a, x += x&-x;
}
int query(int x) {
    int ans = 0;
    while(x) ans += bit[x], x -= x&-x;
    return ans;
}
int MP[N];
int main() {
    int t;
    scanf("%d", &q);
    for (int i = 1; i <= q; i++) {
        scanf("%s%d", s, &t);
        Q[i] = mp(t, s[0]);
        a[i] = t;
    }
    sort(a+1, a+1+q);
    for (int i = 1; i <= q; i++) {
        t = lower_bound(a+1, a+1+q, Q[i].fi) - a;
        if(Q[i].se != 'K') {
            MP[t] = Q[i].fi;
            Q[i].fi = t;
        }
    }
    for (int i = 1; i <= q; i++) {
        if(Q[i].se == 'I') {
            if(!cnt[Q[i].fi])add(Q[i].fi, 1), cnt[Q[i].fi]++;
        }
        else if(Q[i].se == 'D') {
            if(cnt[Q[i].fi]) cnt[Q[i].fi]--, add(Q[i].fi, -1);
        }
        else if(Q[i].se == 'K') {
            if(Q[i].fi > query(q)) {
                printf("invalid\n");
                continue;
            }
            int l = 1, r = q, m = (l+r) >> 1;
            while(l < r) {
                if(query(m) < Q[i].fi) l = m+1;
                else r = m;
                m = (l+r) >> 1;
            }
            printf("%d\n", MP[m]);
        }
        else if(Q[i].se == 'C') {
            printf("%d\n", query(Q[i].fi-1));
        }
    }
    return 0;
}
View Code

5449 stack

#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

const int N = 2e3 + 5;
char s[N];
int main() {
    int cs = 0;
    while(~scanf("%s", s+1)) {
        if(s[1] == '-') break;
        int n = strlen(s+1);
        int l = 0, ans = 0;
        for (int i = 1; i <= n; i++) {
            if(s[i] == '{') l++;
            else {
                if(l) l--;
                else ans++, l++;
            }
        }
        ans += l/2;
        printf("%d. %d\n", ++cs, ans);
    }
    return 0;
}
View Code

12471 dp+记忆化搜索

#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

const int N = 2e3 + 5;
int dp[N][N][3];
int dir[3][2] = {3, 2, -5, -10, -20, 5};
int dfs(int a, int b, int t) {
    if(a <= 0 || b <= 0) return -1;
    if(~dp[a][b][t]) return dp[a][b][t];
    for (int i = 0; i < 3; i++) {
        if(i != t)dp[a][b][t] = max(dp[a][b][t], dfs(a+dir[i][0], b+dir[i][1], i)+1);
    }
    return dp[a][b][t];
}
int main() {
    int T, h, a;
    mem(dp, -1);
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &h, &a);
        printf("%d\n", max(max(dfs(h, a, 0), dfs(h, a, 1)), dfs(h, a, 0)));
    }
    return 0;
}
View Code

24 java大数

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);
        }
    }
}
View Code

9921 bfs水题

#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 piii 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 = 55;
char s[N][N];
bool vis[N][N];
int n, m;
int dir[8][2] = {1, 0, 0, 1, -1, 0, 0, -1, 1, -1, -1, 1, 1, 1, -1, -1};
queueq;
int main() {
    int cs = 0;
    while(~scanf("%d%d", &n, &m)) {
        if(!n && !m) break;
        for (int i = 1; i <= n; i++) scanf("%s", s[i]+1);
        mem(vis, false);
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if(s[i][j] == 'A') {
                    q.push({1, {i, j}});
                    vis[i][j] = true;
                }
            }
        }
        int ans = 0;
        while(!q.empty()) {
            piii p = q.front();
            q.pop();
            ans = p.fi;
            for (int i = 0; i < 8; i++) {
                int x = p.se.fi + dir[i][0];
                int y = p.se.se + dir[i][1];
                if(1 <= x && x <= n && 1 <= y && y <= m && !vis[x][y] && s[x][y] == s[p.se.fi][p.se.se]+1) {
                    vis[x][y] = true;
                    ans = p.fi + 1;
                    q.push({p.fi+1, {x, y}});
                }
            }
        }
        printf("Case %d: %d\n", ++cs, ans);
    }
    return 0;
}
View Code

总结1:按点赞数降序第一页都是水题,不过复习了之前学过的算法(线段树合并、区间dp之类的),学会了各种离线操作,所以不打算刷简单题了。

转载于:https://www.cnblogs.com/widsom/p/9068570.html

你可能感兴趣的:(SPOJ 刷题记录)