SQOI Round0 题解

· · 个人记录

A

全部商品打 5 折其实就是钱数翻倍,将打折后的钱和原来的钱取 max 然后跑一遍背包即可。

参考代码:

#include<iostream>

using namespace std;

const int N = 2e3 + 10;
int n, m, k, f[N], u[N], v[N];

int main(){
    cin >> n >> m >> k;
    m = max(m, (m-k)*2);
    for(int i = 1; i <= n; i++) cin >> u[i];
    for(int i = 1; i <= n; i++) cin >> v[i];
    for(int i = 1; i <= n; i++){
        for(int j = m; j >= u[i]; j--){
            f[j] = max(f[j], f[j-u[i]] + v[i]);
        }
    }
    cout << f[m];
    return 0;
}

B

考虑矩阵的每个数值对答案的贡献,分别统计包含 0 的子矩阵,包含 0,1 的子矩阵,然后由于包含 0,1 的子矩阵已经在包含 0 的子矩阵中统计过,只需要再加上包含 0,1 的子矩阵的个数即可,以此类推。

参考代码:

#include<iostream>

using namespace std;
typedef long long ll;

const int N = 1010;
int n, a[N][N], x[N*N], y[N*N], lx = 1e9, ly = 1e9, rx, ry;
ll ans = 0;

int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            cin >> a[i][j];
            x[a[i][j]] = i;
            y[a[i][j]] = j;
        }
    }
    for(int i = 0; i < n*n; i++){
        lx = min(lx, x[i]);
        rx = max(rx, x[i]);
        ly = min(ly, y[i]);
        ry = max(ry, y[i]);
        ans += (ll)lx*(n-rx+1)*ly*(n-ry+1);
    }
    cout << ans;
    return 0;
}

C

设 a+b=n(a,b,n>0),则 n^2=(a^2+2ab+b^2)=(n^2-2ab+b^2)+4ab=(a-b)^2+4ab,所以当 n 不变时,|a-b| 越小,ab 越大。

于是就贪心的每次取 b 数组最大值和 a 的最小值和 最大值即可。

参考代码:

#include<iostream>
#include<algorithm>

using namespace std;
typedef long long ll;

const int N = 2e5 + 10, mod = 998244853;
int n, a[N], b[N]; ll s = 0, ans;

int main(){
    cin >> n;
    for(int i = 0; i < n; i++) {cin >> a[i]; s = ((ll)a[i] + s) % mod;}
    for(int i = 0; i < n-1; i++) cin >> b[i];
    sort(a, a + n);
    sort(b, b + n - 1);
    for(int i = 0; i < n; i++) ans = (ans + (ll)(s-a[i]+mod) * a[i] % mod * b[n-i-2] % mod) % mod;
    cout << ans;
    return 0;
}

D

考虑数形结合,如图:

然后得出式子其实就等于 F_n \times F_{n+1},矩阵快速幂求出即可。

#include<iostream>

using namespace std;
typedef long long ll;

const int N = 2, mod = 1e9 + 7; 
int t; ll n;

struct rect{
    int w, h;
    ll a[N][N];

    void input(){
        for(int i = 0; i < w; i++){
            for(int j = 0; j < h; j++){
                cin >> a[i][j];
            }
        }
    }

    void output(){
        for(int i = 0; i < w; i++){
            for(int j = 0; j < h; j++){
                cout << a[i][j] << ' ';
            }
            cout << endl;
        }
    }

    void setup(){
        for(int i = 0; i < w; i++){
            for(int j = 0; j < h; j++){
                a[i][j] = (i == j);
            }
        }
    }

    rect operator * (rect o) const{
        rect ret;
        for(int i = 0; i < w; i++){
            for(int j = 0; j < o.h; j++){
                ll sum = 0;
                for(int k = 0; k < h; k++){
                    sum += (a[i][k] * o.a[k][j] + mod) % mod;
                    sum %= mod;
                }
                ret.a[i][j] = sum;
            }
        }
        ret.w = w;
        ret.h = o.h;
        return ret;
    }
};

rect pow(rect x, ll y){
    rect res;
    res.w = 2; res.h = 2;
    res.setup();
    while(y){
        if(y % 2 == 1){
            res = res * x;
            y /= 2;
            x = x * x;
        }
        else{
            y /= 2;
            x = x * x;
        }
    }
    return res;
}

