2024年3月20日-暑期实习-第一题(100分)-塔子哥安排座位

在线评测链接

题目描述

一列具有 m 个座位的火车,从起点到终点共停靠 n 个站点,站点编号从0到n-1。发车前有x名乘客预定了座位,因为预定数量可能超出座位数,为了保证效率最大化,请计算如何分配才能是座位利用率最大,并输出最大的座位利用数。

说明:

座位利用数定义为每个座位被使用的站数。例如有两个座位,第一个座位从第 0到 10 站有人坐(表示从0站上车,10站下车,第10站不占座,所以利用率是10-0=10),第二个座位从第1到9站有人坐,则座位利用率为(10-0)+(9-1)=18。乘客在某站下车后,其他乘客从这一站就可以开始使用这个座位;无需考虑乘客需要更换座位的问题,保证任意时刻列车上乘客数量不超过 m 即可

输入描述

第一行输入 m、n和x三个数字,分别表示列车座位数量、停靠站点数量和预定乘客数量

1 <= m <= 9

2 <= n <= 20

1 <= x <= 9

接下来x行输入,表示x条预定记录,每行有两个输入,分别表示此预定记录的上车站点和下车站点

输出描述

一个整数。

样例

输入

2 11 4
0 1
1 9
0 10
3 8

输出

19

说明

选择前三位乘客可以使座位利用率最大:19=(1-0)+(9-1)+(10-0)。若选择后两位乘客,则利用率为15=(10-0)+(8-3)。若选择全部四位乘客,则第3到8站车上存在3名乘客,超出列车座位数。

输入

1 11 2
0 5
5 10

输出

10

题目思路

可以暴力模拟所有情况,使用二进制枚举的方法。

用0和1代表乘客不乘坐和乘坐两种情况,用一个数字可以代表所有乘客是否乘坐,枚举这个数的二进制位即可。

对于每一种情况,模拟当前是否有人乘坐对应站的对应座位,当某一站的乘客数量超过m时,就说明该情况人数太多,说明不符合条件。

直接求所有符合条件的情况的最大利用数即可。时间复杂度 O ( n ∗ x ∗ 2 x O(n * x * 2^x O(nx2x)$

代码

Java

import java.util.*;

public class Main {

    static class Pair {
        int x, y;

        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    public static void solve() {
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt();
        int n = scanner.nextInt();
        int x = scanner.nextInt();
        List<Pair> a = new ArrayList<>();
        for (int i = 0; i < x; ++i) {
            int first = scanner.nextInt();
            int second = scanner.nextInt();
            a.add(new Pair(first, second));
        }
        int ans = 0;
        for (int i = 0; i < (1 << x); ++i) {
            int[] cnt = new int[n];
            boolean ok = true;
            int res = 0;
            for (int j = 0; ok && j < x; ++j) {
                if ((i >> j & 1) == 1) {
                    for (int k = a.get(j).x; ok && k < a.get(j).y; ++k) {
                        ++cnt[k];
                    }
                    res += a.get(j).y - a.get(j).x;
                }
            }
            for (int j = 0; j < n; ++j) {
                if (cnt[j] > m) {
                    ok = false;
                    break;
                }
            }
            if (ok) {
                ans = Math.max(ans, res);
            }
        }
        System.out.println(ans);
    }

    public static void main(String[] args) {
        solve();
    }
}

C++

#include<bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;
#define x first
#define y second

void solve() {
	int m, n, x;
    cin >> m >> n >> x;
    vector<PII> a(x);
    for (int i = 0;i < x;++ i) cin >> a[i].x >> a[i].y;
    int ans = 0;
    for (int i = 0;i < 1<<x;++ i) {
    	vector<int> cnt(n, 0);
    	bool ok = true;
    	int res = 0;
    	for (int j = 0;ok && j < x;++ j) if(i >> j & 1) {
    		for (int k = a[j].x;ok && k < a[j].y;++ k) {
    			++ cnt[k];
    		}
    		res += a[j].y - a[j].x;
    	}
    	for (int j = 0;j < n;++ j) if (cnt[j] > m) {
    		ok = false;
    		break;
    	}
    	if (ok) ans = max(ans, res);
    }
    cout << ans << endl;
}

signed main() {
	solve();
}

Python

def solve():
    m, n, x = map(int, input().split())
    a = [tuple(map(int, input().split())) for _ in range(x)]
    ans = 0
    for i in range(1 << x):
        cnt = [0] * n
        ok = True
        res = 0
        for j in range(x):
            if (i >> j) & 1:
                for k in range(a[j][0], a[j][1]):
                    cnt[k] += 1
                res += a[j][1] - a[j][0]
        for j in range(n):
            if cnt[j] > m:
                ok = False
                break
        if ok:
            ans = max(ans, res)
    print(ans)

if __name__ == "__main__":
    solve()

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

塔子哥学算法

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

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

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

打赏作者

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

抵扣说明:

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

余额充值