CodeForces 1294B Collecting Packages(排序+贪心)

CodeForces 1294B Collecting Packages(排序+贪心)

http://codeforces.com/contest/1294/problem/B

 

 

 

 

 

 大致题意:

一张图上有n个包裹,给出他们的坐标,一个机器人从(0,0)出发,只能向右(R)或向上(U),问能否收集到所有包裹,如果能,给出字典序最小的路径。

 

最开始当成搜索题了,其实可以排序+贪心写的。

 

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+7;
const int maxn=1e5+10;
using namespace std;
 
pair<int,int> p[1005];
 
int main()
{
    #ifdef DEBUG
    freopen("sample.txt","r",stdin);
    #endif
//    ios_base::sync_with_stdio(false);
//    cin.tie(NULL);
    
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d",&p[i].first,&p[i].second);
        }
        sort(p+1,p+1+n);
        string ans;
        int X,Y;
        X=Y=0;
        int flag=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=X;j<p[i].first;j++)
                ans+='R';
            X=p[i].first;
            if(p[i].second<Y)
            {
                flag=0;break;
            }
            else
            {
                for(int j=Y;j<p[i].second;j++)
                    ans+='U';
                Y=p[i].second;
            }
        }
        if(!flag) printf("NO\n");
        else 
        {
            printf("YES\n");
            cout<<ans<<endl;
        }
    }
    
    return 0;
}

 

 

-

posted @ 2020-01-23 13:24  jiamian22  阅读(471)  评论(0编辑  收藏  举报