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

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

大致题意:

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

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

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 const int maxn=1e5+10;
17 using namespace std;
18
19 pair<int,int> p[1005];
20
21 int main()
22 {
23     #ifdef DEBUG
24     freopen("sample.txt","r",stdin);
25     #endif
26 //    ios_base::sync_with_stdio(false);
27 //    cin.tie(NULL);
28
29     int T;
30     scanf("%d",&T);
31     while(T--)
32     {
33         int n;
34         scanf("%d",&n);
35         for(int i=1;i<=n;i++)
36         {
37             scanf("%d %d",&p[i].first,&p[i].second);
38         }
39         sort(p+1,p+1+n);
40         string ans;
41         int X,Y;
42         X=Y=0;
43         int flag=1;
44         for(int i=1;i<=n;i++)
45         {
46             for(int j=X;j<p[i].first;j++)
47                 ans+=‘R‘;
48             X=p[i].first;
49             if(p[i].second<Y)
50             {
51                 flag=0;break;
52             }
53             else
54             {
55                 for(int j=Y;j<p[i].second;j++)
56                     ans+=‘U‘;
57                 Y=p[i].second;
58             }
59         }
60         if(!flag) printf("NO\n");
61         else
62         {
63             printf("YES\n");
64             cout<<ans<<endl;
65         }
66     }
67
68     return 0;
69 }

-

原文地址:https://www.cnblogs.com/jiamian/p/12230146.html

时间: 2024-08-01 10:13:12

CodeForces 1294B Collecting Packages(排序+贪心)的相关文章

2014 Super Training #8 B Consecutive Blocks --排序+贪心

当时不知道怎么下手,后来一看原来就是排个序然后乱搞就行了. 解法不想写了,可见:http://blog.csdn.net/u013368721/article/details/28071241 其实就是滑动窗口的思想. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algor

Codeforces 442B Andrey and Problem(贪心)

题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,现在他有n个朋友,每个朋友想出题目的概率为pi,但是他可以同时向多个人寻求帮助,不过他只能要一道题,也就是如果他向两个人寻求帮助,如果两个人都成功出题,也是不可以的. 解题思路:贪心,从概率最大的人开始考虑,如果询问他使得概率变大,则要询问. #include <cstdio> #include <cstring> #include <a

Codeforces Round #300-Tourist&#39;s Notes(贪心)

Tourist's Notes Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level

HDU 4912 Paths on the tree LCA 排序贪心

lca... 排个序然后暴力保平安 _(:зゝ∠)_ #pragma comment(linker, "/STACK:102400000,102400000") #include"cstdio" #include"iostream" #include"set" #include"queue" #include"string.h" using namespace std; #define

Codeforces 432E Square Tiling(构造+贪心)

我们通常这么写 using (SqlDataReader drm = sqlComm.ExecuteReader()) { drm.Read();//以下把数据库中读出的Image流在图片框中显示出来. MemoryStream ms = new MemoryStream((byte[])drm["Logo"]); Image img = Image.FromStream(ms); this.pictureBox1.Image = img; } 我的写数据 private void b

Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序+贪心

链接: http://codeforces.com/contest/822/problem/C 题意: 有x天的假期, 有n张旅行票, 每张票有起始时间l, 结束时间r, 花费cost, 想把假期分成两部分出去旅游, 两部分时间不能重合(ri < lj || rj < li), 问最小花费是多少, 如果不能两部分, 输出-1 题解: CF官方解法, 效率O(nlogn2) 设置一个结构体, struct P{int p, len, cost, type}; 将每张票(l, r, cost) 表

CodeForces - 556D Case of Fugitive (贪心+排序)

Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water. The only dry land there is an archipelago of n narrow islands located in a row. For more comfort l

Codeforces Round #615 (Div. 3). B - Collecting Packages

题面:https://codeforces.com/contest/1294/problem/B 题目大意: 机器人从(0,0)开始,他只能往上'U'或者往右'R'走 坐标系中有着很多包裹,分别在一些点上 机器人需要走过去把这些包裹全部收集起来 问能不能做到 如果能,再输出移动方式,相同移动方式输出字典序最小的方案 解题思路: pair或者结构体排序,x与y的优先级任意,因为下一个包裹必定在当前机器人位置右方/上方/右上方 否则直接输出NO,表示不可能存在这种移动方式 在输出移动方式时,注意能先

Educational Codeforces Round 85 B. Middle Class(排序/贪心/水题)

Many years ago Berland was a small country where only nn people lived. Each person had some savings: the ii -th one had aiai burles. The government considered a person as wealthy if he had at least xx burles. To increase the number of wealthy people