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;
}
- SQL Server 性能优化之——系统化方法提高性能
- C# 调用PowerShell方法
- 使用Visual Studio 2010 一步一步创建Powershell Module 和 Cmdlet
- [SQLServer大对象]——FileTable初体验
- JavaScript闭包,只学这篇就会了
- [数据库基础]——编码标准之格式
- [数据库基础]——编码标准之结构
- 2017前端开发手册三-前端职位描述
- [.NET自我学习]Delegate 泛型
- [数据库基础]——图解JOIN
- ubuntu搭建内网穿透服务Ngrok
- SQL Server 性能优化之——T-SQL 临时表、表变量、UNION
- node.js 学习笔记
- SQL Server 性能优化之——T-SQL TVF和标量函数
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 半天掌握TypeScript,感觉就像写Java
- PyTorch中神经网络的对抗性攻击和防御
- 了解 Swift 的 Result 类型
- Java自动化测试(特殊元素处理 26)
- Java自动化测试(web自动化测试框架 28)
- 你想要拥有自己的搜索引擎吗?
- JAVA三年面试总结,金九银十,你准备好了吗?
- Best Cow Line(POJ 3617)
- Flask单点登录竟然只要几行代码就能搞定!
- 区间调度问题
- 迷宫的最短路径
- Lake Counting (POJ No.2386)
- 部分和问题(DFS)
- 为什么 React Hooks useState 更新不符预期?
- 技术分享 | MySQL 使用 MariaDB 审计插件