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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章