北京大学PKU在线测评系统 3078题 Q

原题:

Q

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1188 Accepted: 838

Description

You've got a queue. And you just got to mess with it.
Given a queue of items and a series of queue operations, return the resulting queue.
Queue operations are defined as follows:

starting-position to requested-position

meaning one wants the item at the starting position to be moved to the requested position. So if the queue of items were:

Item1 Item2 Item3 Item4 Item5

(Item1 being in position 1, Item2 in position 2, etc.)
after applying the queue operation:

5 to 2

the resulting queue would be:

Item1 Item5 Item2 Item3 Item4

as Item5 (the item in position 5) was moved to position 2. Multiple queue operations are applied at the same time, however; e.g., given the queue of items:

Item1 Item2 Item3 Item4 Item5 Item6 Item7 Item8

If the following queue operations were applied:

2 to 6; 6 to 3; 4 to 5; 5 to 2; 7 to 4; 8 to 1

then the resulting queue would be:

Item8 Item5 Item6 Item7 Item4 Item2 Item1 Item3

As you can see, the queue operations are strictly enforced, with other items (not involved in queue operations) maintaining their order and moving to vacant positions in the queue. Note that no two queue operations will have the same starting-position or same requested-position defined.
 

Input

Input to this problem will begin with a line containing a single integer x indicating the number of datasets. Each data set consists of three components:

  1. Start line – A single line, "m n" (1 <= m, n <= 20) where m indicates the number of items in the queue and n indicates the number of queue operations.
  2. Queue items – A line of short (between 1 and 8 characters) alphanumeric names for the items in the queue. Names are unique for a given data set and contain no whitespace.
  3. Queue operations – n lines of queue operations in the format "starting-position requested-position".

 

Output

For each dataset, output the queue after the queue operations have been applied. Print the elements of the queue on a single line, starting from the first and ending with the last, with a single space separating each item.

Sample Input

3
5 1
alpha beta gamma delta epsilon
5 2
8 6
a b c d e f g h
2 6
6 3
4 5
5 2
7 4
8 1
3 2
foo bar baz
3 1
1 3

Sample Output

alpha epsilon beta gamma delta
h e f g d b a c
baz bar foo
解决方案:
/* 3078.cpp */

#include <iostream>
#include <string>
using namespace std;

int main()
{
    // 队列元素
    typedef struct {
        char names[8];
        int requested;
        bool isOrder;
    }Queue;

    unsigned int items;      // 队列元素数
    unsigned int operations; // 队列操作数
    unsigned int datasets;   // 队列集合数
    unsigned int start;      // 开始位置
    unsigned int requested;  // 要求位置

    int i;
    int j;
    Queue* queue;

    cin >> datasets;

    string answer = "";  // 结果字符串
    string tmp;          // 临时字符串
    while (datasets > 0) {
        cin >> items >> operations;

        // 动态生成队列
        queue = new Queue[items];
        for (i = 0; i < items; i++) {
            queue[i].requested = -1;
            queue[i].isOrder = true;
            cin >> queue[i].names;
        }

        // 进行队列操作
        while (operations > 0) {
            cin >> start >> requested;
            start--;
            requested--;
            queue[requested].requested = start;
            queue[start].isOrder = false;  // 标记被移动的位置
            operations--;
        }

        i = 0;
        j = 0;
        // 保存当前的结果队列
        for (i = 0; i < items; i++) {
            // 当前位置并没被要求
            if (queue[i].requested == -1) {
                // 寻找一个没被移动的位置并将其名字记录在tmp中
                while (queue[j].isOrder == false) {
                    j++;
                }
                tmp = queue[j].names;
                j++;
            }
            // 当前位置被要求,则记录要求这位置的元素的名字
            else {
                tmp = queue[queue[i].requested].names;
            }
            // 每两个元素间要加上空格
            answer += tmp;
            answer += " ";
        }
        // 每两个队列间加上回车
        answer += "/n";
        // 删除当前队列
        delete[] queue;

        datasets--;
    }
    // 输出所有结果队列
    cout << answer.c_str();
    return 0;
}
方法很简单,没有对算法进行时间和空间的优化。只是凭自己的一时想法就写出来的。
反正就是通过测试了。
有需要的可以参考下上面的源码。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
北京大学数字普惠金融指数(pku-dfiic)2022年将继续关注数字普惠金融领域的发展状况,重点关注金融科技在服务普惠金融方面的应用和创新。该指数将以数据和研究为基础,深入分析包括金融科技技术应用、金融机构服务能力、监管环境和用户体验等方面的发展情况,评估数字普惠金融领域的整体发展水平和各项指标的综合表现。 在2022年,北京大学数字普惠金融指数将更加关注数字支付、区块链、大数据分析以及人工智能等新兴技术在普惠金融领域的应用情况,评估这些技术对提升金融普惠性的作用。同时,该指数还将关注金融科技创新对金融服务效率和用户体验的影响,考量金融科技在促进金融包容和降低金融服务成本方面的贡献。 除此之外,北京大学数字普惠金融指数还将关注金融监管政策对数字普惠金融发展的影响,评估不同国家和地区的监管环境对数字普惠金融创新和应用的支持程度。同时,该指数还将分析金融机构在数字普惠金融领域的服务能力和创新动态,评估不同机构在技术应用、产品设计和市场拓展方面的表现。 总的来说,北京大学数字普惠金融指数(pku-dfiic)2022年将持续关注数字普惠金融领域的发展趋势和关键问,为政府、金融机构和科研机构提供全面准确的数据支持和分析参考,推动数字普惠金融的健康发展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值