专栏首页glm的全栈学习之路Codeforces Round #615 (Div. 3)B. Collecting Packages

Codeforces Round #615 (Div. 3)B. Collecting Packages

B. Collecting Packages

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There is a robot in a warehouse and nn packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point (0,0)(0,0). The ii-th package is at the point (xi,yi)(xi,yi). It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point (0,0)(0,0) doesn't contain a package.

The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point (x,y)(x,y) to the point (x+1,yx+1,y) or to the point (x,y+1)(x,y+1).

As we say above, the robot wants to collect all nn packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.

The string ss of length nn is lexicographically less than the string tt of length nn if there is some index 1≤j≤n1≤j≤n that for all ii from 11 to j−1j−1 si=tisi=ti and sj<tjsj<tj. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way.

Input

The first line of the input contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases. Then test cases follow.

The first line of a test case contains one integer nn (1≤n≤10001≤n≤1000) — the number of packages.

The next nn lines contain descriptions of packages. The ii-th package is given as two integers xixi and yiyi (0≤xi,yi≤10000≤xi,yi≤1000) — the xx-coordinate of the package and the yy-coordinate of the package.

It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point (0,0)(0,0) doesn't contain a package.

The sum of all values nn over test cases in the test doesn't exceed 10001000.

Output

Print the answer for each test case.

If it is impossible to collect all nn packages in some order starting from (0,00,0), print "NO" on the first line.

Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path.

Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable.

Example

input

Copy

3
5
1 3
1 2
3 3
5 5
4 3
2
1 0
0 1
1
4 3

output

Copy

YES
RUUURRRRUU
NO
YES
RRRRUUU

Note

For the first test case in the example the optimal path RUUURRRRUU is shown below:

B. Collecting Packages

题目链接:https://codeforces.com/contest/1294/problem/B

题意:

有n个盒子需要你捡,第i个盒子的坐标为 (Xi , Yi)。你从(0,0)出发,每次只能选择向上或者向右移动,问能否将n个盒子都捡完,若可以捡完,则输出字典序最小的一条路线

首先理解字典序最小,往右走是R,往上走是U,R的字典序比U前面,所以如果可以往右走和往上走要先往右走再往上走

这样我们就很自然的对每一个坐标对按x从小到大排序,然后如果上一个盒子的y坐标比下一个大,那么gg直接输出no,

所以得先预检查一遍,如果没有这种情况的存在就ok,然后每次先往右走i+1个盒子的横坐标减i个盒子的横坐标,然后往上走i+1个盒子的纵坐标减i个盒子的纵坐标

#include<bits/stdc++.h>
#define ll long long
#define rg register ll
using namespace std;
ll n,t;
struct node
{
    ll x,y;
    bool operator <(const node& a)const{
        if(x==a.x)return y<a.y;
        return x<a.x;
    }
}p[1005];
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(rg i=1;i<=n;i++)
        {
            cin>>p[i].x>>p[i].y;
        }
        sort(p+1,p+1+n);
        ll tepx=0,tepy=0,flag=0;
        for(rg i=1;i<=n;i++)
        {
            if(p[i].y<p[i-1].y)
            {
                flag=1;
                break;
            }   
        }
        if(flag)cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            for(rg i=1;i<=n;i++)
            {
                for(rg j=1;j<=p[i].x-tepx;j++)cout<<"R";
                tepx=p[i].x;
                for(rg j=1;j<=p[i].y-tepy;j++)cout<<"U";
                tepy=p[i].y;
            }
            cout<<endl;
        }
        
    }

    while(1)getchar();
    return 0;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 2019.8.15乘兴打Codeforces Round #569 (Div. 2)小记

    Recently, on the course of algorithms and data structures, Valeriy learned how t...

    glm233
  • Codeforces Round #624 (Div. 3) C - Perform the Combo

    You want to perform the combo on your opponent in one popular fighting game. The...

    glm233
  • PAT (Advanced Level) Practice 1099 Build A Binary Search Tree (30 分)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the...

    glm233
  • 从更统一的视角分析VAE

    With an overall view of VAE, we can extend standard VAE loss in the reconstructi...

    用户1908973
  • 为什么序号要从0开始

    《Why numbering should start at zero》,原文链接: http://www.cs.utexas.edu/users/EWD/t...

    SeanCheney
  • 【CS224N笔记】一文详解神经网络来龙去脉

    我们输入一个向量并且通过一系列的逻辑回归函数,我们可以得到一个输出向量,但是我们不需要提前决定,逻辑回归试图预测的向量是什么

    zenRRan
  • Computational Geometry in Python

    This post is a simplified version of the accompanying notebook to chapter 6 of m...

    周星星9527
  • Palabos Tutorial 1/3: First steps with Palabos

    The following tutorial provides an overview of the C++ programmer interface of t...

    周星星9527
  • RL09 Bayesian Network

    • Bayesian Networks are directedacyclic graphs (DAGs) withan associated set of ...

    安包
  • 如何快速定位SAP CRM订单应用(Order Application)错误消息抛出的准确位置

    In my blog Six kinds of debugging tips Fabian Geyer raised a very good point abo...

    Jerry Wang

扫码关注云+社区

领取腾讯云代金券