Collecting Packages(CF-1294B)

Problem Description

There is a robot in a warehouse and n packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point (0,0). The i-th package is at the point (xi,yi). It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point (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) to the point (x+1,y) or to the point (x,y+1).

As we say above, the robot wants to collect all n 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 s of length n is lexicographically less than the string t of length n if there is some index 1≤j≤n that for all i from 1 to j−1 si=ti and sj<tj. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way.


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

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

The next n lines contain descriptions of packages. The i-th package is given as two integers xi and yi (0≤xi,yi≤1000) — the x-coordinate of the package and the y-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) doesn't contain a package.

The sum of all values n over test cases in the test doesn't exceed 1000.


Print the answer for each test case.

If it is impossible to collect all n packages in some order starting from (0,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.



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




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

题意:有 n 个包裹,给出这 n 个包裹的坐标,一个人从 (0,0) 点出发,只能向右或向上行走,问是否能收集到所有的包裹,若能给出字典序最小的路径



由于要求字典序最小,因此将所有的包裹优先按横坐标进行排序,再计算相邻两包裹的横纵坐标的差,如果小于 0 就说明存在无法收集包裹的情况,如果没有小于 0 的情况,那么就根据横纵坐标差转换成 R  和 U 即可

Source Program

#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-6;
const int MOD = 1000000000+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,1,-1,1,1};
const int dy[] = {1,-1,0,0,-1,1,-1,1};
using namespace std;

struct Node {
    int x, y;
    bool operator<(const Node &rhs) const {
        if (x == rhs.x)
            return y < rhs.y;
        return x < rhs.x;
} node[N];
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int n;
        scanf("%d", &n);
        node[0].x = 0, node[0].y = 0;
        for (int i = 1; i <= n; i++)
            scanf("%d%d", &node[i].x, &node[i].y);
        sort(node + 1, node + 1 + n);

        bool flag = true;
        string res = "";
        for (int i = 1; i <= n; i++) {
            int subX = node[i].x - node[i - 1].x;
            if (subX < 0) {
                flag = false;
            } else {
                while (subX--)
                    res += "R";
                int subY = node[i].y - node[i - 1].y;
                if (subY < 0) {
                    flag = false;
                } else {
                    while (subY--)
                        res += "U";
        if (flag) {
            cout << res << endl;
        } else
    return 0;


发布了1890 篇原创文章 · 获赞 777 · 访问量 204万+


©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客