CodeForces 350C Bombs(模拟)
2016-07-14 23:16
344 查看
C - Bombs
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
350C
Description
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th
bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no
bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position
as (x, y). In order to destroy all the bombs, the robot can perform three types of operations:
Operation has format "1 k dir". To perform the operation robot have to move in direction dirk (k ≥ 1)
times. There are only 4directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding
to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb.
Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot
can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in
its container.
Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0).
It is forbidden to perform the operation if the container has no bomb.
Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate
plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109)
— the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
Output
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations.
If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
Sample Input
Input
Output
Input
Output
题意:给你一个机器人,在(0,0),在除了(0,0)点之外的点中存在一些炸弹,你的任务是将这些炸弹移到(0,0)处销毁
直接用一个数组或者vector,将所有的炸弹加入其中,然后排序,接着一个个的销毁,如此几个步骤是不会变的:
1.到达炸弹位置
2.将炸弹移动到(0,0)
3.销毁炸弹
最终输出最少要多少步?
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
350C
Description
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th
bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no
bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position
as (x, y). In order to destroy all the bombs, the robot can perform three types of operations:
Operation has format "1 k dir". To perform the operation robot have to move in direction dirk (k ≥ 1)
times. There are only 4directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding
to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb.
Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot
can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in
its container.
Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0).
It is forbidden to perform the operation if the container has no bomb.
Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate
plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109)
— the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0).
Output
In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations.
If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.
Sample Input
Input
2 1 1 -1 -1
Output
12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3
Input
3 5 0 0 5 1 0
Output
12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
题意:给你一个机器人,在(0,0),在除了(0,0)点之外的点中存在一些炸弹,你的任务是将这些炸弹移到(0,0)处销毁
直接用一个数组或者vector,将所有的炸弹加入其中,然后排序,接着一个个的销毁,如此几个步骤是不会变的:
1.到达炸弹位置
2.将炸弹移动到(0,0)
3.销毁炸弹
最终输出最少要多少步?
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector> using namespace std; const int MAXN = 1e5 + 5; struct o{ int x, y; o(int x,int y):x(x), y(y){} bool operator < (const o &a) const{ return abs(x) + abs(y) < abs(a.x) + abs(a.y);//绝对值排序 } }; vector<o> V; int A[MAXN], n, x, y; int main(){ while(~scanf("%d", &n)){ int ans = 0; V.clear(); for(int i = 0;i < n;i ++){ scanf("%d%d", &x, &y); if(x != 0) ans ++;//判断这个点是一步到还是两步到 if(y != 0) ans ++; V.push_back(o(x, y)); } ans *= 2;//一去一回 ans += n * 2;//2操作和3操作各一回 sort(V.begin(), V.end()); printf("%d\n", ans); for(int i = 0;i < n;i ++){ if(V[i].y > 0) printf("1 %d U\n", abs(V[i].y)); if(V[i].y < 0) printf("1 %d D\n", abs(V[i].y)); if(V[i].x > 0) printf("1 %d R\n", abs(V[i].x)); if(V[i].x < 0) printf("1 %d L\n", abs(V[i].x)); printf("2\n"); if(V[i].y > 0) printf("1 %d D\n", abs(V[i].y)); if(V[i].y < 0) printf("1 %d U\n", abs(V[i].y)); if(V[i].x > 0) printf("1 %d L\n", abs(V[i].x)); if(V[i].x < 0) printf("1 %d R\n", abs(V[i].x)); printf("3\n"); } } return 0; }
相关文章推荐
- 374. Guess Number Higher or Lower
- CVPR2011论文
- 百度地图聚合点
- Java常见异常(Runtime Exception )小结
- yum本地源配置
- 两数相减遇到精度数问题时,通过先均成以100相减后再除以100
- 系统攻击工具库(持续更新)
- MVC 设定起始页后如何恢复,以及启动项的更改
- .Net基本语法学习(1)
- 373. Find K Pairs with Smallest Sums
- VMware配置nginx之后,无法再本地访问
- VNC远程登录树莓派的图形界面
- html5离线缓存使用
- PowerDesigner数据库建模
- JavaEE学习路线图
- Elipse中如何快速安装SVN?
- CVPR2012论文
- 多进程zmq通信项目 MultiProCom + 期货基础知识
- iOS 图片填充 UIImageView
- 安装oracle11.2.0.4提示缺少elfutils-libelf-devel-0.97包