1026. Table Tennis (30)【排序+模拟+逻辑复杂】——PAT (Advanced Level) Practise

本文深入探讨了游戏开发领域的关键技术,包括游戏引擎、编程语言、硬件优化等,并结合人工智能与音视频处理的最新进展,展示如何将这些技术应用于游戏开发中,以提升用户体验与创新性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目信息

1026. Table Tennis (30)

时间限制400 ms
内存限制65536 kB
代码长度限制16000 B

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the tables are occupied, they will have to wait in a queue. It is assumed that every pair of players can play for at most 2 hours.

Your job is to count for everyone in queue their waiting time, and for each table the number of players it has served for the day.

One thing that makes this procedure a bit complicated is that the club reserves some tables for their VIP members. When a VIP table is open, the first VIP pair in the queue will have the priviledge to take it. However, if there is no VIP in the queue, the next pair of players can take it. On the other hand, if when it is the turn of a VIP pair, yet no VIP table is available, they can be assigned as any ordinary players.

Input Specification:

Each input file contains one test case. For each case, the first line contains an integer N (<=10000) - the total number of pairs of players. Then N lines follow, each contains 2 times and a VIP tag: HH:MM:SS - the arriving time, P - the playing time in minutes of a pair of players, and tag - which is 1 if they hold a VIP card, or 0 if not. It is guaranteed that the arriving time is between 08:00:00 and 21:00:00 while the club is open. It is assumed that no two customers arrives at the same time. Following the players’ info, there are 2 positive integers: K (<=100) - the number of tables, and M (< K) - the number of VIP tables. The last line contains M table numbers.

Output Specification:

For each test case, first print the arriving time, serving time and the waiting time for each pair of players in the format shown by the sample. Then print in a line the number of players served by each table. Notice that the output must be listed in chronological order of the serving time. The waiting time must be rounded up to an integer minute(s). If one cannot get a table before the closing time, their information must NOT be printed.

Sample Input:
9
20:52:00 10 0
08:00:00 20 0
08:02:00 30 0
20:51:00 10 0
08:10:00 5 0
08:12:00 10 1
20:50:00 10 0
08:01:30 15 1
20:53:00 10 1
3 1
2
Sample Output:
08:00:00 08:00:00 0
08:01:30 08:01:30 0
08:02:00 08:02:00 0
08:12:00 08:16:30 5
08:10:00 08:20:00 10
20:50:00 20:50:00 0
20:51:00 20:51:00 0
20:52:00 20:52:00 0
3 3 2

解题思路

维护用户队列、空闲桌子队列和桌子到期队列。
注意:超过两小时需截断,等待时间需四舍五入,8点前需等待,21点关门

AC代码

#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <functional>
using namespace std;
bool table[10005], isvip[10005];
int tableTimes[10005];
struct node{
    int arriveTime, useTime;
    bool vip;
    node(){}
    node(int t, int tu, bool v):arriveTime(t), useTime(tu), vip(v){}
    bool operator<(const node& b)const{
        return arriveTime < b.arriveTime;
    }
};
bool operator<(const pair<int,int> &a, const pair<int,int> &b){
    return a.first < b.first;
}
set<node> arriveList; //顾客到达列表
priority_queue<pair<int, int>, vector<pair<int,int> >, greater<pair<int,int> > > que; //乒乓球台到期队列
set<int> freeList; //空闲台队列

