[hihocoder][Offer收割]编程练习赛57

1-偏差排列

斐波那契数列

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI;

void makedata() {
    freopen("input.txt", "w", stdout);
    fclose(stdout);
}

lint f[60];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    f[1] = 1, f[2] = 2;
    for (int i = 3; i <= n; i++) f[i] = f[i - 1] + f[i - 2];
    cout << f[n] << endl;
    return 0;
}
View Code

递增N元组

先把每一行排序

dp[i][j]表示从第i行开始,且第i行选择第j个及以后的数字的方案数

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI;
typedef map<int, int> MII;

void makedata() {
    freopen("input.txt", "w", stdout);
    fclose(stdout);
}

int mp[105][110000];
lint dp[105][110000];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n, m, x;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> mp[i][j];
        }
        sort(mp[i], mp[i] + m);
    }
    for (int i = 0; i < m; i++) dp[n][i] = m - i;
    for (int i = n - 1; i >= 1; i--) {
        int p = m - 1;
        for (int j = m - 1; j >= 0; j--) {
            dp[i][j] = dp[i][j + 1];
            while (mp[i + 1][p - 1] > mp[i][j] && p) p--;
            if (mp[i][j] >= mp[i + 1][p]) continue;
            dp[i][j] += dp[i + 1][p];
            dp[i][j] %= 1000000007;
        }
    }
    cout << dp[1][0] << endl;
    return 0;
}
View Code

逃离迷宫5

bfs...

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI;
typedef map<int, int> MII;

void makedata() {
    freopen("input.txt", "w", stdout);
    fclose(stdout);
}

struct node {
    int x, y, k;
    node(int xx, int yy, int kk) : x(xx), y(yy), k(kk) {}
};
queue<node> q;
int d[1005][1005][2];
string mp[1005];
const int dx[4] = { -1, 0, 1, 0 };
const int dy[4] = { 0, 1, 0, -1 };

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> mp[i];
    memset(d, 0x3F, sizeof(d));
    while (!q.empty()) q.pop();
    if (mp[0][0] == '.') d[0][0][0] = 0, q.push(node(0, 0, 0));
    else d[0][0][1] = 0, q.push(node(0, 0, 1));
    while (!q.empty()) {
        node p = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
            int px = p.x + dx[i], py = p.y + dy[i];
            if (px < 0 || px >= n || py < 0 || py >= n) continue;
            if (mp[px][py] == '.') {
                if (d[px][py][p.k] > d[p.x][p.y][p.k] + 1) {
                    d[px][py][p.k] = d[p.x][p.y][p.k] + 1;
                    q.push(node(px, py, p.k));
                }
            } else {
                if (p.k == 0) {
                    if (d[px][py][1] > d[p.x][p.y][0] + 1) {
                        d[px][py][1] = d[p.x][p.y][0] + 1;
                        q.push(node(px, py, 1));
                    }
                }
            }
        }
    }
    if (d[n - 1][n - 1][0] != 0x3F3F3F3F || d[n - 1][n - 1][1] != 0x3F3F3F3F) cout << min(d[n - 1][n - 1][0], d[n - 1][n - 1][1]) << endl;
    else cout << -1 << endl;
    return 0;
}
View Code

最大割集

算出每个点相关的边的异或和,割<S,T>中边的异或和就是S集合中点的边异或和的异或和,因为点集内部的边在这些异或和中被计算了两次变成0,只剩下S到T的边。然后按w排序从大到小添加线性基、

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
#include<iomanip>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI;
typedef map<int, int> MII;

void makedata() {
    freopen("input.txt", "w", stdout);
    fclose(stdout);
}

template <typename T> class LnBase {
public:
    int sz, szc;
    T *x;
    int *y;
    LnBase() {
        x = 0, sz = sizeof(T) << 3;
        szc = -1, resize(sz);
    }
    void resize(int size) {
        sz = size;
        if (!x) delete (x);
        x = new T[sz + 2];
        y = new int[sz + 2];
        memset(x, 0, sz * sizeof(T));
        memset(y, 0, sz << 2);
    }
    T operator[](int h) {
        return x[h];
    }
    //增加一个向量,若该向量能被已有向量线性表示,返回-1;否则返回该向量增加的是哪一个维度
    int add(T v) {
        for (int i = sz - 1; i >= 0; i--)
            if (v & (T)1 << i) {
                if (!x[i]) {
                    x[i] = v;
                    szc = -1;
                    return i;
                }
                v ^= x[i];
            }
        return -1;
    }
    //若该向量能被已有向量线性表示,返回1;否则返回0
    int find(T v) {
        for (int i = sz - 1; i >= 0; i--) {
            if (v & (T)1 << i && x[i]) v ^= x[i];
            if (!v) return 1;
        }
        return 0;
    }
    //线性基能表示出的最大向量
    T max() {
        T s = 0;
        for (int i = sz - 1; i >= 0; i--) {
            if ((s ^ x[i]) > s) s ^= x[i];
        }
        return s;
    }
    //线性基能表示出的最小向量,为空返回-1
    T min() {
        for (int i = 0; i < sz; i++) if (x[i]) return x[i];
        return -1;
    }
    //矩阵标准化
    void canonicity() {
        int i, j;
        for (i = sz - 1; i > 0; i--)
            for (j = i - 1; j >= 0; j--) if (x[i] & (T)1 << j) x[i] ^= x[j];
        for (szc = i = 0; i < sz; i++) if (x[i]) y[szc++] = i;
    }
    //线性基能表示出的第k大的向量
    T kth(long long K) {
        if (szc < 0) canonicity();
        if (K >= 1ll << szc) return -1;
        T s = 0;
        for (int i = szc - 1; i >= 0; i--) if (K & 1ll << i) s ^= x[y[i]];
        return s;
    }
};
LnBase<lint> lb;
pair<lint, lint> p[110000];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n, m, u, v;
    lint t;
    long double sum = 0;
    cin >> n >> m;
    memset(p, 0, sizeof(p));
    for (int i = 0; i < n; i++) {
        cin >> p[i].first;
        sum += p[i].first;
    }
    for (int i = 0; i < m; i++) {
        cin >> u >> v >> t;
        u--, v--;
        p[u].second ^= t, p[v].second ^= t;
    }
    sort(p, p + n);
    long double sel = 0;
    for (int i = n - 1; i >= 0; i--) {
        if (lb.add(p[i].second) != -1) sel += p[i].first;
    }
    cout << setprecision(0) << fixed;
    cout << (2 * sel - sum) << endl;
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/dramstadt/p/9109921.html