2022 04 05学习记录

早上补牛客比赛题

下午一觉睡到4点 复习会数学吧

gk的树

题目链接:https://ac.nowcoder.com/acm/contest/30825/F

反省 在队友做出了数学题的时候自己放弃了 其实这题应该在我的能力范围内 没有写出这题很不应该Description:

​ 给定一棵树,每次操作可以删一条边 最少需要多少次操作使得每个节点的度数都<=k

Solution:

​ 从叶子节点开始处理 若叶子节点度数大于k 就删边 模拟一下倒推回根节点 可以用dfs 也可以dp

Code:

const int N = 100010;
int n, k, res;
int cnt[N]; //度数和
vector<int> e[N];

void dfs(int u, int fa)
{
    cnt[u] = (fa > 0);
    for(auto v : e[u])
    {
        if(v == fa) continue;
        dfs(v, u);
        cnt[u] ++;
    }
    if(cnt[u] > k)  res += cnt[u] - k, cnt[fa] --; 
   	//因为下面的树怎么砍都无法对上面做贡献了 所以只需要对父节点的度数 -1
}

void solve()
{
    cin >> n >> k;

    for(int i = 1; i <= n; i++)
        e[i].clear();
    
    for(int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        e[u].pb(v);e[v].pb(u);
    }

    res = 0;
    dfs(1, 0);
    cout << res << endl;
}

迷宫(BFS + 最短路)

题目链接:https://ac.nowcoder.com/acm/contest/30825/C

Description:

​ 三维走迷宫 移动方式 上下左右前后

​ 游戏的过程中会出现两种操作

​ 1 x y z 在xyz处出现一个新的出口

​ 2 x y z 玩家在xyz处复活 询问最少移动次数达到出口易 并输出移动次数

​ 确保在操作2出现之前必定存在操作1

Solution:

​ 看起来就是一个A* 当时嫌麻烦不想写 心态是错误的 如果因为麻烦就不想写的话 那还怎么过题?

​ 但其实也不是写A* 写一个其实是一个最短路的思维+BFS就可以了

​ 我们就用最短路的思路去写 dis数组记录的是离坐标ijk最近的出口需要多少步 初始化为正无穷

​ 当每次新加入一个出口的时候 将出口入队

​ 每次询问的时候 就把队列跑到空为止 更新最短路径

Code:

int n, m, h, Q;

bool check(int x, int y, int z)
{
    if(x > n || y > m || z > h || x < 1 || y < 1 || z < 1)  return true;
    return false;
}

int d[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, -1, 0}, {0, 1, 0}, {1, 0, 0}, {-1, 0, 0}};

void solve()
{
    scanf("%d%d%d%d", &n, &m, &h, &Q);
    vector<vector<vector<int>> >dis(n + 5, vector<vector<int> >(m + 5, vector<int>(h + 5)));

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            for(int k = 1; k <= h; k++)
                dis[i][j][k] = inf;

    int op, x, y, z;
    queue<pair<int, pair<int, int>> > q;
    while(Q --)
    {
        scanf("%d%d%d%d", &op, &x, &y, &z);
        if(op == 1)
        {
            q.push({x, {y, z}});
            dis[x][y][z] = 0;
        }
        else if(op == 2)
        {
            while(q.size())
            {
                auto t = q.front();
                q.pop();
                int x1 = t.first, y1 = t.second.first, z1 = t.second.second;
                for(int i = 0; i < 6; i++)
                {
                    int xx = x1 + d[i][0];
                    int yy = y1 + d[i][1];
                    int zz = z1 + d[i][2];
                    if(check(xx, yy, zz))   continue;
                    if(dis[x1][y1][z1] + 1 < dis[xx][yy][zz])
                    {
                        dis[xx][yy][zz] = dis[x1][y1][z1] + 1;
                        q.push({xx,{yy, zz}});
                    }
                }
            }
            printf("%d\n",dis[x][y][z]);
        }
    }
}

int main()
{
	int _;
	scanf("%d", &_);
	while(_--)
	{
		solve();
	}
	return 0;
}

大数乘法(快速幂)

题目链接:https://ac.nowcoder.com/acm/contest/30825/J

Description:

​ 给定整数x,y,你需要输出x^y mod p 的值

​ 0 ≤ x ≤ 100000,0 ≤ y ≤ 10^100000, 100000 ≤ p ≤ 1000000007

Solution:

利用快速幂的思想处理 这个公式 并且用快速幂进行计算
x1234=x14∗x103∗x1002∗x10001 x^{1234}={x^1}^{4}*{x^{10}}^3*{x^{100}}^2*{x^{1000}}^1 x1234=x14x103x1002x10001
Code:

