Uva - 1607 - Gates




题目理解麻烦,估计提交量少(总共只有32个人。。。)也是因为题目比较麻烦,看起来像物理题,实际理解了还可以。整个电路的功能就4种,先判断x=0和x=1的输出是否相同,吐过相同,而整个电路是常熟,随便输出;如果不同,,而二分1的个数继续测试。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset> 
#include <cassert> 
#include <cmath>
#include <functional>

using namespace std;

const int maxm = 200005;
int n, m;

struct Gates
{
	int a, b, o;
}gates[maxm];

// k个0输入,即000..0111...1这种情况的输出
int output(int k)
{
	for (int i = 1; i <= m; i++) {
		int a = gates[i].a;
		int b = gates[i].b;
		int va = a < 0 ? -a > k : gates[a].o;
		int vb = b < 0 ? -b > k : gates[b].o;
		gates[i].o = !(va && vb);
	}
	return gates[m].o;
}

int solve(int vn)
{
	int L = 1, R = n;
	while (L < R) {
		int mid = L + (R - L) / 2; // 二分1的个数
		if (output(mid) == vn) {
			R = mid;
		}
		else {
			L = mid + 1;
		}
	}
	return L;
}

int main()
{
	ios::sync_with_stdio(false);
	int T;
	cin >> T;
	while (T--) {
		cin >> n >> m;
		for (int i = 1; i <= m; i++) {
			cin >> gates[i].a >> gates[i].b;
		}
		int v0 = output(0);
		int vn = output(n);
		if (v0 == vn) { // 相同,那么整个电路就是常数
			for (int i = 1; i <= n; i++) {
				cout << "0";
			}
		}
		else {
			int x = solve(vn);
			for (int i = 1; i < x; i++) {
				cout << "0";
			}
			cout << "x";
			for (int i = x + 1; i <= n; i++) {
				cout << "1";
			}
		}
		cout << endl;
	}

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值