SF-X未来科技赛道-编程方向(半决赛)(签到题 + 设计题 + 最短路径 + 贪心)

顺丰小哥分拣快递

顺丰小哥是顺丰的仓管员,他每天都需要对快递进行分拣。

我们用不同的字符表示快递的类型,共有小写字母、大写字母、数字三种类型。顺丰小哥需要把相同类型的快递分拣到一起。请你编写一个程序完成这个操作。

思路

很简单的一道签到题

代码

#include <bits/stdc++.h>
using namespace std;

int main() {
    string s;
    cin >> s;
    string s1 = "", s2 = "", s3 ="";
    for (int i = 0; i < s.length(); i ++) {
        if (s[i] >= '0' && s[i] <= '9') {
            s3 += s[i];
        } else if (s[i] >= 'a' && s[i] <= 'z') {
            s1 += s[i];
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
            s2 += s[i];
        }
    }
    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;

    return 0;
}

顺丰管理处罚系统

顺丰里面有一个处罚管理系统,为了管理处罚系统,请你帮忙实现如下函数:
List query(int pageNo, int pageSize) :查询处罚记录,记录按照id升序排序
List getByUserId(int userId) :查询某个用户的处罚记录,记录按照id升序排序
int punish(String operatorUserName, int userId, int punishStatus) : 处罚操作, 如果用户已经有被处罚了,新的处罚必须高于当前处罚等级才能生效,operatorUserName为执行处罚的人,userId为被处罚用户的编号
int relieve(String operatorUserName, int userId) : 解除当前处罚,operatorUserName为解除处罚的人,userId为被解除处罚用户的编号。若当前用户无处罚记录则该操作无效。解除处罚的status设置为0

只有有效的处罚或解除处罚操作,才会被记录id。这两种操作的返回值为该操作的id,从1开始自增。若操作为无效操作,返回-1

思路

个人觉得能在编程比赛中看到这种题型能还是比较开心的,贴合实际场景,考验选手对数据结构的实际设计与应用能力。
具体在这道题目中,我用的是Java,所以基本上对我来说就是在考察Stream的用法。

代码

import java.util.*;
import java.util.stream.*;

class HistoryResult {
    private int id;
    private int userId;
    private String operatorUserName;
    private int status;

    HistoryResult(int id, int userId, String operatorUserName, int status) {
        this.id = id;
        this.userId = userId;
        this.operatorUserName = operatorUserName;
        this.status = status;
    }

    public int getId() {
        return id;
    }

    public int getUserId() {
        return userId;
    }

    public String getOperatorUserName() {
        return operatorUserName;
    }

    public int getStatus() {
        return status;
    }
}

public class Main {

    List<HistoryResult> history = new ArrayList<>();
    Map<Integer, List<Integer>> userHistory = new HashMap<>();


    /**
     * 查询处罚记录,记录按照id升序排序
     *
     * @param pageNo   第几页
     * @param pageSize 页数大小
     * @return 查询结果
     */
    public List<HistoryResult> query(int pageNo, int pageSize) {
        int start = pageSize * (pageNo - 1) <= 0 ? 0 : pageSize * (pageNo - 1);
        if (start >= history.size()) return null;
        int end = pageNo * pageSize > history.size() ? history.size() : pageNo * pageSize;
        if (end < 0) return null;
        List<HistoryResult> res= history.subList(start, end);
        return res;
    }

    /**
     * 查询某个用户的处罚记录,记录按照id升序排序
     *
     * @param userId 用户
     * @return 处罚记录
     */
    public List<HistoryResult> getByUserId(int userId) {
        return history.stream().filter(e -> e.getUserId() == userId).collect(Collectors.toList());
    }

