首页 > 其他分享 >机试重点题-2021/2023

机试重点题-2021/2023

时间:2024-03-22 20:00:59浏览次数:22  
标签:dist int void cost 2021 2023 机试 include ar

2021

5:由二叉树前々序列和中々序列得到后々序列列

 

#include <iostream>
#include <unordered_map>

using namespace std;

const int N = 50010;
int n;
int a[N], b[N]; //前序,中序 
unordered_map<int, int> p;

void build(int al, int ar, int bl, int br)
{
    if(al > ar) return;
    int root = a[al];
    int k = p[root];
    //画图更直观 
    build(al + 1, ar - br + k, bl, k - 1); //左 
    build(ar -br + k + 1, ar, k + 1, br); //右 
    int ans = root; //访问根结点 
    cout << ans << " ";
}

int main()
{
    cin >> n;
    for(int i = 0; i<n; i++) cin >> a[i];
    for(int i = 0; i<n; i++)
    {
        cin >> b[i];
        p[b[i]] = i;
    }
    build(0, n-1, 0, n-1);
    
    return 0;
}

/*
5
3 9 20 15 7
9 3 15 20 7
output:9 15 7 20 3
*/
View Code

 

 

2023

A:单源最短路径问题(双权值)

HDU 3790:多目标最短路径

考察:单源最短路径(Dijkstra)

#include <iostream>
#include <cstring>

using namespace std;
const int N = 1010;
int g_dist[N][N], g_cost[N][N];
int dist[N], cost[N];
bool st[N];
int n, m;
int s, e;

void dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    memset(cost, 0x3f, sizeof cost);

    dist[s] = 0;
    cost[s] = 0;
    for(int i = 0; i<n; i++) {
        int t = -1;
        for(int j = 1; j<=n; j++) {
            if(!st[j] &&(t == -1 || dist[t] > dist[j]))
                t = j;
        }
        st[t] = true;

        for(int j = 1; j<=n; j++) {
            if(dist[j] > dist[t] + g_dist[t][j]) {
                dist[j] = dist[t] + g_dist[t][j];
                cost[j] = cost[t] + g_cost[t][j];
            }
            else if(dist[j] == dist[t] + g_dist[t][j]) {
                if(cost[j] > cost[t] + g_cost[t][j]) {
                    cost[j] = cost[t] + g_cost[t][j];
                }
            }
        }
    }
    return;
}

int main()
{
    memset(g_dist, 0x3f, sizeof g_dist);
    memset(g_cost, 0x3f, sizeof g_cost);

    cin >> n >> m;
    while(m--) {
        int a, b, d, w;
        cin >> a >> b >> d >> w;
        g_dist[a][b] = g_dist[b][a] = min(g_dist[a][b], d);
        g_cost[a][b] = g_cost[b][a] = min(g_cost[a][b], w);
    }

    cin >> s >> e;

    dijkstra();
    cout << dist[e] << " " << cost[e];

    return 0;
}
View Code

 

B:最长上升子序列之和

考察:动态规划

//最大上升子序列之和
#include <iostream>

using namespace std;

const int N = 1010;
int n;
int f[N];
int a[N];
int main()
{
    cin >> n;
    for(int i = 1; i<=n; i++)
        cin >> a[i]; //各自的数值

    for(int i = 1; i<=n; i++) {
        f[i] = a[i]; //f[i]:以a[i]为结尾的序列之和
        for(int j = 1; j<i; j++) {
            if(a[j] < a[i]) {
                f[i] = max(f[i], f[j]+a[i]);
            }
        }
    }

    int res = 0;
    for(int i = 1; i<=n; i++) {
        res = max(f[i], res);
    }
    cout << res;
    return 0;
}
View Code

 

C:调整大根堆

考察:堆排序

#include <iostream>

using namespace std;

const int N = 10010;
int h[N], size;
int cnt;
int n;

void up(int u)
{
    int t = u;
    int left = u * 2;
    int right = u * 2 + 1;
    if(left <= size && h[left] > h[t]) t = left;
    if(right <= size && h[right] > h[t]) t = right;
    
    if(t != u){
        swap(h[t], h[u]);
        cnt ++;
        up(t);
    }
}

void up_2(int u)
{
    while(u / 2 != 0 && h[u/2] < h[u])
    {
        swap(h[u/2], h[u]);
        cnt ++;
        u /= 2;
    }
}