int main(){
    cin >> t;
    while(t--){
        cin >> n;
        rect r;
        r.a[0][0] = 0; r.a[0][1] = 1; r.a[1][0] = 1; r.a[1][1] = 1;
        r.w = 2; r.h = 2;
        rect r3 = pow(r, n - 2);
        rect r2;
        r2.a[0][0] = 1; r2.a[1][0] = 1;
        r2.w = 2; r2.h = 1;
        rect ans1 = r3 * r2;
        ll gt1 = ans1.a[1][0];

        r3 = pow(r, n - 1);
        rect ans2 = r3 * r2;
        ll gt2 = ans2.a[1][0];
        cout << gt1 * gt2 % mod << '\n';
    }
    return 0;
}

E

观察到值域非常小,于是就考虑统计区间 1 \sim 5 的个数,然后依次填充回数组即可,可以考虑使用线段树。

参考代码:

#include<iostream>

using namespace std;

const int N = 2e5 + 10;
int n, m, op, l, r, a[N], al, ar, ans;

struct Node{
    bool b[6];
    int c[6], len;
} segt[N*4];

void merge(int u){
    for(int i = 0; i <= 5; i++){
        segt[u].c[i] = segt[u*2].c[i] + segt[u*2+1].c[i];
    }
    segt[u].len = segt[u*2].len + segt[u*2+1].len;
}

void down(int u){
    for(int i = 0; i <= 5; i++){
        if(segt[u].b[i]){
            for(int i = 0; i <= 5; i++){
                segt[u*2].b[i] = segt[u*2].c[i] = 0;
            }
            segt[u*2].b[i] = 1, segt[u*2].c[i] = segt[u*2].len;
            for(int i = 0; i <= 5; i++){
                segt[u*2+1].b[i] = segt[u*2+1].c[i] = 0;
            }
            segt[u*2+1].b[i] = 1, segt[u*2+1].c[i] = segt[u*2+1].len;
            segt[u].b[i] = 0;
        }
    }
}

void build(int u, int l, int r){
    for(int i = 0; i <= 5; i++){
        segt[u].b[i] = segt[u].c[i] = 0;
    }
    if(l == r){
        segt[u].c[a[l]] = 1;
        segt[u].len = 1;
        return;
    }
    down(u);
    int mid = (l + r) / 2;
    build(u*2, l, mid);
    build(u*2+1, mid+1, r);
    merge(u);
} 

int L, R, V, gt[6];
void update(int u, int l, int r){
    if(L <= l && r <= R){
        for(int i = 0; i <= 5; i++){
            segt[u].b[i] = segt[u].c[i] = 0;
        }
        segt[u].b[V] = 1;
        segt[u].c[V] = segt[u].len;
        return;
    }
    int mid = (l + r) / 2;
    down(u);
    if(L <= mid) update(u*2, l, mid);
    if(mid < R) update(u*2+1, mid+1, r);
    merge(u);
}

void query(int u, int l, int r){
    if(L <= l && r <= R){
        for(int i = 0; i <= 5; i++){
            gt[i] += segt[u].c[i];
        }
        return;
    }
    int mid = (l + r) / 2;
    down(u);
    if(L <= mid) query(u*2, l, mid);
    if(mid < R) query(u*2+1, mid+1, r);
    merge(u);
}

int main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> a[i];
    al = 1, ar = n;
    build(1, 1, n);
    for(int i = 1; i <= m; i++){
        cin >> op >> l >> r;
        if(op == 1){
            L = l, R = r;
            for(int i = 0; i <= 5; i++) gt[i] = 0;
            if(L <= R) query(1, 1, n);
            L = l; R = L + gt[5] - 1, V = 5;
            if(L <= R) update(1, 1, n);
            L = R+1; R = L + gt[4] - 1, V = 4;
            if(L <= R) update(1, 1, n);
            L = R+1; R = L + gt[3] - 1, V = 3;
            if(L <= R) update(1, 1, n);
            L = R+1; R = L + gt[2] - 1, V = 2;
            if(L <= R) update(1, 1, n);
            L = R+1; R = L + gt[1] - 1, V = 1;
            if(L <= R) update(1, 1, n);
            L = R+1; R = r, V = 0;
            if(L <= R) update(1, 1, n);
        }
        else{
            L = l, R = r;
            for(int i = 0; i <= 5; i++) gt[i] = 0;
            if(L <= R) query(1, 1, n);
            L = l; R = L + gt[0] - 1, V = 0;
            if(L <= R) update(1, 1, n);
            L = R+1; R = L + gt[1] - 1, V = 1;
            if(L <= R) update(1, 1, n);
            L = R+1; R = L + gt[2] - 1, V = 2;
            if(L <= R) update(1, 1, n);
            L = R+1; R = L + gt[3] - 1, V = 3;
            if(L <= R) update(1, 1, n);
            L = R+1; R = L + gt[4] - 1, V = 4;
            if(L <= R) update(1, 1, n);
            L = R+1; R = r, V = 5;
            if(L <= R) update(1, 1, n);
        }
    }
    for(int i = 1; i <= n; i++){
        L = i, R = i;
        for(int i = 0; i <= 5; i++) gt[i] = 0;
        query(1, 1, n);
        for(int i = 0; i <= 5; i++) if(gt[i]) cout << i << ' ';
    }
    return 0;
}

