跳到正文

个人记录

Noip2024模拟赛Day18

luckyzjy约 13 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 13 分钟

版本

  1. v1当前

前言

再用快写我就是大沙碧

题目分析

T1 矩阵矩阵

题目

给出两个 n×nn\times n 的矩阵,mm 次询问它们的积中给定子矩阵的数值和。

做法

有一点点思维(约等于没有)

想想每个数会被累加几次

发现对于一个a,b,c,da,b,c,d的询问A矩阵每一列都只有在[a,c][a,c]之间的部分会累加(d−b+1)(d-b+1)次,对于B矩阵同理

瞬秒

但是

快写没开LL快写没开LL

代码

C++
#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
//#define int unsigned long long
ll a[2010][2010],b[2010][2010];
inline void write(ll x){
    if(x<0) x=-x,putchar('-');
    if(x>9) write(x/10);
    putchar('0'+x%10);
}int n,m;
int main(){
//        write(1145141919);
    freopen("matrix.in","r",stdin);
    freopen("matrix.out","w",stdout);



    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    cin>>n>>m;

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            cin>>a[i][j],a[i][j]+=a[i-1][j];

    }
    //cout<<a[n][n]<<endl;

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            cin>>b[i][j],b[i][j]+=b[i][j-1];//for(int i=1;i<=n;i++) b[n][i]+=b[n][i-1];
            //a[i][n]+=a[i-1][n];
    }


    for(int i=1;i<=m;i++){
        ll x,y,z,w;
        cin>>x>>y>>z>>w;
        if(x>z) swap(x,z);
        if(y>w) swap(y,w);
        //cout<<a[z][n]-a[x-1][n]<<endl;
        ll ans=0;

        for(int j=1;j<=n;j++){
            ans+=1ll*(a[z][j]-a[x-1][j])*(b[j][w]-b[j][y-1]);
        }
        cout<<ans<<"\n";
    }


    return 0;
}

T2 斗转星移

题目

平面上有 nn 个整数坐标点。如果将点 (x0,y0)(x_0,y_0) 移动到 (x1,y1)(x_1,y_1) 。则需要的代价为 abs(x0−x1)+abs(y0−y1)abs(x_0-x_1)+abs(y_0-y_1) 。求使得 个点在同一位置上最少需要的代价。

做法

很常见的类型,将所有 x,yx,y 枚举配对所产生的n2n^2个点就是终点的所有方案,对所有点到所有终点的路径排序前缀和枚举比较即可

代码

C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int dis[52][52][52];

int x[52],y[52];
int n;

int main(){
//        write(1145141919);
    freopen("tower.in","r",stdin);
    freopen("tower.out","w",stdout);



    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x[i]>>y[i];
    }

    for(int i=1;i<=n;i++){
        for(int o=1;o<=n;o++){


            for(int j=1;j<=n;j++){
                dis[i][o][j]=abs(x[i]-x[j])+abs(y[o]-y[j]);
            }

            for(int j=1;j<=n;j++){
                for(int k=1;k<n;k++){
                    if(dis[i][o][k]>dis[i][o][k+1]) swap(dis[i][o][k],dis[i][o][k+1]);
                }
            }
//if(dis[i][o][2]==4) cout<<x[i]<<" "<<y[o]<<endl;0
            for(int j=1;j<=n;j++){

                //cout<<dis[i][o][j]<<" ";
                dis[i][o][j]+=dis[i][o][j-1];
            }//cout<<"\n";
        }
    }

    for(int i=1;i<=n;i++){
        int mn=0x3f3f3f3f;
        for(int j=1;j<=n;j++){
            for(int k=1;k<=n;k++)
            mn=min(mn,dis[j][k][i]);
        }
        cout<<mn<<"\n";
    }

    return 0;
}

T3 树树数数

题目

给一棵树,你可以匹配有边相连的两个点,问你这棵树的最大匹配是多少,并且计算出有多少种最大匹配。

做法

题目短是本T3唯一的亮点

对于60分,随便设状态dpi,0/1dp_{i,0/1}为点ii跟/不跟自己儿子匹配的最大匹配和数量(开结构体),稍微分类讨论一下结果就出来了

