codeforces1066C

这是一个关于火车站调度问题的算法解析,主要涉及如何通过贪心策略确定火车驶出的最优顺序。给定一系列火车进入和离开火车站的操作,以及询问特定火车离开所需的最小提前驶离的火车数量,算法通过对每次操作进行处理并维护最优解来得出答案。

codeforces1066C

题目链接
在一个神奇的地方有一座神奇的火车站,它只有一条铁轨,可是它却会有从两个方向到来的火车…
现在给你t个操作或询问,操作或询问可能的情况有以下几种:
1.? num ——询问至少需要从火车站驶出多少列火车才能让编号num的火车顺利离开火车站
2.L num——有一列编号为num的火车从左侧驶入火车站,并停留在火车站中
3.R num——有一列编号为num的火车从右侧驶入火车站,并停留在火车站中
现在需要你针对每一次第一种询问输出结果。
Input
第一行包含了一个数字t(1 < = t < = 2e5),代表操作或询问的次数。
接下来t行,每一行包含了一个代表操作类型的符号和一个整数代表列车的编号。
列车的编号num满足1 < = num < =2e5,并且没有列车的编号是相同的
Output
针对每一次第一种询问,每次在独立的一行中输出使编号num的火车顺利离开火车站,需要提前驶离的最少列车数量。(注意这只是询问,并不是说这些列车真的驶离了火车站)
Examples
Input
8
L 6
R 2
R 3
? 3
L 4
? 6
L 5
? 6
Output
0
1
2
Input
10
L 1000
R 100
R 12
L 11
? 11
L 10
R 15
? 100
R 110
? 15
Output
0
2
1
Note
针对第一组样例
1.火车站情况为[6]
2.火车站情况为[6,2]
3.火车站情况为[6,2,3]
4.列车3可以直接出站不需要其他列车提前出站,因此答案是0
5.火车站情况为[4,6,2,3]
6.列车3出站之前应该让列车4先出站,答案是1
7.火车站情况为[5,4,6,2,3]
9.列车6出站之前应该让列车5,4或者列车3,2先出站,因此答案是2

针对第二组样例
1.火车站情况为[1000]
2.火车站情况为[1000,100]
3.火车站情况为[1000,100,12]
4.火车站情况为[11,1000,100,12]
5.列车11出站前不需要其他列车先出站,答案是0
6.火车站情况为[10,11,1000,100,12]
7.火车站情况为[10,11,1000,100,12,15]
8.列车100出站前需要列车12,15先出站,答案是2
9.火车站情况为[10,11,1000,100,12,15,110]
10.列车15出站前需要列车110先出站,答案是1

思路

贪心,用最小的代价将火车开出,故对一辆火车,必定是在“?”询问前的最后一次开入的才是最优位置。因此我们只需要用数组对每个数记录一下最优解,最后分别取min(最优解位置-最左端位置,最右端-最优位置)即可

#include<bits/stdc++.h>
using namespace std;
const int maxn=400010;
int a[maxn];
char str[2];
int main()
{
    int t;
    scanf("%d",&t);
    int l=200000;
    int r=l-1;
    while(t--)
    {
        int num;
        scanf("%s",str);
        scanf("%d",&num);
        if(str[0]=='L')
        {
            l--;
            a[num]=l;
        }
        else if(str[0]=='R')
        {
            r++;
            a[num]=r;
        }
        else
        {

            int res=min(a[num]-l,r-a[num]);
            cout<<res<<endl;
        }
    }
    return 0;
}

### Codeforces Problem 1332C Explanation The provided references pertain specifically to problem 742B on Codeforces rather than problem 1332C. For an accurate understanding and solution approach for problem 1332C, it's essential to refer directly to its description and constraints. However, based on general knowledge regarding competitive programming problems found on platforms like Codeforces: Problem 1332C typically involves algorithmic challenges that require efficient data structures or algorithms such as dynamic programming, graph theory, greedy algorithms, etc., depending upon the specific nature of the task described within this particular question[^6]. To provide a detailed explanation or demonstration concerning **Codeforces problem 1332C**, one would need direct access to the exact statement associated with this challenge since different tasks demand tailored strategies addressing their unique requirements. For obtaining precise details about problem 1332C including any sample inputs/outputs along with explanations or solutions, visiting the official Codeforces website and navigating to contest number 1332 followed by examining section C is recommended. ```python # Example pseudo-code structure often seen in solving competitive coding questions. def solve_problem_1332C(input_data): # Placeholder function body; actual logic depends heavily on the specifics of problem 1332C. processed_result = process_input(input_data) final_answer = compute_solution(processed_result) return final_answer input_example = "Example Input" print(solve_problem_1332C(input_example)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值