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

题面

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). The ii-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 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 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.

Input

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 nn lines contain descriptions of packages. The ii-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 nn over test cases in the test doesn't exceed 1000.

Output

Print the answer for each test case.

If it is impossible to collect all nn 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.

大意:起点在(0,0),给你n个点的坐标,只能向上或向右走,判断能否走完这些点.可以的话找到最短路径(比如"RU"<"UR")

思路:用结构体数组存下这些点,排序,如果某个点比它前一个点的x或y坐标小的话gg.否者记录下多少个'R'或'U.

#include<iostream>
#include<string>
#include<string.h>
#define ll long long
#include<algorithm>
using namespace std;
int t,a,b,c,n;
   
struct V{
	int x,y;
}v[1010];
bool comp(V a,V b){
	if(a.x==b.x) return a.y<b.y;
	return a.x<b.x;
}
string s;
int solve()
{
	s="";
	for(int i=1;i<=n;i++)
	{
		int a=v[i].x-v[i-1].x;
		int b=v[i].y-v[i-1].y;
		
		if(a<0||b<0) return 0;
		else {
			while(a--) s+='R';
			while(b--) s+='U';
			//cout<<s<<endl;
		}
	}
	return 1;
}
int main()
{
    cin>>t;
    while(t--)
    {
    	v[0].x=0;v[0].y=0;
    	cin>>n;
    	for(int i=1;i<=n;i++)
    	cin>>a>>b,v[i].x=a,v[i].y=b;
		sort(v+1,v+1+n,comp);
    	if(!solve()) cout<<"NO"<<endl;
    	else cout<<"YES"<<endl<<s<<endl;
    }
    return 0;
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值