LL qpow(LL a, LL b, LL mod)
{
    LL res = 1;
    a %= mod;
    while(b)
    {
        if(b & 1)   res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

void solve()
{
    LL x, mod;
    stringt s;
    cin >> x;
    cin >> s;
    cin >> mod;

    int n = s.size();
    LL res = qpow(x, s[n - 1] - '0', mod);

    LL inside = x;
    for(int i = n - 2; i >= 0; i--) //因为第一位是pow(x,0)为了统一写法 循环从第二位开始
    {
        LL k = s[i] - '0';
        inside = qpow(inside, 10, mod); //预处理内层
        res = res * qpow(inside, k, mod) % mod;
    }
    cout << res << endl;
}
### 构建代码模型以分析X的统计特性变化 要通过给定的数据来预测 $ X $ 的方差、标准差和均的变化趋势,可以采用时间序列分析方法。以下是完整的解决方案: #### 数据准备 假设输入数据是以时间为索引的序列 $ X = \{x_1, x_2, ..., x_n\} $,每一条记录对应于某个时刻的观测。 #### 方差计算公式 方差定义为数据点相对于均的平方偏差的平均[^1]: $$ \sigma^2 = \frac{\sum_{i=1}^{n}(x_i - \bar{x})^2}{n} $$ 其中 $\bar{x}$ 是样本均。 #### 均与标准差 均可以通过以下公式计算: $$ \bar{x} = \frac{\sum_{i=1}^{n}x_i}{n} $$ 标准差则是方差的平方根: $$ \sigma = \sqrt{\sigma^2} $$ #### 时间序列分析 如果目标是观察这些统计量随时间的趋势,则需要将整个数据划分为多个时间段,并分别计算每个时间段内的方差、标准差和均。这可以通过滑动窗口技术实现。 下面提供一段 Python 实现代码: ```python import pandas as pd import numpy as np def calculate_statistics(data, window_size): """ 计算指定窗口大小下的均、方差和标准差。 参数: data (pd.Series): 输入的时间序列数据。 window_size (int): 滑动窗口的大小。 返回: DataFrame: 包含各窗口的均、方差和标准差的结果表。 """ rolling_data = data.rolling(window=window_size) mean_values = rolling_data.mean() variance_values = rolling_data.var(ddof=0) # ddof=0 表示总体方差而非样本方差 std_deviation_values = rolling_data.std(ddof=0) result_df = pd.DataFrame({ 'Mean': mean_values, 'Variance': variance_values, 'StdDeviation': std_deviation_values }) return result_df.dropna() # 示例数据 data = pd.Series(np.random.randn(100), index=pd.date_range('2023-01-01', periods=100)) window_size = 10 # 定义窗口大小 statistics_over_time = calculate_statistics(data, window_size) print(statistics_over_time.head()) ``` 此代码实现了基于滚动窗口的统计量计算功能。`calculate_statistics` 函数接收原始时间序列 `data` 和窗口大小参数 `window_size`,返回一个包含每一窗口对应的均、方差和标准差的新表格。 #### 趋势判断逻辑 为了进一步评估这些统计量是否呈现增长或减少的趋势,可利用简单的比较法或者更复杂的机器学习算法(如线性回归)拟合曲线并推断斜率方向。例如: ```python from sklearn.linear_model import LinearRegression def detect_trend(values): """ 判断数列表是否存在单调增加/减少趋势。 参数: values (list or array-like): 需要检测趋势的一维数组。 返回: str: 描述趋势的文字说明 ("Increasing", "Decreasing", 或者 "No Clear Trend")。 """ indices = np.arange(len(values)).reshape(-1, 1) model = LinearRegression().fit(indices, values) slope = model.coef_[0] if slope > 0: return "Increasing" elif slope < 0: return "Decreasing" else: return "No Clear Trend" mean_trend = detect_trend(statistics_over_time['Mean'].values) variance_trend = detect_trend(statistics_over_time['Variance'].values) stddev_trend = detect_trend(statistics_over_time['StdDeviation'].values) print(f"Mean trend: {mean_trend}") print(f"Variance trend: {variance_trend}") print(f"Standard Deviation trend: {stddev_trend}") ``` 以上脚本能够自动识别三个主要统计特征——均、方差以及标准差的整体变动模式。 --- #### 结果解读 最终输出将是三类趋势描述:“Increase”代表该属性随着时间逐渐上升;“Decrease”则表明下降倾向;而“No Clear Trend”意味着无明显规律可循。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值