F

线段树分别维护 \sum^r_{i=l} a_{i} 和 \sum^r_{i=l} ia_{i},查询时求 \sum^r_{i=l} ia_{i}-(l-1)\sum^r_{i=l} a_{i} 即可。

参考代码:

#include<iostream>

using namespace std;
typedef long long ll;

const int N = 2e5 + 10;
int n, m, a[N], op, L, R, x;
ll ans = 0, res1, res2;

struct Node{
    ll s1, s2;
    int fg, l, r;
} t[N*4];

void down(int u){
    if(t[u].fg){
        ll y = t[u].fg;
        t[u].fg = 0;
        t[u*2].s1 = y*(t[u*2].r-t[u*2].l+1);
        t[u*2].s2 = y*(t[u*2].l+t[u*2].r)*(t[u*2].r-t[u*2].l+1)/2;
        t[u*2].fg = y;
        t[u*2+1].s1 = y*(t[u*2+1].r-t[u*2+1].l+1);
        t[u*2+1].s2 = y*(t[u*2+1].l+t[u*2+1].r)*(t[u*2+1].r-t[u*2+1].l+1)/2;
        t[u*2+1].fg = y;
    }
}

void build(int u, int l, int r){
    t[u].fg = 0;
    t[u].l = l; t[u].r = r;
    if(l == r){
        t[u].s1 = (ll)a[l];
        t[u].s2 = (ll)a[l] * l;
        return;
    }
    int mid = (l + r) / 2;
    build(u*2, l, mid);
    build(u*2+1, mid+1, r);
    t[u].s1 = t[u*2].s1 + t[u*2+1].s1;
    t[u].s2 = t[u*2].s2 + t[u*2+1].s2;
}

void update(int u){
    if(L <= t[u].l && t[u].r <= R){
        t[u].s1 = (ll)x*(t[u].r-t[u].l+1);
        t[u].s2 = (ll)x * (t[u].l+t[u].r) * (t[u].r-t[u].l+1) / 2;
        t[u].fg = x;
        return;
    }
    int mid = (t[u].l + t[u].r) / 2;
    down(u);
    if(L <= mid) update(u*2);
    if(mid < R) update(u*2+1);
    t[u].s1 = t[u*2].s1 + t[u*2+1].s1;
    t[u].s2 = t[u*2].s2 + t[u*2+1].s2;
}

void query(int u){
    if(L <= t[u].l && t[u].r <= R){
        res1 += t[u].s1;
        res2 += t[u].s2;
        return;
    }
    int mid = (t[u].l + t[u].r) / 2;
    down(u);
    if(L <= mid) query(u*2);
    if(mid < R) query(u*2+1);
}

int main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);
    while(m--){
        cin >> op >> L >> R; ans = 0;
        if(op == 1){
            cin >> x;
            update(1);
        }
        else{
            res1 = res2 = 0;
            query(1);
            cout << res2 - res1 * (L-1) << '\n';
        }
    }
    return 0;
}

G

只有一个炸弹,所以炸弹必须放在桥上(定义请看oiwiki),于是跑边双+缩点,然后看哪个桥的式子最大即可。

参考代码:

#include<iostream>
#include<vector>
#include<map>

using namespace std;
typedef long long ll;

const int N = 2e5 + 10;

struct pii{
    int v, id;
};
vector<pii> g[N]; vector<int> G[N];
int n, m, u, v, tt = 0, low[N], dfn[N], rep[N], sc, sm[N];
bool brg[N]; ll ans = 0;

void dfs(int u, int ine){
    low[u] = dfn[u] = ++tt;
    for(auto vv: g[u]){
        int v = vv.v, id = vv.id;
        if(id == ine) continue;
        if(dfn[v] == 0){
            dfs(v, id);
            low[u] = min(low[u], low[v]);
            if(low[v] > dfn[u]) brg[id] = 1;
        }
        else{
            low[u] = min(low[u], dfn[v]);
        }
    }
}

void dfs2(int u, int w){
    rep[u] = w;
    for(auto vv: g[u]){
        int v = vv.v, id = vv.id;
        if(brg[id] || rep[v]) continue;
        dfs2(v, w);
    }
}

void dfs3(int u, int f){
    for(int v: G[u]){
        if(v == f) continue;
        dfs3(v, u);
        sm[u] += sm[v];
    }
}

