算法设计 第二次上机 All discs considered

本文介绍了一种算法,用于计算安装由两个DVD组成的操作系统时所需的最小DVD更换次数。该算法考虑了软件包间的依赖关系,并假设只有一个DVD光驱可用。

总时间限制: 
10000ms 
内存限制: 
65536kB
描述
Operating systems are large software artefacts composed of many packages, usually distributed on several media, e.g., discs. You probably remember the time when your favourite operating system was delivered on 21 floppy discs, or, a few years later, on 6 CDs. Nowadays, it will be shipped on several DVDs, each containing tens of thousands of packages. 

The installation of certain packages may require that other packages have been installed previously. Therefore, if the packages are distributed on the media in an unsuitable way, the installation of the complete system requires you to perform many media changes, provided that there is only one reading device available, e.g., one DVD-ROM drive. Since you have to start the installation somehow, there will of course be one or more packages that can be installed independently of all other packages. 

Given a distribution of packages on media and a list of dependences between packages, you have to calculate the minimal number of media changes required to install all packages. For your convenience, you may assume that the operating system comes on exactly 2 DVDs. 
输入
The input contains several test cases. Every test case starts with three integers N1, N2, D. You may assume that 1<=N1,N2<=50000 and 0<=D<=100000. The first DVD contains N1 packages, identified by the numbers 1, 2, ..., N1. The second DVD contains N2 packages, identified by the numbers N1+1, N1+2, ..., N1+N2. Then follow D dependence specifications, each consisting of two integers xi, yi. You may assume that 1<=xi,yi<=N1+N2 for 1<=i<=D. The dependence specification means that the installation of package xi requires the previous installation of package yi. You may assume that there are no circular dependences. The last test case is followed by three zeros.
输出
For each test case output on a line the minimal number of DVD changes required to install all packages. By convention, the DVD drive is empty before the installation and the initial insertion of a disc counts as one change. Likewise, the final removal of a disc counts as one change, leaving the DVD drive empty after the installation.
样例输入
3 2 1
1 2
2 2 2
1 3
4 2
2 1 1
1 3
0 0 0
样例输出
3
4
3
来源
Ulm Local 2004


拓扑排序,需要维护两个队列用作拓扑。


#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <queue>
#include <vector>
#include <string.h>

using namespace std;

vector<int> graph[100005];
int indegree[100005];
int n1, n2;

int topSort(int n)
{
    queue<int> dis[2];
    int in[100005];
    int cost = 1;
    memcpy(in, indegree, sizeof(indegree));
    for(int i = 1; i <= n1; i++)
        if(in[i] == 0)
            dis[0].push(i);
    for(int i = n1 + 1; i <= n1 + n2; i++)
        if(in[i] == 0)
            dis[1].push(i);
    while(!dis[0].empty() || !dis[1].empty())
    {
        while(!dis[n].empty())
        {
            int now = dis[n].front();
            dis[n].pop();
            for(int i = 0; i < graph[now].size(); i++)
            {
                in[graph[now][i]]--;
                if(in[graph[now][i]] == 0)
                {
                    if(graph[now][i] > n1)
                        dis[1].push(graph[now][i]);
                    else
                        dis[0].push(graph[now][i]);
                }
            }
        }
        if(n == 0)
            n = 1;
        else
            n = 0;
        cost++;
    }
    return cost;
}

int main()
{
    ios::sync_with_stdio(false);
    //freopen("test.txt", "r", stdin);
    int d;
    while(cin >> n1 >> n2 >> d)
    {
        if(n1 == 0 && n2 == 0 && d == 0)
            break;
        for(int i = 0; i < 100005; i++)
            graph[i].clear();
        memset(indegree, 0, sizeof(indegree));
        int x, y;
        for(int i = 0; i < d; i++)
        {
            cin >> x >> y;
            graph[y].push_back(x);
            indegree[x]++;
        }
        cout << min(topSort(0), topSort(1)) << endl;
    }
    return 0;
}



