UVA11134- Fabled Rooks
来源:互联网 发布:高通量数据分析是什么 编辑:程序博客网 时间:2024/06/06 02:42
题目链接
题意:在n*n棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定的矩形之内。
思路:刚开始以为是n皇后的问题,但是本题只要水平和竖直才能攻击到,并没有斜线的约束。所以可以判断出行和列是互相没有影响的,那么只要分别对行和列进行贪心操作,先按照左端点值从小到大排序,然后用优先队列维护,先处理右端点小的。
做法与这题类似 UVA1422#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int MAXN = 5005;struct Car{ int l, r, id; friend bool operator < (const Car a, const Car b) { return a.r > b.r; }}x[MAXN], y[MAXN], c[MAXN];int n;int cmp(Car a, Car b) { return a.l < b.l;}int judge(Car *cur, int flag) { sort(cur, cur + n, cmp); priority_queue<Car> q; Car state; int cnt = 0; for (int i = 0; i < n; i++) { while (cnt < n && cur[cnt].l <= i + 1) q.push(cur[cnt++]); if (q.empty()) { return false; } state = q.top(); q.pop(); if (state.r < i + 1) { return false; } if (flag == 0) c[state.id].l = i + 1; else c[state.id].r = i + 1; } return true;}void outPut() { for (int i = 0; i < n; i++) printf("%d %d\n", c[i].l, c[i].r);}int main() { while (scanf("%d", &n) && n) { for (int i = 0; i < n; i++) { scanf("%d%d%d%d", &x[i].l, &y[i].l, &x[i].r, &y[i].r); x[i].id = y[i].id = i; } if (judge(x, 0) && judge(y, 1)) outPut(); else printf("IMPOSSIBLE\n"); } return 0;}
1 0
- UVA11134- Fabled Rooks
- UVa11134 - Fabled Rooks(贪心)
- uva11134 -Fabled Rooks
- UVA11134 Fabled Rooks
- UVA11134 Fabled Rooks(贪心)
- UVA11134-Fabled Rooks(贪心)
- Uva11134 Fabled Rooks(贪心,优先级队列)
- 解题报告 之 UVA11134 Fabled Rooks
- uva11134 fabled rooks 贪心+问题独立分解
- 【高效算法设计——问题分解】Uva11134 Fabled Rooks
- Uva11134 Fabled Rooks【问题分解+贪心】【例题8-4】
- Fabled Rooks
- Fabled Rooks
- Fabled Rooks
- Fabled Rooks
- uva11134 Fabled Rocks 贪心
- uva 11134 Fabled Rooks
- Uva-11134-Fabled Rooks
- LeetCode | Trapping Rain Water(柱子间存储的水容量)
- linux面试题
- 操作系统知识
- jnlp文件 ContestAppletProd.jnlp无法打开
- 爱迪生如何面对失败
- UVA11134- Fabled Rooks
- Linux下忘记root密码怎么办?
- 某前辈在通信领域十年的总结
- POJ 2488 A Knight's Journey
- 解决安装不同版本office后造成的PIA COM组件调用失败
- 剑指offer 4.3 举例形象化问题3 - 从上到下打印二叉树
- HDU1025 Constructing Roads In JGShining's Kingdom
- 人事管理系统 c语言版
- 机房重构 之 SqlHelper