void dfs4(int u, int f){
    for(int v: G[u]){
        if(v == f) continue;
        dfs4(v, u);
        ans = max(ans, (ll)sm[v] * (n-sm[v]));
    }
}

int main(){
    cin >> n >> m;
    for(int i = 1; i <= m; i++){
        cin >> u >> v;
        g[u].push_back({v, i});
        g[v].push_back({u, i});
    }
    for(int i = 1; i <= n; i++) if(!dfn[i]) dfs(i, 0);
    for(int i = 1; i <= n; i++) if(!rep[i]) dfs2(i, ++sc);
    for(int i = 1; i <= n; i++){
        for(pii j: g[i]) if(rep[i] != rep[j.v]) G[rep[i]].push_back(rep[j.v]);
    }
    for(int i = 1; i <= n; i++) sm[rep[i]]++;
    dfs3(1, 0);
    dfs4(1, 0);
    cout << ans << '\n';
    return 0;
}

Ex

观察到 n \ge 3,可以列以下方程:

\begin{cases} a_1+a_2=w_1 \\ a_2+a_3=w_2 \\ a_1+a_3=w_3 \end{cases}

解得:

\begin{cases} a_1=\frac{w_1+w_2+w_3}{2}-w_2 \\ a_2=\frac{w_1+w_2+w_3}{2}-w_3 \\ a_3=\frac{w_1+w_2+w_3}{2}-w_1 \end{cases}

接下来通过询问 (a_1+a_i) 得出 a_i=(a_1+a_i)-a_1(i \ge 4) 即可。

参考代码:

#include<iostream>

using namespace std;

const int N = 1e3 + 10;
int t, p, n, a[N], lst, x1, x2, x3;

int main(){
    cin >> t >> p;
    while(t--){
        cin >> n;
        cout << "? 1 1 2" << endl;
        cin >> x1;
        cout << "? 1 2 3" << endl;
        cin >> x2;
        cout << "? 1 1 3" << endl;
        cin >> x3;
        a[1] = (x1+x2+x3)/2-x2;
        a[2] = (x1+x2+x3)/2-x3;
        a[3] = (x1+x2+x3)/2-x1;
        for(int i = 4; i <= n; i++){
            cout << "? 1 1 " << i << endl;
            cin >> x1;
            a[i] = x1 - a[1];
        }
        cout << "! ";
        for(int i = 1; i <= n; i++) cout << a[i] << ' ';
        cout << endl;
    }
    return 0;
} 

Ex+

显然:

a \bmod b=\begin{cases} a \bmod b & a>b \\ 0 & a=b \\ a & a<b \end{cases} b \bmod a=\begin{cases} b & a>b \\ 0 & a=b \\ b \bmod a & a<b \end{cases}

而 a \bmod b < b,b \bmod a < a,所以当 a \ne b 时,\max(a \bmod b,b \bmod a)=\min(a,b),最后可以用一次加操作来获取未知值。

参考代码:

#include<iostream>
#include<vector>

using namespace std;

const int N = 1e3 + 10;
int t, p, n, a[N], lst, x1, x2;
vector<int> unk; 

int main(){
    cin >> t >> p;
    while(t--){
        cin >> n; unk.push_back(1);
        for(int i = 1; i <= n; i++) a[i] = -1;
        for(int i = 2; i <= n; i++){
            cout << "? 2 " << unk.back() << ' ' << i << endl;
            cin >> x1;
            cout << "? 2 " << i << ' ' << unk.back() << endl;
            cin >> x2;
            if(x1 == 0 && x2 == 0){
                unk.push_back(i);
            }
            else if(x1 > x2){
                for(int j: unk) a[j] = x1;
                unk.clear(); unk.push_back(i);
            }
            else{
                a[i] = x2;
            }
        }
        if(unk.size() == n){
            cout << "? 1 1 2" << endl;
            cin >> x1;
            for(int j: unk) a[j] = x1/2;
        }
        else{
            int kn = 0;
            for(int i = 1; i <= n; i++){
                if(a[i] != -1){
                    kn = i;
                    break;
                }
            }
            cout << "? 1 " << kn << ' ' << unk.back() << endl;
            cin >> x1;
            for(int j: unk) a[j] = x1 - a[kn];
        }
        unk.clear();
        cout << "! ";
        for(int i = 1; i <= n; i++) cout << a[i] << ' ';
        cout << endl;
    }
    return 0;
} 

???

复制 Markdown,就会发现 You can get the answer to this question in the example of question G 的提示,于是考虑在 G 题上打表,查找样例在正式测试点中的第几个,可以发现第 23 个测试点和样例测试点一模一样,于是输出 23 即可。

参考代码:

#include<iostream>

using namespace std;

int main(){
    cout << 23;
    return 0;
}