int main()
{
    int n, tableNum, tableVipNum, tableVipNow,a, b, c, t, vip;
    int nowTime = 8*3600;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i){
        scanf("%d:%d:%d%d%d", &a, &b, &c, &t, &vip);
        arriveList.insert(node(a*3600 + b*60 + c, t, vip == 1));
    }
    scanf("%d%d", &tableNum, &tableVipNum);
    for (int i = 0; i < tableNum; ++i){
        freeList.insert(i);//初始化空闲台
    }
    tableVipNow = tableVipNum;
    for (int i = 0; i < tableVipNum; ++i){
        scanf("%d", &vip);
        isvip[vip-1] = true;
    }
    while (!arriveList.empty() && nowTime < 21*3600){
        t = arriveList.begin()->arriveTime;
        if (!freeList.empty() && t <= nowTime){
            if (tableVipNow > 0){
                set<node>::iterator it = arriveList.begin();
                while (it != arriveList.end() && nowTime >= it->arriveTime){
                    if (it->vip){
                        break;
                    }
                    ++it;
                }
                if (it != arriveList.end() && nowTime >= it->arriveTime){
                    --tableVipNow;
                    set<int>::iterator itable = freeList.begin();
                    while (!isvip[*itable]){
                        ++itable;
                    }
                    t = it->arriveTime;
                    printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", t/3600, t%3600/60, t%60, nowTime/3600, nowTime%3600/60, nowTime%60, (nowTime - t + 30)/60); //四舍五入
                    tableTimes[*itable]++;
                    que.push(make_pair(nowTime + min(120, it->useTime) * 60, *itable)); //超过两小时截断
                    freeList.erase(*itable);
                    arriveList.erase(*it);
                    continue;
                }
            }
            printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", t/3600, t%3600/60, t%60, nowTime/3600, nowTime%3600/60, nowTime%60, (nowTime - t + 30)/60);
            tableTimes[*freeList.begin()]++;
            que.push(make_pair(nowTime + min(120, arriveList.begin()->useTime) * 60, *freeList.begin())); //截断
            if (isvip[*freeList.begin()]){
                --tableVipNow;
            }
            freeList.erase(*freeList.begin());
            arriveList.erase(*arriveList.begin());
            continue;
        }else {
            if (que.empty() || (!freeList.empty() && que.top().first > t)){
                nowTime = t;
            }else{
                if (nowTime < que.top().first){
                    nowTime = que.top().first;
                }
                while (!que.empty() && nowTime >= que.top().first){
                    freeList.insert(que.top().second);
                    if (isvip[que.top().second]) {
                        ++tableVipNow;
                    }
                    que.pop();
                }
            }
        }
    }
    printf("%d", tableTimes[0]);
    for (int i = 1; i < tableNum; ++i){
        printf(" %d", tableTimes[i]);
    }
    return 0;
}
<think>好的,用户遇到了Spring Boot项目中UserMapper Bean未找到的问题。我需要先分析可能的原因,然后给出解决方案。首先,UserMapper通常是一个MyBatis的Mapper接口,Bean未找到可能是因为没有被正确扫描到。 首先考虑组件扫描的问题。Spring Boot需要在主应用类上使用@MapperScan注解来指定Mapper接口的包路径,或者每个Mapper接口添加@Mapper注解。如果用户没有正确配置这些,会导致Bean无法被创建。 接下来,检查MyBatis的依赖是否正确。项目应该包含mybatis-spring-boot-starter,否则MyBatis相关的自动配置不会生效,导致Mapper无法被识别。 然后,确认UserMapper是否在Spring的组件扫描路径下。如果应用主类所在的包与Mapper接口不在同一包或子包下,又没有使用@MapperScan指定路径,Spring可能无法发现这些接口。 还要查看配置文件,比如application.properties,是否配置了mybatis.mapper-locations,指向XML文件的位置。如果XML文件存放位置不符合默认配置,这里需要显式指定。 另外,查看是否有多个数据源配置导致冲突。如果项目中配置了多个DataSource,可能需要使用@Primary注解指定主数据源,或者在配置类中明确指定SqlSessionFactoryBean和MapperScannerConfigurer。 最后,检查IDE的编译输出目录,确保Mapper接口的.class文件已经正确生成。有时候构建工具的问题可能导致类文件未生成,需要清理并重新编译项目。 综合这些可能的原因,应该逐步引导用户检查配置、依赖和路径设置,确保所有步骤都正确无误。同时,引用相关的文档和之前的文章,帮助用户更深入地理解每个配置项的作用。</think>在Spring Boot项目中遇到`UserMapper` Bean未找到的问题,通常由组件扫描、依赖配置或注解缺失引起。以下是逐步排查和解决方案: --- ### **1. 检查Mapper接口的注解与扫描配置** - **确保Mapper接口添加了`@Mapper`注解** 每个MyBatis Mapper接口需标注`@Mapper`,否则Spring无法识别: ```java import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper { // SQL方法定义 } ``` - **或全局配置扫描路径** 在主应用类添加`@MapperScan`指定Mapper包路径[^3]: ```java @SpringBootApplication @MapperScan("com.example.mapper") // 替换为实际包名 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` --- ### **2. 验证MyBatis依赖是否正确** - **在`pom.xml`中添加MyBatis Starter依赖** 确保包含以下依赖以启用自动配置: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.1</version> </dependency> ``` 如果未正确引入,Spring Boot不会自动配置Mapper扫描[^1]。 --- ### **3. 检查XML映射文件路径配置** - **在`application.properties`中指定XML位置** 如果使用XML定义SQL映射,需显式配置路径: ```properties mybatis.mapper-locations=classpath:mapper/*.xml ``` 确保XML文件位于`src/main/resources/mapper/`目录下。 --- ### **4. 排除多数据源配置冲突** - **若配置了多个数据源** 需在`@Configuration`类中手动指定`SqlSessionFactory`和`MapperScannerConfigurer`,避免自动配置失效: ```java @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); return factoryBean.getObject(); } ``` --- ### **5. 验证组件扫描范围** - **确保Mapper接口在Spring Boot主类同级或子包下** 默认扫描范围是主类所在包及其子包。若Mapper接口在其他路径,需通过`@ComponentScan`扩展扫描范围[^2]。 --- ### **6. 清理并重新构建项目** - **执行Maven/Gradle清理命令** 有时IDE缓存会导致编译不更新: ```bash mvn clean install ``` 或使用IDE的`Rebuild Project`功能。 --- ### **示例项目结构** ``` src/ ├── main/ │ ├── java/ │ │ └── com.example/ │ │ ├── Application.java # 主类 │ │ └── mapper/ │ │ └── UserMapper.java # Mapper接口 │ └── resources/ │ └── mapper/ │ └── UserMapper.xml # XML映射文件 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值