题目

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.
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 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.
Output
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.

Input
3
5
1 3
1 2
3 3
5 5
4 3
2
1 0
0 1
1
4 3
Output
YES
RUUURRRRUU
NO
YES
RRRRUUU

题意与思路

机器人捡包,只能往右,往上走,问是否能够捡到所有包。
这里不能捡到的情况是:x大,y却小,即位于右下方的包。

按坐标排序,然后作差即为需要移动的步数


这里使用三个参数的sort(),学习一下cmp的用法。

//cmp:升序使用小于号,降序使用大于号

并且点对pair<int ,int >的知识复习一下。

AC代码

#include<bits/stdc++.h>
using namespace std;//排序规则:横坐标先排,纵坐标后排
bool cmp(pair <int,int>a,pair<int,int>b)
{if(a.first!=b.first)return a.first<b.first;return a.second<b.second;//先排横坐标,后排纵坐标
}
int main()
{int T;vector<pair<int,int> >v;cin>>T;while(T--){int n,x,y;//坐标 cin>>n;int flag=0;v.clear();//输入到vector中v.push_back(make_pair(0,0));for(int i=0;i<n;i++){    cin>>x>>y;v.push_back(make_pair(x,y));}sort(v.begin(),v.end(),cmp);//for(vector<pair<int,int> > ::iterator it=v.begin()+1;it!=v.end();it++)//迭代器遍历{//错误情况:位于右下方的点 if(it->first>(it-1)->first &&it->second<(it-1)->second){cout<<"NO"<<endl;flag=1;break;//跳出for循环  }           } if(flag) continue;//继续下一个例子cout<<"YES"<<endl;//先右后上,遍历/* for(vector<pair<int,int> >::iterator it=v.begin();it!=v.end();it++){cout<<it->first<<"  "<<it->second<<endl;}*/for(vector<pair<int,int> >::iterator it=v.begin()+1;it!=v.end();it++){if(it->first>(it-1)->first)//横坐标不同{int a=it->first-(it-1)->first;//作差,输出R个数while(a--)cout<<"R";}if(it->second>(it-1)->second)//纵坐标不同{int b=it->second-(it-1)->second;//输出U的个数while(b--)cout<<"U";}} cout<<endl;v.clear();}return 0;
}

测试结果


参考资料
https://vjudge.net/contest/359083#problem/C

希望对你有帮助。

CodeForces-1294B排序+pair使用相关推荐

  1. CodeForces - 1324 D. Pair of Topics 思维+多解法

    CodeForces - 1324 D. Pair of Topics 原题地址: http://codeforces.com/contest/1324/problem/D 基本题意: 给你一个数组, ...

  2. Codeforces 1324 D. Pair of Topics(二分)

    题意: 给出两组长度为 n n n 的数组 a i , b i a_i,b_i ai​,bi​,问满足 ( i < j ) a i + a j > b i + b j (i < j) ...

  3. codeforces 1324 D. Pair of Topics(思维)

    题目描述: 参考博客 题意: 给两个大小为 n 的数组A, B, 判断数组中有多少对 满足 Ai + Aj > Bi + Bj. 解题思路: 由于 n 的大小限制,两个for循环必然超时. 调整 ...

  4. codeforces 961 D. Pair Of Lines (几何,向量叉乘,三点共线)

    题目连接   codeforces 961.D 题意: 给出若干个点,问是否能画出两条线,保证这些点都在这两条线上 题解: 两点确定一条直线,找出一点不在这条直线上,然后枚举这三个点两两在一条直线上, ...

  5. Codeforces Round #490 (Div. 3)

    GET reverse() ve.pop_back() ve.back() A. Mishka and Contest 题意 给定一个序列表示题目的难度和Mishka的能力,问Mishka能解决几道问 ...

  6. CodeForces - 1090D Similar Arrays(构造+思维)

    题目链接:点击查看 题目大意:第一行给出两个数n和m,n代表有n个数,m代表有m组比较关系,接着给出m组关系pos_a和pos_b,代表在一个数组中,pos_a和pos_b存在一种比较关系 现在要求我 ...

  7. 知乎搜索排序模型的演进

    公众号:系统之神与我同在 导读: 搜索, 是用户获取信息, 找答案最方便快捷的方式 .一次用户搜索会经历 Query 解析 .召回 .排序多个环节, 排序作为最后整个过程一环, 对用户的体验 有最直接 ...

  8. [CSP-S模拟测试]:轰炸行动(bomb)(塔尖+拓扑排序+语文)

    题目描述 战狂也在玩<魔方王国>.他只会征兵而不会建城市,因此他决定对小奇的城市进行轰炸. 小奇有n座城市,城市之间建立了$m$条有向的地下通道.战狂会发起若干轮轰炸,每轮可以轰炸任意多个 ...

  9. c++ pair详解

    总述: 介绍pair的基本用法,包括pair的创建,排序,使用特性等 1.pair的创建 a.pair<int,int> p或者pair<int,int>p(0,1). b.可 ...

最新文章

  1. mac镜像cdr格式_设计常用文件格式!萌新必备
  2. LeetCode打卡 52八皇后Ⅱ53最大子序和54螺旋矩阵
  3. c winform 上传文件到mysql_WinForm上传文件至服务器
  4. 详解C++11智能指针
  5. 这样才能使本地Mysql服务允许被外部主机连接(两步)
  6. PhpStorm 对 AngularJS 的支持
  7. 框架复习笔记-Java-案例:牛客网讨论社区
  8. html dom的nodetype值介绍,HTML DOM nodeType 属性
  9. 探测器反向偏压_近红外和可见光双模有机光电探测器
  10. Linux下的磁盘分区与加密
  11. Java引入依赖aar_Grade将依赖的jar 打包进aar
  12. Liferay教程– Liferay门户Portlet教程
  13. VS2013中安装配置和使用Boost库
  14. javascript document.cookie
  15. 微信表情图像代表什么意思_微信表情包每个表情代表什么意思
  16. 互联网公司分布式集群架构图入门解析(简单通俗易懂,超详细)
  17. BIOS内部模块详解
  18. [JZOJ6093]【GDOI2019模拟2019.3.30】星辰大海【计算几何】【半平面交】
  19. vue中h5页面的搭建
  20. vue移动端图片裁剪上传

热门文章

  1. 字符串和json之间的互相转化
  2. mysql 事物隔离级别详解
  3. SQL获取所有用户名,数据库名、所有表名、所有字段名及字段类型
  4. asp.net mvc中ckeditor+ckfinder的配置方法
  5. 重要提醒--to 小爱
  6. [转]深入理解 __doPostBack
  7. acer clear 工具_50个能帮你节省时间的开发工具!(值得收藏)-头条
  8. 数字表达_关于数字、日期及时间的英文表达方式,这里都齐全了
  9. 第六章 逻辑回归-机器学习老师板书-斯坦福吴恩达教授
  10. STM32 电机教程 16 - PMSM电机磁场定向控制原理