    /**
     * 处罚操作, 如果用户已经有被处罚了,新的处罚不能低于当前处罚等级才能生效
     *
     * @param operatorUserName 操作者的名字
     * @param userId           处罚的用户
     * @param punishStatus     处罚类型
     * @return 返回处罚的记录id, 处罚不成功返回-1
     */
    public int punish(String operatorUserName, int userId, int punishStatus) {
        List<Integer> list = userHistory.get(userId);
        if (list != null) {
            if (list.size() > 0) {
                int recordNo = list.get(list.size() - 1);
                int lastStatus = history.get(recordNo).getStatus();
                if (punishStatus < lastStatus) return -1;
            }
        } else {
            list = new ArrayList<>();
        }
        history.add(new HistoryResult(history.size() + 1, userId, operatorUserName, punishStatus));
        list.add(history.size() - 1);
        userHistory.put(userId, list);
        return history.size();
    }

    /**
     * 解除当前处罚
     *
     * @param operatorUserName 操作者的名字
     * @param userId           解除处罚的用户
     * @return 如果当前用户正在被处罚中,解除当前处罚,返回处罚记录id,如果用户没有被处罚,返回-1表示解除处罚非法
     */
    public int relieve(String operatorUserName, int userId) {
        List<Integer> list = userHistory.get(userId);
        if (list == null) return -1;

        List<Integer> numList = list.stream().filter(e -> {
            HistoryResult tmp = history.get(e);
            return tmp.getStatus() != 0;
        }).collect(Collectors.toList());

        if (numList == null || numList.size() <= 0) return -1;
        list.removeAll(numList);
        userHistory.put(userId, list);
        history.add(new HistoryResult(history.size() + 1, userId, operatorUserName, 0));
        return history.size();
    }

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        Scanner in = new Scanner(System.in);
        List<HistoryResult> printHistory = new ArrayList<>();
        List<Integer> printOperators = new ArrayList<>();
        int opNum = Integer.parseInt(in.nextLine());
        while (in.hasNext()) {
            String op = in.nextLine();
            String data = in.nextLine();
            if (op.equals("punish")) {
                String[] punishData = data.split(" ");
                String operatorUserName = punishData[0];
                int userId = Integer.parseInt(punishData[1]);
                int punishStatus = Integer.parseInt(punishData[2]);
                printOperators.add(main.punish(operatorUserName, userId, punishStatus));
            } else if (op.equals("relieve")) {
                String[] relieveData = data.split(" ");
                String operatorUserName = relieveData[0];
                int userId = Integer.parseInt(relieveData[1]);
                printOperators.add(main.relieve(operatorUserName, userId));
            } else if (op.equals("getByUserId")) {
                int userId = Integer.parseInt(data);
                List<HistoryResult> results = main.getByUserId(userId);
                if (results != null && !results.isEmpty()) {
                    printHistory.addAll(results);
                }
            } else if (op.equals("query")) {
                String[] queryData = data.split(" ");
                int pageNo = Integer.parseInt(queryData[0]);
                int pageSize = Integer.parseInt(queryData[1]);
                List<HistoryResult> results = main.query(pageNo, pageSize);
                if (results != null && !results.isEmpty()) {
                    printHistory.addAll(results);
                }
            } else {
                throw new Exception("错误的输入");
            }
        }
        printHistory.forEach(res -> System.out.println(res.getId() + "_" + res.getUserId() + "_" + res.getOperatorUserName() + "_" + res.getStatus()));
        printOperators.forEach(System.out::println);
    }
}

顺丰小哥送快递

地图上有n个城市,m条道路。每个道路连接着两个城市。顺丰小哥需要将快递从1号城市运到n号城市,
另外,顺丰小哥有一个魔法,他可以花费x时间从一个城市传送到任意另一个城市。为了避免暴露自己会魔法的事实,他不会在起点和终点使用魔法(也不会作为魔法的目的地),且魔法最多只能使用一次。
顺丰小哥想知道,自己完成送快递至少需要多少时间?

思路

  • 看到数据范围能考虑到图的存储以邻接表的方式存储,
  • 算法思路是:找到需要使用魔法的路径和不需要使用魔法的路径
    • 分别找到距离起点、终点最近的点(a,b),使用魔法的距离:x + dis(0, a) + dis(b, n)
    • 利用dijstra算法找到(1, n)的最短路径
    • 比较前两者的大小即可

