답안 #63022

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
63022 2018-07-31T10:32:22 Z Bruteforceman 게임판 (CEOI13_board) C++11
70 / 100
200 ms 2712 KB
#include <bits/stdc++.h>
using namespace std;
string a, b;

string convert(string s) {
    string ans = "";
    string t = "";
    for(int i = 0; i < s.size(); i++) {
        if(!t.empty() && t.back() == 'L' && s[i] == 'R') {
            t.pop_back();
        }
        else if(!t.empty() && t.back() == 'R' && s[i] == 'L') {
            t.pop_back();
        } else {
            t += s[i];
        }
    }
    s = t;
    for(int i = 0; i < s.size(); i++) {
        if(s[i] == '1') {
            ans += "0";
        } else if (s[i] == '2') {
            ans += "1";
        } else if (s[i] == 'U') {
            ans.pop_back();
        } else if (s[i] == 'L') {
            int cur = ans.size() - 1;
            while(ans[cur] == '0') {
                ans[cur] = '1';
                --cur;
            }
            ans[cur] = '0';
        } else {
            int cur = ans.size() - 1;
            while(ans[cur] == '1') {
                ans[cur] = '0';
                --cur;
            }
            ans[cur] = '1';
        }
    }
    return ans;
}

void remove_same(string &p, string &q) {
    string x = "";
    string y = "";
    int len = 0;
    for(int i = 0; i < p.size() && i < q.size(); i++) {
        if(p[i] == q[i]) {
            len = i + 1;
        } else {
            break;
        }
    }
    x = p;
    y = q;
    p = "";
    q = "";
    for(int i = len; i < x.size(); i++) p += x[i];
    for(int i = len; i < y.size(); i++) q += y[i];
}
const int inf = 1e7;

int main(int argc, char const *argv[])
{
    cin >> a >> b;
    a = convert(a);
    b = convert(b);
    if(a.size() > b.size()) swap(a, b);
    remove_same(a, b);    
    if(a > b) {
        for(int i = 0; i < a.size(); i++) {
            a[i] = a[i] == '0' ? '1' : '0';
        }
        for(int i = 0; i < b.size(); i++) {
            b[i] = b[i] == '0' ? '1' : '0';
        }
    }
    int sz = a.size();
    while(a.size() < b.size()) a += "1";
    long long ans = sz + b.size();
    long long diff = 0;
    for(int i = 1; i <= a.size(); i++) {
        int p = a[i - 1] - '0'; 
        int q = b[i - 1] - '0';
        diff <<= 1;
        diff += q - p;
        ans = min(ans, diff + abs(sz - i) + abs((int)b.size() - i));
        if(diff > inf) break;
    }
    cout << ans << endl;
    return 0;
}

Compilation message

board.cpp: In function 'std::__cxx11::string convert(std::__cxx11::string)':
board.cpp:8:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < s.size(); i++) {
                    ~~^~~~~~~~~~
board.cpp:19:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < s.size(); i++) {
                    ~~^~~~~~~~~~
board.cpp: In function 'void remove_same(std::__cxx11::string&, std::__cxx11::string&)':
board.cpp:49:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < p.size() && i < q.size(); i++) {
                    ~~^~~~~~~~~~
board.cpp:49:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < p.size() && i < q.size(); i++) {
                                    ~~^~~~~~~~~~
board.cpp:60:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = len; i < x.size(); i++) p += x[i];
                      ~~^~~~~~~~~~
board.cpp:61:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = len; i < y.size(); i++) q += y[i];
                      ~~^~~~~~~~~~
board.cpp: In function 'int main(int, const char**)':
board.cpp:73:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < a.size(); i++) {
                        ~~^~~~~~~~~~
board.cpp:76:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < b.size(); i++) {
                        ~~^~~~~~~~~~
board.cpp:84:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 1; i <= a.size(); i++) {
                    ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 3 ms 364 KB Output is correct
3 Correct 3 ms 440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 992 KB Output is correct
2 Correct 5 ms 992 KB Output is correct
3 Correct 13 ms 1244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1244 KB Output is correct
2 Correct 2 ms 1244 KB Output is correct
3 Correct 3 ms 1244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1244 KB Output is correct
2 Correct 15 ms 1616 KB Output is correct
3 Correct 8 ms 1616 KB Output is correct
4 Correct 2 ms 1616 KB Output is correct
5 Correct 2 ms 1616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1616 KB Output is correct
2 Correct 3 ms 1616 KB Output is correct
3 Correct 3 ms 1616 KB Output is correct
4 Correct 2 ms 1616 KB Output is correct
5 Correct 4 ms 1616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1616 KB Output is correct
2 Correct 6 ms 1616 KB Output is correct
3 Correct 2 ms 1616 KB Output is correct
4 Correct 3 ms 1616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 1616 KB Output is correct
2 Correct 17 ms 2020 KB Output is correct
3 Correct 10 ms 2020 KB Output is correct
4 Correct 3 ms 2020 KB Output is correct
5 Correct 4 ms 2020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1054 ms 2328 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1091 ms 2520 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1066 ms 2712 KB Time limit exceeded
2 Halted 0 ms 0 KB -