2015-2016 Northwestern European Regional Contest (NWERC 2015) E - Elementary Math

contest link : http://codeforces.com/gym/101485

就瞎写一题的题解吧,当时有事没去打

题意:给你n对数,问你有没有一种方法,让每一对经过 +、-、*操作,能得到n个不同的数

解题思路:很显然的二分图最大匹配了,唯一需要注意的是保存下来答案,显然直接建图是不行的,link数组开不到那么大,这时候用类似于离散化的思想,把res转换成编号(当然,要用另外一个数组存下来的),然后再跑Hungary就行了

这题感觉挺无聊的,就是无聊了想写个博客。。

#pragma GCC diagnostic error "-std=c++11"
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<ext/rope>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define gcd __gcd
#define pb push_back
#define mp make_pair
#define lowbit(x) x & (-x)
#define PII  pair<int, int> 
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(__typeof(b) i = a; i <= (b); i++)
#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 7;
const int maxn = (int)2500;
using namespace std;
using __gnu_cxx::crope;

vector<int> G[maxn];
map<ll, int> M;
ll a[maxn], b[maxn];
ll res[3*maxn], ans[maxn];
int link[3*maxn];
bool vis[3*maxn];
int n, cnt = 1;

bool dfs(int i){
	if(i == -1) return true;
	for(auto t : G[i]){
		if(vis[t]) continue;
		vis[t] = true;
		if(dfs(link[t])){
			link[t] = i;
			return true;
		}
	}
	return false;
}

int solve(){
	memset(link, -1, sizeof(link));
	int ret = 0;
	rep(i, 1, n){
		memset(vis, 0, sizeof(vis));
		if(dfs(i)) ret++;
	}
	return ret;
}

int work(ll x){
	if(!M[x]){
		M[x] = cnt++;
		res[M[x]] = x;
	}
	return M[x];
}

int main()
{
	FAST;
	cin >> n;
	rep(i, 1, n){
		cin >> a[i] >> b[i];
		G[i].pb(work(a[i] + b[i]));
		G[i].pb(work(a[i] * b[i]));
		G[i].pb(work(a[i] - b[i]));
	}
	if(solve() < n) return 0 * puts("impossible");
	rep(i, 1, cnt) if(link[i] != -1) ans[link[i]] = res[i];
	rep(i, 1, n){
		char ch;
		if(a[i] + b[i] == ans[i]) ch = '+';
		else if(a[i] * b[i] == ans[i]) ch = '*';
		else ch = '-';
		cout << a[i] << ' ' << ch << ' ' << b[i] << " = " << ans[i] << endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值