Problem UVA1607-Gates

Accept: 111  Submit: 767
Time Limit: 3000 mSec

Problem Description

Input

The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 ≤ d ≤ 20. The data sets follow. Each data set consists of two consecutive lines. The first of those lines contains exactly two positive integers n and m separated by single space, 1 ≤ n ≤ 100000, 1 ≤ m ≤ 200000. Integer n is the number of the net inputs and integer m is the number of the gates in the net. The second of those lines contains exactly 2m nonzero integers, separated by single spaces. The numbers on positions 2j −1 and 2j describe the signal sources for the inputs to gate j. The positive number s means the output of gate s. The negative number s means the (−s)-th input to the net. The gates and the net inputs are numbered starting from one. The input of each gate is connected to an input of the net or to an output of a gate whose description occurred earlier in the sequence. Each net input is connected to at least one gate input. Each gate output is connected to at least one gate input except the output of the last gate that is connected to the output of the net.

 Output

The output should consist of exactly d lines, one line for each data set. The line number i should contain the answer to the i-th data set. The answer to one data set should consist of a sequence of exactly k characters terminated by the end of line (with no spaces in between). Each of those characters should be ‘0’ (the digit ‘zero’) or ‘1’ (the digit ‘one’) or ‘x’ (lower-case letter ‘x’). The i-th symbol of the sequence denotes the assignment to the i-th input of the net. If there are more than one optimal assignment then your program should output any of them (but only one).
 

 Sample Input

1
3 6
-1 -3 -1 -2 1 2 1 2 4 3 5 5
 

 Sample Output

10x

题解:这个题比较难理解的地方我觉得在二分的正确性(orz),前面的推理还是比较好理解的,如果当x为0、1时输出相同那么输出就是常数,随意输出一个01串即可,如果不同,那么输出就是x或!x,,也就是说00……0和11……1输出不同,那么从10……0、110……0一直到11……1中间必有一个状态使得11……100……0的输出和11……1相同,从第一个数开始尝试将其变为1,那么试到的第一个输出和11……1相同的状态就可以构造出一个满足条件的解,把该状态新加的1设置为x即可,然而每次输出的时间复杂度时O(m),尝试的时间复杂度时O(n),肯定会超时,这个时候据lrj的分析,可以二分1的个数来找到这个位置,这样就变成O(mlogn)(感觉紫书上写错了),这样就不会超时了。

二分正确性的简单证明:首先要明白一个问题,这个题完全有可能有多解,但是二分不是只能卡出来一个解吗,解不是应该唯一吗,这个想法时错误的,错误的原因可以通过我下面的描述看出。如果现在二分到有mid个1(也就是从左往右显是mid个1,然后全是0)如果这个解不是可行解,也就是说这个状态的输出还是和00……0相同,那么我们就可以忽略前面mid个1,后面一定还有一个状态使得输出改变,所以把解的范围缩小到mid到R是可以找到解的(即便1到L有解),如果这个解是可行解,也就是说这个状态的输出和11……1相同那么我们可以忽略从mid到R的数,从L到mid这个区间里肯定是有解的(不然怎么会输出改变),这就证明了二分的合理性。这个二分难理解就在于虽然是在二分卡一个答案,但是答案完全有可能并不唯一,被你舍弃的搜索范围并不是没有解只是你选择继续搜索的区间里一定有解。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxm =  + ;

 struct Nand {
int a, b, o;
}nand[maxm]; int n, m; int output(int k) {
for (int i = ; i <= m; i++) {
int x = nand[i].a, y = nand[i].b;
int va = x < ? -x > k : nand[x].o;
int vb = y < ? -y > k : nand[y].o;
nand[i].o = !(va && vb);
}
return nand[m].o;
} int solve(int vn) {
int l = , r = n;
int ans;
while (l <= r) {
int mid = (l + r) >> ;
if (output(mid) == vn) {
ans = mid;
r = mid - ;
}
else l = mid + ;
}
return ans;
} int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d%d", &n, &m);
for (int i = ; i <= m; i++) {
scanf("%d%d", &nand[i].a, &nand[i].b);
}
int vn = output(n), v0 = output();
if (vn == v0) {
for (int i = ; i <= n; i++) {
printf("");
}
printf("\n");
}
else {
int pos = solve(vn);
for (int i = ; i < pos; i++) printf("");
printf("x");
for (int i = pos + ; i <= n; i++) printf("");
printf("\n");
}
}
return ;
}

