UVA 10763 Foreign Exchange

本文介绍了一种使用map数据结构实现的配对消除算法,通过记录每一对数字出现的次数来判断一组数字对是否能够完全相互抵消。适用于解决简单的配对问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:数字a->b需要b->a来相消,输入n个配对,看是否能全部相消
解题思路:用map记录a->b出现的次数 遇到相同的就+1 遇到b->a就-1, 用cou来记录剩余未相消的配对

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
int n;
int cou;
struct con {
    int x, y;
    bool operator <(const con &c)const {
        if(x != c.x) {
            return x < c.x;
        }
        if(y != c.y) {
            return y < c.y;
        }
        return false;
    }
};
map<con, int> Ma;
void jud(int a, int b) {
    con con1;
    con1.x = b;
    con1.y = a;
    if(Ma[con1] > 0) {
        Ma[con1]--;
        cou = cou - 2;
    }
    else {
        con con2;
        con2.x = a;
        con2.y = b;
        Ma[con2]++;
    }
}
int main() {
    while(cin >> n) {
        if(n == 0)
            break;
        cou = n;
        for(int i = 0; i < n; i++) {
            int a, b;
            cin >> a >> b;
            jud(a, b);
        }
        if(cou == 0) {
            cout << "YES" << endl;
        }
        else {
            cout << "NO" << endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值