//从n/2~1处不断向下调整 
void down(int u)
{
    int t = u;
    int left = 2 * u;
    int right = 2 * u + 1;
    
    if(left <=size && h[t] < h[left]) t = left;
    if(right <= size && h[t] < h[right]) t = right;
    
    if(t != u)
    {
        swap(h[t], h[u]);
        cnt ++;
        down(t);
    }
}

int main()
{
    cin >> n;
    size = n;
    
    for(int i = 1; i<=n; i++)
        cin >> h[i];
        
    for(int i = n/2; i>0; i--) down(i);
    
    for(int i = 1; i<=n; i++)
        cout << h[i] << " ";
    cout << endl;
    cout << "cnt = " << cnt << endl;
    
    
    return 0;
}
/*
8
53 17 78 9 45 65 87 32
7
6 1 5 9 8 4 7
output:
87 45 78 32 17 65 53 9
cnt = 5
9 8 7 1 6 4 5
cnt = 4
*/
View Code

 

标签:dist,int,void,cost,2021,2023,机试,include,ar
From: https://www.cnblogs.com/GeekDragon/p/18088985

相关文章

  • CCF软件能力认证202312-1——仓库规划
    问题描述西西艾弗岛上共有个仓库,依次编号为。每个仓库均有一个维向量的位置编码,用来表示仓库间的物流运转关系。具体来说,每个仓库均可能有一个上级仓库,满足:仓库位置编码的每一维均大于仓库位置编码的对应元素。比如编码为的仓库可以成为的上级,但不能成为的上级。如......
  • ubuntu安装cuda和cudnn,并测试tensorflow和pytorch库的与cuda的兼容性(2023年版)
    lspci|grep-invidia查看nvidia设备,看到GPUgcc--version检查是否安装上gcc软件包根据官方文档指示,pipinstalltorch==1.13.1+cu117-fhttps://download.pytorch.org/whl/torch_stable.html,pipinstalltorchaudio==0.13.1+cu117-fhttps://download.pytorch.org/whl/torch......
  • 2023-5-11-elasticsearch使用
    索引操作、数据操作索引操作索引的创建、删除等创建索引PUT/shopping{"acknowledged":true,"shards_acknowledged":true,"index":"shopping"}获取索引详细信息GET/_cat/indices?vhealthstatusindexuuid......
  • 2023-5-3-skywalking基本使用
    服务探针配置、服务安装服务配置探针无侵入配置1.下载agent并解压skywalking-agent├──LICENSE├──NOTICE├──activations├──bootstrap-plugins├──config├──licenses├──logs├──optional-plugins├──optional-reporter-plugins├─......
  • 2023-8-10-canal使用
    工作原理、mysql开启二进制日志、启动服务端、启动客户端工作原理canal模拟MySQLslave的交互协议,伪装自己为MySQLslave,向MySQLmaster发送dump协议MySQLmaster收到dump请求,开始推送binarylog给slave(即canal)canal解析binarylog对象(原始为byte流......
  • 2023-6-20-springboot中使用es
    依赖、配置、定义索引对象、操作、其他依赖<!--Maven--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>配置spring:elasticsearch:......
  • 2023-12-22-flink-cdc使用
    应用场景、上手体验应用场景FlinkCDC(ChangeDataCapture)是一种用于捕获和处理数据源中的变化的流处理技术。它可以实时地将数据源中的增量更新捕获到流处理作业中,使得作业可以实时响应数据变化。以下是FlinkCDC的一些常见应用场景:数据仓库和实时分析:FlinkCDC可以......
  • 2023-12-5-logstash和filebeat使用
    应用场景、组件介绍、logstash启动、filebeat启动应用场景分布式场景中,不同服务器的服务日志集中收集管理,方便排查问题组件介绍logstash日志收集器,将接受到的日志存储到ES中fielbeat日志解析器,将日志解析后通过网络发送给日志收集器logstash启动下载https://www.elastic......
  • 2021-4-30-正则表达式总结
    元字符、反义、转义、贪婪与懒惰、分组、后向引用、零宽断言、Python中的re模块参考文档https://deerchao.cn/tutorials/regex/regex.htm元字符元字符说明.匹配除换行符以外的其他任意字符\w匹配字母、数字、下划线、汉字\s匹配任意空白字符\d匹配任意......
  • 2021-4-10-达梦数据库
    模式、状态、状态切换、表空间、GROUPBY、JOIN语句报错、事务模式达梦数据库支持3中模式:Normal模式、Primary模式和Standby模式。1)Normal模式用户可以正常访问数据库,操作没有限制。正常生成本地归档,但不发送实时归档(Realtime)、即时归档(Timely)和异步归档(Async)。将数据......