Gym - 101485E Elementary Math

题目来源:http://codeforces.com/gym/101485/attachments

二分图匹配+离散化。

WA了好多发:

1.‘+’、‘*’运算得到的结果可能相同,不要忘了加else。

2.数据会爆int,一定要用long long

 

匈牙利算法进行二分图匹配。

代码:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=9510;
int n,cnt,from[maxn*3];
ll a[maxn],b[maxn],ans[maxn];
map<ll,ll> m,p;
vector<int> v[maxn];
bool vis[maxn*3];
bool match(int x) {
    for (int i = 0; i < v[x].size(); ++i) {
        if (vis[v[x][i]] == 0) {
            vis[v[x][i]] = 1;
            if (from[v[x][i]] == -1 || match(from[v[x][i]])) {
                from[v[x][i]] = x;
                return 1;
            }
        }
    }
    return 0;
}
bool hungry() {
    memset(from, -1, sizeof(from));
    for (int i = 1; i <= n; ++i) {
        memset(vis, 0, sizeof(vis));
        if (!match(i))return 0;
    }
    return 1;
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%lld%lld", &a[i], &b[i]);
        ll x = a[i] + b[i];
        if (m.count(x) == 0) {
            ++cnt;
            m[x] = cnt;
            p[cnt] = x;
        }
        v[i].push_back(m[x]);
        x = a[i] - b[i];
        if (m.count(x) == 0) {
            ++cnt;
            m[x] = cnt;
            p[cnt] = x;
        }
        v[i].push_back(m[x]);
        x = a[i] * b[i];
        if (m.count(x) == 0) {
            ++cnt;
            m[x] = cnt;
            p[cnt] = x;
        }
        v[i].push_back(m[x]);
    }
    if (!hungry()) {
        printf("impossible\n");
        return 0;
    }
    for (int i = 1; i <= cnt; ++i) {
        if (from[i] != -1) {
            ans[from[i]] = p[i];
        }
    }
    for (int i = 1; i <= n; ++i) {
        printf("%lld ", a[i]);
        if (a[i] + b[i] == ans[i])printf("+");
        else if (a[i] - b[i] == ans[i])printf("-");
        else if (a[i] * b[i] == ans[i])printf("*");
        printf(" %lld = %lld\n", b[i], ans[i]);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值