UVA1607-Gates(思维+二分)的更多相关文章

  1. UVA1607 Gates 与非门电路 (二分)

    题意:给你一个按发生时间的序列,表示与非门电路的输入,一开始全部输入是x,现在要改成尽量少的x,实现相同的功能. 题解:电路功能只有4中0,1,x,非x.那么如果一开始x改变了,输出结果不变,那么说明 ...

  2. Educational Codeforces Round 60 C 思维 + 二分

    https://codeforces.com/contest/1117/problem/C 题意 在一个二维坐标轴上给你一个起点一个终点(x,y<=1e9),然后给你一串字符串代表每一秒的风向, ...

  3. [NOIP10.6模拟赛]1.merchant题解--思维+二分

    题目链接: while(1)gugu(while(1)) 闲扯 考场上怕T2正解写挂其他两题没管只打了暴力,晚上发现这题思维挺妙的 同时想吐槽出题人似乎热衷卡常...我的巨大常数现在显露无疑QAQ 分 ...

  4. Voltage Keepsake CodeForces - 801C (思维+二分)

    题目链接 这是一道很棒的二分题. 思路: 首先先思考什么情况下是可以无限的使用,即输出-1. 我们思考可知,如果每一秒内所有设备的用电量总和小于等于充电器每秒可以充的电,那么这一群设备就可以无限使用. ...

  5. 【Codeforces】894D. Ralph And His Tour in Binary Country 思维+二分

    题意 给定一棵$n$个节点完全二叉树,$m$次询问,每次询问从$a$节点到其它所有节点(包括自身)的距离$L$与给定$H_a$之差$H_a-L$大于$0$的值之和 对整棵树从叶子节点到父节点从上往下预 ...

  6. C. Magic Ship (思维+二分)

    https://codeforces.com/contest/1117/problem/C 你是一个船长.最初你在点 (x1,y1) (显然,大海上的所有点都可以用平面直角坐标描述),你想去点 (x2 ...

  7. Present CodeForces - 1323D (思维+二分)

    题目大意比较简单,就是求一堆(二元组)的异或和. 思路:按位考虑,如果说第k位为1的话,那么一定有奇数个(二元组)在该位为1.二元组内的数是相加的,相加是可以进位的.所以第k位是0还是1,至于k为后边 ...

  8. HDU 5178 pairs —— 思维 + 二分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5178 pairs Time Limit: 2000/1000 MS (Java/Others)     ...

  9. BZOJ 1594: [Usaco2008 Jan]猜数游戏 线段树 + 思维 + 二分

    Code: #include<bits/stdc++.h> #define maxn 3000000 using namespace std; void setIO(string s) { ...

随机推荐

  1. mybatis XML中 遍历map写法

    <select id="selectMapTest" parameterType="java.util.HashMap" resultMap=" ...

  2. 学习HttpClient,从两个小例子开始

    前言 HTTP(Hyper-Text Transfer Protocol,超文本传输协议)在如今的互联网也许是最重要的协议,我们每天做的很多事情都与之有关,比如,网上购物.刷博客.看新闻等.偶尔你的上 ...

  3. js发送邮件确定email地址

    <a href="mailto:wjl@tom.com?subject=aaa&body=11111">test</a>

  4. angular ng-repeat出来的数据 每条修改数据后返回给接口 如何取到每个对应修改的值

    接口结构 $scope.DataList = [ { "dataA":"numA", "dataB":"numB"a } ...

  5. 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)

    题意 题目链接 Sol 复习一下01分数规划 设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\).可以二分一个答案\(k\),我们需要检查\(\ ...

  6. Unity3D手机斗地主游戏开发实战(02)_叫地主功能实现

    大体思路 前面我们实现了点击开始游戏按钮,系统依次给玩家发牌的逻辑和动画,并展示当前的手牌.这期我们继续实现接下来的功能--叫地主. 1.首先这两天,学习了DOTween,这是一个强大的Unity动画 ...

  7. Python 标准类库-Windows特殊服务之msvcrt

    标准类库-Windows特殊服务之msvcrt   by:授客 QQ:1033553122 广告:出售自研自动化小平台(无需编码也可用),有需要请联系 测试环境 win7 64位 Python 3.4 ...

  8. python爬虫从入门到放弃(九)之 Requests+正则表达式爬取猫眼电影TOP100

    import requests from requests.exceptions import RequestException import re import json from multipro ...

  9. session,cookie,sessionStorage,localStorage的区别

    浏览器的缓存机制提供了可以将用户数据存储在客户端上的方式,可以利用cookie,session等跟服务端进行数据交互. 一.cookie和session cookie和session都是用来跟踪浏览器 ...

  10. mysql之用户管理

    本文内容: 用户的介绍 查看用户 创建用户帐户 修改账户 删除帐户 关于匿名用户 首发日期:2018-04-19 用户的介绍: mysql的客户端连接是以用户名来登录服务端. 服务端可以对用户的权限来 ...