题目链接:
Codeforces 1294B Collecting Packages
思路:
将横纵坐标存在pair里并排序,此时可以得到最优解;
然后依次遍历,如果当前坐标比前一个坐标的y值要小,则输出NO;否则就依次计算答案
代码:
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define sc second
typedef pair<int, int> P;
int main() {
#ifdef MyTest
freopen("Sakura.txt", "r", stdin);
#endif
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
vector<P> v(n + 1);
v[0].fi = v[0].sc = 0;
for(int i = 1; i <= n; i++) {
cin >> v[i].fi >> v[i].sc;
}
sort(v.begin(), v.end());
string s = "";
for(int i = 1; i <= n; i++) {
if(v[i].sc < v[i - 1].sc) { puts("NO"); goto here; }
for(int j = 0; j < v[i].fi - v[i - 1].fi; j++) s += "R";
for(int j = 0; j < v[i].sc - v[i - 1].sc; j++) s += "U";
}
cout << "YES\n" << s << '\n';
here:;
}
return 0;
}