对于100分,只需把结构体内部的整数换成高精度类型即可

有个人高精度没初始化调了2h

代码

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long


const int N = 2010;

int n;
bool bj[N];

int head[N], ver[N * 2], nxt[N * 2], tot;

void add(int x, int y) {
    ver[++tot] = y;
    nxt[tot] = head[x];
    head[x] = tot;
}

struct number {
    int a[152], cnt = 1;
    void init() { cnt = 1, memset(a, 0, sizeof a); }
    friend number operator+(number x, number y) {
        number c;
        c.init();
        c.cnt = max(x.cnt, y.cnt);
        for (int i = 1; i <= c.cnt; i++) c.a[i] = x.a[i] + y.a[i];
        for (int i = 1; i <= c.cnt; i++) {
            if (c.a[i] >= 10) {
                c.a[i + 1] += c.a[i] / 10;
                c.a[i] %= 10;
                if (i == c.cnt)
                    c.cnt++;
            }
        }
        return c;
    }

    friend number operator*(number x, number y) {
        number c;
        c.init();
        c.cnt = x.cnt + y.cnt - 1;

        for (int j = 1; j <= x.cnt; j++)
            for (int i = 1; i <= y.cnt; i++) c.a[i + j - 1] += x.a[j] * y.a[i];

        for (int i = 1; i <= c.cnt; i++) {
            if (c.a[i] >= 10) {
                c.a[i + 1] += c.a[i] / 10;
                c.a[i] %= 10;
                if (i == c.cnt)
                    c.cnt++;
            }
        }
        return c;
    }
    void giv(int p) {
        cnt = 0;
        while (p) {
            a[++cnt] = p % 10;
            p /= 10;
        }
        if (!cnt)
            cnt = 1;
    }
    void ot() {
        for (int i = cnt; i >= 1; i--) cout << a[i];
        cout << "\n";
    }
};

struct node {
    int ans;
    number cnt;
} dp[N][2];

node cmp(node a, node b) {
    if (a.ans > b.ans)
        return a;
    if (b.ans > a.ans)
        return b;
    // node res;res.cnt.init();
    a.cnt = a.cnt + b.cnt;
    // a.ans=a.ans;
    return a;
}

node adnode(node a, node b) {
    node res;
    res.ans = a.ans + b.ans;
    // res.cnt.init();
    res.cnt = a.cnt * b.cnt;
    // a.ans=a.ans+b.ans;
    return res;
}

void dfs(int x) {
    // cout<<x<<"\n";
    dp[x][0].ans = 0, dp[x][1].ans = -0x3f3f3f3f;
    dp[x][0].cnt.giv(1);
    dp[x][1].cnt.giv(1);

    for (int i = head[x]; i; i = nxt[i]) {
        //        if(x==2) dp[x][0].cnt.ot();
        int y = ver[i];
        dfs(y);
        node gt;
        gt.ans = dp[y][0].ans + 1;
        gt.cnt = dp[y][0].cnt;
        if (dp[x][1].ans < 0)
            dp[x][1] = gt;
        else
            dp[x][1] = cmp(adnode(dp[x][1], cmp(dp[y][1], dp[y][0])), adnode(dp[x][0], gt));
        dp[x][0] = adnode(dp[x][0], cmp(dp[y][1], dp[y][0]));
    }
}

signed main() {
    //        write(1145141919);
    freopen("TREE.in", "r", stdin);
    freopen("TREE.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;

    for (int i = 1; i <= n; i++) {
        int p;
        int m;
        cin >> p >> m;
        for (int j = 1; j <= m; j++) {
            int x;
            cin >> x;
            add(p, x);
            bj[x] = 1;
        }
    }

    int root;
    for (int i = 1; i <= n; i++) {
        if (!bj[i])
            root = i;
    }
    dfs(root);
    node endans = cmp(dp[root][0], dp[root][1]);
    cout << endans.ans << "\n";
    endans.cnt.ot();

    return 0;
}

总结

  1. 读写,今天没犯,进步很大
  2. 数据范围,今天没犯,进步很大
  3. LLLLLLLLLLLLLLLLLLLLLLLLLLLL
  4. 初始化初始化初始化初始化初始化初始化
原文