UVa OJ 1607 - Gates

UVa OJ 1607 - Gates

Problem

描述起来很麻烦,大家还是直接去OJ站看吧。我之后也会解释一下题目的意思

题目链接: 1607 - Gates

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 rst of those lines contains exactly two positive integers n and m separated by single space, 1 ≤ n ≤ 100.000, 1 ≤ m ≤ 200.000. 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 or 1 or 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

Solution

对于该题,我理解题目意思的时间是长于解题时间的

一般AC之后我会习惯性的看一下题目数据,方便我知道程序效率的提升空间,对程序作出改进。在看这道题目时,发现提交的人很少,60人左右,但是能提交基本都过了,没过的只有三四人。所以可以发现,题目难点其实主要还是读题。

输入和输出很好理解,那么这道题目到底要我们干嘛。我稍微说一下。题目的意思其实是给我们n根线,这n根线可以给低电平,也可以给高电平。现在需要我们设计一种电路,完成一项固定的任务。比如永远都是输出0,永远都是输出1,或者永远和我们输入的x相反(例如我们给x以1,则输出0),和x相同。理解了这个之后,我们就好做题了。

我们线全部给电路0,再全部给电路1,如果两者相同,电路的输出就永远是0或1,这样我们可以不要x,全部指定输入的电平,比如全0。如果两者不同,把第一个0变1,还是不同则再试第二个0变成1,直到找到x的位置,可以让电路发挥其和x的关系作用。这里用二分法可以提高查找速度。

#include<iostream>    
using namespace std;

#define LL gate[i].in1
#define RR gate[i].in2

class gateClass {
public:
    int in1, in2, out;
} gate[200005];
int n, m, all0, all1;

int getOutput(int pos) {
    for (int i = 1; i <= m; i++) {
        int x = LL>0 ? gate[LL].out:-LL<=pos;
        int y = RR>0 ? gate[RR].out:-RR<=pos;
        gate[i].out = !(x && y);
    }
    return gate[m].out;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int cas , mid;
    cin >> cas;
    while (cas--) {
        cin >> n >> m;
        for (int i = 1; i <= m; i++)
            cin >> gate[i].in1 >> gate[i].in2;
        int all0 = getOutput(0);
        int all1 = getOutput(n);
        if (all0 == all1) {
            for (int i = 1; i <= n; i++) cout << '0';
        } else {
            int lef = 1, rit = n;
            while (lef < rit) {
                mid = lef + rit >> 1;
                if (getOutput(mid) == all1) rit = mid;
                else lef = mid + 1;
            }
            for (int i = 1; i < rit; i++) cout << '1';
            cout << 'x';
            for (int i = rit + 1; i <= n; i++) cout << '0';
        }
        cout << '\n';
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
杭州电子科技大学在线评测系统(杭电OJ)中的题目1000-1100是一系列编程题,我将分别进行回答。 1000题是一个简单的入门题,要求计算两个整数的和。我们可以使用一个简单的算法,读取输入的两个整数,然后将它们相加,最后输出结果即可。 1001题是一个稍微复杂一些的题目,要求实现字符串的逆序输出。我们可以使用一个循环来逐个读取输入的字符,然后将这些字符存储在一个数组中。最后,我们可以倒序遍历数组并将字符依次输出,实现字符串的逆序输出。 1002题是一个求最大公约数的问题。我们可以使用辗转相除法来解决,即先求出两个数的余数,然后将被除数更新为除数,将除数更新为余数,直至两个数的余数为0。最后的被除数就是最大公约数。 1003题是一个比较简单的排序问题。我们可以使用冒泡排序算法来解决,即每次比较相邻的两个元素,如果它们的顺序错误就交换它们的位置。重复这个过程直至整个数组有序。 1100题是一个动态规划问题,要求计算给定序列中的最长上升子序列的长度。我们可以使用一个数组dp来保存到达每个位置的最长上升子序列的长度。每当遍历到一个位置时,我们可以将其和之前的位置比较,如果比之前位置的值大,则将其更新为之前位置的值加1,最后返回dp数组的最大值即可。 以上是对杭电OJ1000-1100题目的简要回答,涉及了一些基本的编程知识和算法思想。希望对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值