蓝桥算法训练营—普及组 day06 (c++)

5tayhumb1e 2023-02-10 17:59:14

1.P1130 红牌(数字三角形模型)

img


1.c++代码

#include<iostream>
#include<algorithm>
using namespace std;

const int N = 2e3 + 10;

int a[N][N];
int f[N][N]; 
//f[j][i]表示当 选择第j个小组的第i个步骤工作天数 ,所用的步数的最小值 
//f[j][i] = min(f[j - 1][i],f[j - 1][i - 1]) + a[i][j];
//if(i == 1) f[j][i] = min(f[j - 1][i],f[j - 1][n]) + a[i][j];
//if(j == 1) f[j][i] = a[i][j]; 

//这题是 输入
// 2 6 1 8 
// 3 6 2 6      但是我们可以把这个矩阵先输出 列  再输出行 
// 4 2 3 6  

// 2 3 4
// 6 6 2
// 1 2 3      这样也是可以的 
// 8 6 6
 
int n,m;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> m >> n; 
    for(int i = 1; i <= n; i ++){  //n是行 ,m是列 
        for(int j = 1; j <= m; j ++){
            cin >> a[i][j];
        }    
    }
    
    for(int j = 1; j <= m; j ++){  //先枚举列,再枚举行 
        for(int i = 1; i <= n; i ++){
            if(j == 1){
                f[j][i] = a[i][j]; 
            }else if(i == 1){
                f[j][i] = min(f[j - 1][i],f[j - 1][n]) + a[i][j];
            }else {
                f[j][i] = min(f[j - 1][i],f[j - 1][i - 1]) + a[i][j];
            }
        }
    }

    int ans = INT32_MAX;
    
    for(int i = 1; i <= n;i ++){
        ans = min(f[m][i],ans);
    }
    
    cout << ans;
    return 0;
} 

2.P1296 奶牛的耳语(模拟)

img


1.c++代码

#include<iostream>
#include<algorithm>
using namespace std;

const int N = 1e6 + 10;

int a[N];
int n,d;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> n >> d;
    for(int i = 1; i <= n; i ++){
        cin >> a[i];
    }
    sort(a + 1, a + 1 + n);
    
    int cnt = 0;
    int tmp = a[1];
    for(int i = 2; i <= n; i ++){
        int j = i - 1;
        while(i > j && j >= 1){
            if(a[i] - a[j] <= d){
                cnt ++;
                j --;
            }else{
                break;
            }
        }
    }
    cout << cnt;
    return 0;
} 

3.P1591 阶乘数码(高精度乘法)

img


1.c++代码

#include<iostream>
#include<vector>
using namespace std;

int n;

vector<int> get_mul(vector<int>&A,int b){
    vector<int>ans;
    int t = 0;
    for(int i = 0; i < (int)A.size() || t; i ++){
        if(i < A.size()){
            t += A[i] * b;
        }
        ans.push_back(t % 10);
        t /= 10;
    }
    
    while(ans.size() > 1 && ans.back() == 0){
        ans.pop_back();
    } 
    return ans;
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> n;
    while(n --){
        int a,t;
        cin >> a >> t;
        
        vector<int>A;
        A.push_back(1);
        for(int i = 2; i <= a; i ++){
            A = get_mul(A,i);
        }
        int cnt = 0;
        for(int i = 0; i < A.size(); i ++){
            if(A[i] == t){
                cnt ++;
            }
        }
        cout << cnt << endl;
    } 
    return 0;
}

4.P1367 蚂蚁(贪心,模拟

img


1.c++代码

#include<iostream>
#include<algorithm>
using namespace std;

const int N = 1e5 + 10;

struct ant{
    int u;//当前所处的位置
    int direction;// 当前的方向
    int number; //蚂蚁编号 
} ants[N],backup[N];
int n,t;

bool cmp_u(ant a,ant b){  //根据位置 从小到大 排序 
    return a.u < b.u;
}

bool cmp_number(ant a,ant b){  //根据编号 从小到大 排序 
    return a.number < b.number;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> n >> t;
    for(int i = 1; i <= n; i ++){
        cin >> ants[i].u >> ants[i].direction;
        ants[i].number = backup[i].number = i;
        backup[i].u = ants[i].u; //backup 记录 蚂蚁的编号 和 初始位置(为了和变化后的位置能对应上) 
        ants[i].u += t * ants[i].direction; 
    }
    
    sort(ants + 1,ants + 1 + n,cmp_u);
    sort(backup + 1,backup + 1 + n,cmp_u);
    
    // 如果如果蚂蚁A 和蚂蚁B相撞的话,
    //(蚂蚁A在蚂蚁B的左边) 如果蚂蚁A向右,蚂蚁B向左,撞后,  蚂蚁A是往左,蚂蚁B往右 
    // 蚂蚁A 此时往左,蚂蚁B 往右,那么 蚂蚁A还是在蚂蚁B的左边
    // 所以可以 所以每只蚂蚁 如果是有碰撞的情况的话,那么它的 相对位置 是不会变的 
    for(int i = 1; i <= n; i ++){
        ants[i].number = backup[i].number;
    }
    
    for(int i = 1; i < n; i ++){
        if(ants[i].u == ants[i + 1].u){
            ants[i].direction = 0;
            ants[i + 1].direction = 0;
        }
    }
    
    sort(ants + 1, ants + 1 + n,cmp_number);
    
    for(int i = 1; i <= n; i ++){
        cout << ants[i].u << " " << ants[i].direction << endl;
    }
    
    return 0;
}

...全文
40 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,798

社区成员

发帖
与我相关
我的任务
社区描述
和众多高校算法内卷分子,一起学习和交流算法那。浓郁的算法交流氛围,拒绝躺平,有效内卷。加入我们,私信我拉你入核心内卷群。
算法数据结构leetcode 个人社区
社区管理员
  • 执 梗
  • Dream-Y.ocean
  • ღCauchyོꦿ࿐
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

 刷题!

试试用AI创作助手写篇文章吧