UVA11134-Fabled Rooks(贪心)
来源:互联网 发布:定时继续 python 编辑:程序博客网 时间:2024/06/05 15:47
题目链接
http://acm.hust.edu.cn/vjudge/problem/34086
题意
给定一个n x n的棋盘,在棋盘上摆放n个皇后,使其不相互攻击,并且对于每个皇后,给定一个矩阵区域,该皇后只能在这个矩形区域内,输出一个合法方案/impossible
思路
对于每个皇后,其摆放的x,y坐标是独立的,于是就将题意转化成了两个一维的问题:对于一个皇后,给定区间[l, r],只能在该区间上选择一点。最终使1-n中的所有点均能被选择。
于是题目转化成了一个经典的贪心问题,类似于活动安排问题:
首先将区间按照右端点从早到晚排序,右端点相同的,按左端点从早到晚排序。然后用位置去覆盖每个点
代码
#include <iostream>#include <cstring>#include <stack>#include <vector>#include <set>#include <map>#include <cmath>#include <queue>#include <sstream>#include <iomanip>#include <fstream>#include <cstdio>#include <cstdlib>#include <climits>#include <deque>#include <bitset>#include <algorithm>using namespace std;#define PI acos(-1.0)#define LL long long#define PII pair<int, int>#define PLL pair<LL, LL>#define mp make_pair#define IN freopen("in.txt", "r", stdin)#define OUT freopen("out.txt", "wb", stdout)#define scan(x) scanf("%d", &x)#define scan2(x, y) scanf("%d%d", &x, &y)#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)#define sqr(x) (x) * (x)#define pr(x) cout << #x << " = " << x << endl#define lc o << 1#define rc o << 1 | 1#define pl() cout << endl#define CLR(a, x) memset(a, x, sizeof(a))#define FILL(a, n, x) for (int i = 0; i < n; i++) a[i] = xconst int maxn = 5005;int dx[maxn], dy[maxn], n;struct node { int l, r, id; node(int a, int b, int c) : l(a), r(b), id(c) { } node() { }} L[maxn], R[maxn];bool cmp(node x, node y) { if (x.r != y.r) return x.r < y.r; return x.l < y.l;}bool solve(node a[], int sta) { int vis[maxn], pos = 1; CLR(vis, 0); for (int i = 0; i < n; i++) { node &no = a[i]; for (int j = no.l; j <= no.r; j++) { if (vis[j]) { if (j == no.r) return false; continue; } vis[j] = 1; if (sta == 1) dx[no.id] = j; if (sta == 2) dy[no.id] = j; break; } } return true;}int main() { int x, y, z, w; while (scan(n) && n) { for (int i = 0; i < n; i++) { scanf("%d%d%d%d", &x, &y, &z, &w); L[i] = node(x, z, i); R[i] = node(y, w, i); } sort(L, L + n, cmp); sort(R, R + n, cmp); bool f1 = solve(L, 1); bool f2 = solve(R, 2); if (!f1 || !f2) puts("IMPOSSIBLE"); else for (int i = 0; i < n; i++) cout << dx[i] << ' ' << dy[i] << endl; } return 0;}
0 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【问题分解+贪心】【例题8-4】
- uva11134 Fabled Rocks 贪心
- 解题报告 之 UVA11134 Fabled Rooks
- UVa 11134 Fabled Rooks(贪心)
- UVa 11134 - Fabled Rooks(贪心)
- UVA - 11134 - Fabled Rooks(贪心)
- 贪心(Fabled Rooks,uva 11134)
- 【高效算法设计——问题分解】Uva11134 Fabled Rooks
- UVa 11134 Fabled Rooks 解题报告(贪心)
- UVA-11134-Fabled Rooks (结构体排序+贪心)
- MVC初见一
- 自定义控件 ——可拖动测量尺子
- “一盘沙拉”带你入门Dagger2(二)之带参数怎么办
- Java增加窗口、设置窗口标题
- Tomcat部署项目不需要重新启动tomcat自动加载程序
- UVA11134-Fabled Rooks(贪心)
- android Tablayout的使用
- [SCOI2007]组队
- 网红直播经济模式下 “一秒”定胜负
- jquery中toggle事件最新版本移除之后的解决
- Myeclipse下部署项目以及JSP的连接
- 如何设置cookie与读取cookie
- 自定义控件三部曲之绘图篇(十五)——QQ红点拖动删除效果实现(基本原理篇)
- RF+AppiumLibrary移动端(android)自动化测试环境搭建