例题8-17 不公平竞赛(Foul Play, ACM/ICPC NWERC 2012, UVa1609)

思路:
n = 2时,只要满足条件,目标队伍便能获胜。
采取递归分治的解法,每一步构造出满足条件的状态。
对每一步的构造方法:
首先选取黑色队与灰色队配对;再配对目标队;优先黑色队配对;剩下任意配对。


数据读入可以用char存储,%s输入。
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)  
#define SFF(a, b) scanf("%d%d", &a, &b)  
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SFFFF(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define LL long long
#define maxn 1025
#define maxm 205
#define mod 1000000007
#define inf 1000000007
#define eps 1e-4
using namespace std;
int buf[20], l;
int read() {
	int x = 0; char ch = getchar(); bool f = 0;
	while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}
void write(int x) {
	if (!x) { putchar(48); return; }
	l = 0; if (x < 0) putchar('-'), x = -x;
	while (x) buf[++l] = x % 10, x = x / 10;
	while (l) putchar(buf[l--] + 48);
}
//-------------------------chc------------------------------//
bool G[maxn][maxn];

int main() {
	//IN(); OUT();
	int n;
	while (~SF(n)) {
		FOR(i, 1, n + 1) {
			getchar();
			FOR(j, 1, n + 1) G[i][j] = getchar() - '0';
		}
		vector<int> win, lose;
		FOR(i, 2, n + 1) {
			if (G[1][i]) win.push_back(i);
			else lose.push_back(i);
		}
		while (n > 1) {
			vector<int> nw, nl, last;
			n >>= 1;
			FOR(i, 0, lose.size()) {
				int v = lose[i];
				bool matched = false;
				FOR(j, 0, win.size()) {
					int &u = win[j];
					if (u > 0 && G[u][v]) {
						printf("%d %d\n", u, v);
						nw.push_back(u);
						u = 0;			//直接通过把值修改为0,表示已匹配
						matched = true;
						break;
					}
				}
				if (!matched) last.push_back(v);
			}
			bool first = true;
			FOR(i, 0, win.size()) {
				int u = win[i];
				if (u) {
					if (first) printf("1 %d\n", u), first = false;
					else last.push_back(u);
				}
			}
			for (int i = 0; i < last.size(); i += 2) {
				int u = last[i], v = last[i + 1];
				printf("%d %d\n", last[i], last[i + 1]);
				int cur = G[u][v] ? u : v;
				if (G[1][cur]) nw.push_back(cur);
				else nl.push_back(cur);
			}
			win = nw, lose = nl;
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值