代码

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, int> PII;
const int N = 300000 + 100;
const long long INF = 1e18;

int h[N], ne[N], e[N], idx;
ll w[N];
ll d[N];
bool st[N];
int n, m, x;

void add(int x, int y, ll z)
{
    e[idx] = y;
    ne[idx] = h[x];
    w[idx] = z; // w存储x到y的权值
    h[x] = idx ++;
}

ll dijkstra(){
    priority_queue<PII, vector<PII>, greater<PII>> q;
    d[1] = 0;

    q.push({0, 1});
    while(!q.empty())
    {
        // 取出队列中第一个节点(最短路径的节点)
        auto head = q.top();
        q.pop();

        ll distance = head.first;
        int t = head.second;
        // 若用此节点的最短路径更新过其他节点,则跳出。
        if (st[t]) continue;
        st[t] = true;
        // 找到此节点能到达的节点,然后更新他们到起始点的最短距离。
        for (int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (d[j] > w[i] + distance)
            {
                d[j] = w[i] + distance;
                q.push({d[j], j});
            }
        }
    }

    return d[n];
}

int main() {
    scanf("%d%d%d", &n, &m, &x);
    
    for (int i = 0; i <= n; i ++) {
        d[i] = INF;
    }
    memset(h, -1 , sizeof h);
    ll min1 = INF, min2 = INF;
    for (int i = 0; i < m; i ++)
    {
        int a, b;
        ll z;
        scanf("%d%d%lld", &a, &b, &z);
        add(a, b, z);
        add(b, a, z);
        if (a == 1 || b == 1) min1 = min(min1, z);
        if (a == n || b == n) min2 = min(min2, z);
    }
    
    ll ans = dijkstra();

    ans = min(ans, min1 + min2 + x);
    if (ans >= INF) ans = -1;
    printf("%lld\n", ans);
    return 0;
}

顺丰主管调度

顺丰主管将对他手下的兼职快递员们进行任务安排。
已知快递分为两种类型,第一种类型为同城快送,第二种类型为跨城运输。顺丰主管手下有n个兼职快递员,每个快递员的工资分别为a_i,运送快递的顾客满意值为b_i。快递员有两种类型:类型A只能送同城快运,类型B则可以送任意快递。
顺丰主管共有x个同城快送任务,y个跨城运输任务。他需要把这些快递派发给他手下的快递员,每个人最多只能接一个任务。顺丰主管想知道,在支付总工资尽可能小的情况下,最终的顾客满意值之和最大是多少?
注:任务必须全部接完。每个快递员只能接一个任务。

思路

贪心,题目要求在支付总工资尽可能把小的情况下,求最大的顾客满意度之和,所以需要按照工资(从小到大)、顾客满意度(前者相同的情况下按从大到小)两个维度来排序。这样从前到后遍历的过程中,总是安排薪酬较低的兼职快递员去送货物,同时,对于类型B快递员来说,先送跨城运输、后送同城快送可以尽可能的保证任务被接收完。

代码

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5  + 100;

typedef struct {
    int x, y;
    char c;
} node;

typedef long long ll;

node a[N];
int n, x, y;

bool cmp(node &a, node &b) {
    if (a.x == b.x) return a.y > b.y;
    return a.x < b.x;
}

int main() {
    cin >> n >> x >> y; 

    for (int i = 0; i < n; i ++) {
        cin >> a[i].c >> a[i].x >> a[i].y;
    }
    sort(a, a + n, cmp);

    ll ans = 0;
    for (int i = 0; i < n; i ++) {
        if (a[i].c == 'A') {
            if (x) {
                ans += a[i].y;
                x --;
            }
        } else {
            if (y) {
                ans += a[i].y;
                y --;
            } else if (x) {
                ans += a[i].y;
                x --;
            }
        }
    }
    if (x == 0 && y == 0) cout << ans << endl;
    else cout << -1 << endl; 
    return 0;
}```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Honyelchak

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值