CodeForces-1294B排序+pair使用
題目
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使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是数字化人才建设的重要性?
- 下一篇: 在部队残疾有什么补偿