今日练习

今日练习

UVA10881,参考题解后耗时3小时A,我是废物。 : (

//
// Created by 21122 on 2022/5/13/0013.
//
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int order_arr[100005];

static bool cmp(const vector<int>& a,const vector<int>& b){

    return a.back()<b.back();
}


int main()
{
    int data_nums;
    std::cin >> data_nums;
    int case1 = 1;
    for(int i = 0; i < data_nums; i++)        //多组输入
    {

        int L, T, n;
        std::cin >> L >> T >> n;
        std::vector<std::vector<int>> v;
        std::vector<std::vector<int>> v2;
        int index = 0;
        for(int i = 0; i < n; i++)
        {   //I/O handling
            int x;
            char letter;
            int direction;          //-1 for the left, 1 for the right
            std::cin >> x >> letter;    //letter = "L" or "R"
            if('L' == letter)
            {
                direction =  -1;
            }
            else
            {
                direction = 1;
            }
            //index 从1开始
            v.push_back(std::vector<int>{x, direction, index ++});        //<position, direction, index> <<1, 右, 1>, <5, 右, 2>, ......>
            v2.push_back(std::vector<int>{x + T * direction, direction});
        }

        std::sort(v.begin(), v.end());

        for(int i = 0; i < n; i++)
        {
            order_arr[v[i][2]] = i;
        }

//    for(int i = 0; i < v2.size(); i++)
//    {
//        std::cout <<"index:" << i << "    " << v2[i][0] << "  " <<v2[i][1] << std::endl;
//    }

        std::sort(v2.begin(), v2.end());

        for(int i = 0; i < n - 1; i++)
        {
            if(v2[i][0] == v2[i + 1][0])
            {
                v2[i][1] =  0;   // 方向为0, 相撞拉!
                v2[i + 1][1] = 0;
            }
        }

//    for(int i = 0; i < v2.size(); i++)
//    {
//        std::cout <<"index:" << i << "    " << v2[i][0] << "  " <<v2[i][1] << std::endl;
//    }


        cout << "Case #" <<case1 << ":" <<endl;
        case1 ++;

        for(int i = 0; i < n; i++)
        {
            int index_2 = order_arr[i];
            //超出边界
            if(v2[index_2][0] < 0 || v2[index_2][0] > L)
            {
                std::cout << "Fell off" << std::endl;
            }
            else
            {
                std::cout << v2[index_2][0];
                if(v2[index_2][1] == 0)    //相撞
                {
                    std::cout <<" Turning" << std::endl;
                }
                else if(v2[index_2][1] == -1)  //左转
                {
                    std::cout << " L" << std::endl;
                }
                else
                {
                    std::cout << " R" << std::endl;
                }


            }
        }
        cout << endl;
    }


    return 0;
}

总结

(明日补