内容概要:本文详细介绍了“秒杀商城”微服务架构的设计与实战全过程,涵盖系统从需求分析、服务拆分、技术选型到核心功能开发、分布式事务处理、容器化部署及监控链路追踪的完整流程。重点解决了高并发场景下的超卖问题,采用Redis预减库存、消息队列削峰、数据库乐观锁等手段保障数据一致性,并通过Nacos实现服务注册发现与配置管理,利用Seata处理跨服务分布式事务,结合RabbitMQ实现异步下单,提升系统吞吐能力。同时,项目支持Docker Compose快速部署和Kubernetes生产级编排,集成Sleuth+Zipkin链路追踪与Prometheus+Grafana监控体系,构建可观测性强的微服务系统。; 适合人群:具备Java基础和Spring Boot开发经验,熟悉微服务基本概念的中高级研发人员,尤其是希望深入理解高并发系统设计、分布式事务、服务治理等核心技术的开发者;适合工作2-5年、有志于转型微服务或提升架构能力的工程师; 使用场景及目标:①学习如何基于Spring Cloud Alibaba构建完整的微服务项目;②掌握秒杀场景下高并发、超卖控制、异步化、削峰填谷等关键技术方案;③实践分布式事务(Seata)、服务熔断降级、链路追踪、统一配置中心等企业级中间件的应用;④完成从本地开发到容器化部署的全流程落地; 阅读建议:建议按照文档提供的七个阶段循序渐进地动手实践,重点关注秒杀流程设计、服务间通信机制、分布式事务实现和系统性能优化部分,结合代码调试与监控工具深入理解各组件协作原理,真正掌握高并发微服务系统的构建能力。
### Hanoi塔递归算法实现过程解释 #### 1. 基本概念 Hanoi塔问题是经典的递归问题之一,其目标是从起始柱子(通常称为A)通过辅助柱子(B),将所有的圆盘按照相同的顺序移动到目标柱子(C)。每次只能移动一个圆盘,并且任何时候都不能将较大的圆盘放在较小的圆盘之上。 该问题的核心在于利用递归来分解复杂的问题为更小的子问题。具体来说,要将`n`个圆盘从一根柱子移到另一根柱子,可以分为三个主要部分: - 将前`n-1`个圆盘从起始柱子借助辅助柱子移动到中间柱子; - 将第`n`个圆盘直接从起始柱子移动到目标柱子; - 再次将前`n-1`个圆盘从中间柱子借助起始柱子移动到目标柱子[^1]。 #### 2. 时间复杂度分析 由于每一步都需要处理两个子问题以及一次额外的操作,因此整个算法的时间复杂度为 \( O(2^n) \)。这意味着当圆盘数较多时,计算量会迅速增大。 #### 3. C/C++中的代码实现 以下是基于上述逻辑的一个标准C/C++版本的Hanoi塔递归解决方案: ```cpp #include <stdio.h> // 函数用于模拟单步移动操作 void move(char fromPeg, char toPeg) { printf("%c -> %c\n", fromPeg, toPeg); } // 主递归函数定义 void hanoi(int n, char startPeg, char endPeg, char auxPeg) { if (n == 1) { // 当只剩下一个盘子时,直接完成移动 move(startPeg, endPeg); } else { // 步骤一:先将上面n-1个盘子从startPeg移动到auxPeg hanoi(n - 1, startPeg, auxPeg, endPeg); // 步骤二:再单独把最大的那个盘子从startPeg移动到endPeg move(startPeg, endPeg); // 步骤三:最后将之前留在auxPeg上的n-1个盘子移动到endPeg hanoi(n - 1, auxPeg, endPeg, startPeg); } } int main() { int numberOfDiscs; printf("Enter the number of discs: "); scanf("%d", &numberOfDiscs); // 调用hanoi函数解决问题 hanoi(numberOfDiscs, 'A', 'C', 'B'); return 0; } ``` 这段程序首先询问用户输入多少个圆盘,接着调用 `hanoi()` 来执行具体的递归运算并打印出每一个必要的动作序列[^2]。 #### 4. Java中的代码实现 同样地,在Java中也可以采用类似的思路来编写汉诺塔的递归解法如下所示: ```java public class HanoiTower { public static void moveDisk(char sourceRod, char destinationRod){ System.out.println(sourceRod+"->"+destinationRod); } public static void solveHanoiProblem(int numDisks,char startRod ,char targetRod ,char auxiliaryRod ){ if(numDisks==1){ moveDisk(startRod,targetRod ); }else{ // Move top N-1 disks from A to B using C as temporary storage. solveHanoiProblem(numDisks-1,startRod,auxiliaryRod,targetRod ); // Now perform single disk movement between rods. moveDisk(startRod,targetRod ); // Finally shift remaining N-1 Disks From B To Target Rod Using Source As Temporary Storage Again. solveHanoiProblem(numDisks-1,auxiliaryRod,targetRod,startRod ); } } public static void main(String[] args)throws Exception{ java.util.Scanner sc=new java.util.Scanner(System.in); System.out.print("Number Of Discs :"); int no=sc.nextInt(); solveHanoiProblem(no,'A','C','B'); } } ``` 此段JAVA代码实现了与前面提到过的C ++版本几乎完全一致的功能和行为模式[^4]. #### 总结说明 无论是哪种编程语言所书写的汉诺塔递归方案都遵循着同一个基本原理——分治策略下的自我重复调用机制;同时需要注意的是尽管这种方法直观简洁易于理解掌握,但由于它本质上属于指数型增长范畴内的穷举类方法论所以并不适合应用于大规模数据场景下求解此类难题[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值