L - Fabled Rooks(中途相遇法和贪心)

Problem F: Fabled Rooks

We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrictions

  • The i-th rook can only be placed within the rectangle given by its left-upper corner (xliyli) and its right-lower corner (xriyri), where 1 ≤ i ≤ n, 1 ≤ xli ≤ xri ≤ n, 1 ≤ yli ≤ yri ≤ n.
  • No two rooks can attack each other, that is no two rooks can occupy the same column or the same row.

The input consists of several test cases. The first line of each of them contains one integer number, n, the side of the board. n lines follow giving the rectangles where the rooks can be placed as described above. The i-th line among them gives xliylixri, andyri. The input file is terminated with the integer `0‘ on a line by itself.

Your task is to find such a placing of rooks that the above conditions are satisfied and then outputn lines each giving the position of a rook in order in which their rectangles appeared in the input. If there are multiple solutions, any one will do. Output IMPOSSIBLE if there is no such placing of the rooks.

Sample input

8 
1 1 2 2 
5 7 8 8 
2 2 5 5 
2 2 5 5 
6 3 8 6 
6 3 8 5 
6 3 8 8 
3 6 7 8 
8 
1 1 2 2 
5 7 8 8 
2 2 5 5 
2 2 5 5 
6 3 8 6 
6 3 8 5 
6 3 8 8 
3 6 7 8 
0 

Output for sample input

 
1 1 
5 8 
2 4 
4 2 
7 3 
8 5 
6 6 
3 7 
1 1 
5 8 
2 4 
4 2 
7 3 
8 5 
6 6 
3 7 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 //#include <cmath>   命名冲突   y1
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <list>
13 #include <iomanip>
14 #include <cstdlib>
15 #include <sstream>
16 using namespace std;
17 typedef long long LL;
18 const int INF=0x5fffffff;
19 const double EXP=1e-8;
20 const int MS=5005;
21
22 int x1[MS], y1[MS], x2[MS], y2[MS], x[MS], y[MS];
23
24 /*
25 先将各个车分配在同一列的不同行,然后分配不同的列,
26 使他们彼此错开,任意两个车不在同一列和同一行。
27 也就是说行和列的分配时可以分开的。或者说独立的
28 使用贪心法分配。
29 */
30
31 bool solve(int *a,int *b,int *c,int n)
32 {
33    // memset(c,-1,sizeof(c));    注意这样是错误的,因为不知道c到哪里结束。字符串指针才可以,因为有结束符
34    fill(c,c+n,-1);          //  ==-1表示还没有分配
35     for(int col=1;col<=n;col++)
36     {
37         int rook=-1,minb=n+1;
38         for(int i=0;i<n;i++)
39         {
40             if(c[i]<0&&col>=a[i]&&b[i]<minb)
41             {
42                 rook=i;
43                 minb=b[i];
44             }
45         }
46         if(rook<0||col>minb)
47             return false;
48         c[rook]=col;
49     }
50     return true;
51 }
52
53 int main()
54 {
55     int n;
56     while(scanf("%d",&n)&&n)
57     {
58         for(int i=0;i<n;i++)
59             scanf("%d%d%d%d",&x1[i],&y1[i],&x2[i],&y2[i]);
60         if(solve(x1,x2,x,n)&&solve(y1,y2,y,n))
61             for(int i=0;i<n;i++)
62                 printf("%d %d\n",x[i],y[i]);
63         else
64             printf("IMPOSSIBLE\n");
65     }
66     return 0;
67 }
时间: 2024-08-05 15:15:57

L - Fabled Rooks(中途相遇法和贪心)的相关文章

uva 11134 - Fabled Rooks(主要在贪心方法及其实现)

#用到了贪心方法. #这个贪心刚开始想错了方法,后来想到了新的方法,AC 刚开始错在了按左端点升序排序并从左往右取最左端能取的格子,这个方法显然不能符合要求 比如下面这组数据: 2 1 1 3 3 1 1 3 3 2 2 2 2 错误代码: #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; struct note {

01_传说中的车(Fabled Rooks UVa 11134 贪心问题)

问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定的矩形R之内. 问题分析:1.题中最关键的一点是每辆车的x坐标和y坐标可以分开考虑(他们互不影响),不然会变得很复杂,则题目变成两次区间选点问题:使得每辆车在给定的范围内选一个点,任何两辆车不能选同一个点.  2.本题另外一个关键点是贪心法的选择,贪心方法:对所有点的区间,按右端点从小到大排序:每次在一个区间

UVa 11134 Fabled Rooks(贪心)

题意  在n*n的棋盘上的n个指定区间上各放1个'车'  使他们相互不攻击   输出一种可能的方法 行和列可以分开看  就变成了n个区间上选n个点的贪心问题  看行列是否都有解就行   基础的贪心问题  对每个点选择包含它的最优未使用空间 #include <bits/stdc++.h> using namespace std; const int N = 5005; int xl[N], yl[N], xr[N], yr[N], x[N], y[N], n; bool solve(int a

UVA - 11134 Fabled Rooks[贪心 问题分解]

UVA - 11134 Fabled Rooks We would like to place n rooks, 1 ≤ n ≤ 5000, on a n × n board subject to the following restrictions The i-th rook can only be placed within the rectan- gle given by its left-upper corner (xli,yli) and its right- lower corner

UVA 11134 - Fabled Rooks(贪心 / 二分图 + 线段树优化连边)

题目地址:Fabled Rooks 题目大意:n * n 的棋盘上摆了 n <=10^5 个车,让他们两两不攻击,每个车必须摆在一个给定矩形里,给出一个解决方案? 1. 贪心 由于行列互不影响, 所以可以分两遍求.第一遍确定每个车的行数,第二遍确定列数. 以行为例,若从左到右扫描,则按照区间的右端点升序排序,因为如果扫到一个位置两枚棋子都可以放,则选择右端点较小的那个(右端点大的后面还有机会). 2. 二分图匹配 有个毒瘤老师把题目改成了这样:n * n 的棋盘上摆了 n <=10^5 个车,

uva 6757 Cup of Cowards(中途相遇法,貌似)

uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 di?erent characters (Mage, Tank, Fighter,Assassin and Marksman). A team consists of 5 players (one from each kind) and the goal is to kill amonster with L life points. The

(中等) CF 585D Lizard Era: Beginning,中途相遇。

In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana and Worrigan. Overall the game has nmandatory quests. To perform each of them, you need to take exactly two companions. The attitude of each of the com

【中途相遇法】【STL】BAPC2014 K Key to Knowledge

题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11674&courseid=0 题目大意: N个学生M道题(1<=N<=12,1<=M<=30),每道题只有正误两种选项(0 1),每个学生的答题情况和正确题数已知,求标准答案可能有多少种. 如果标准答案只有一种则输出标准答案,否则输出解的个数. 题目思路: [

uva11134 - Fabled Rooks(问题分解,贪心法)

这道题非常好,不仅用到了把复杂问题分解为若干个熟悉的简单问题的方法,更是考察了对贪心法的理解和运用是否到位. 首先,如果直接在二维的棋盘上考虑怎么放不好弄,那么注意到x和y无关(因为两个车完全可以在同一条斜线上,这点和皇后问题不一样),那么就可以分别考虑两个一维的问题:这是一种区间选点问题,在每个区间里都只选一个点,最后这些点分别是1到n.这就联想到这样一个经典的贪心法解决的区间选点问题:数轴上有n个闭区间[ai,bi],选取尽量少的点,使得每个区间都至少含有一个点.这个问题的解决方法就是把所有