Submission #712358

#TimeUsernameProblemLanguageResultExecution timeMemory
712358BackNoobParrots (IOI11_parrots)C++14
Compilation error
0 ms0 KiB
#include "encoder.h"
#include "encoderlib.h"

string getstring(int x)
{
    string res = "";
    while(x) {
        res += char('0' + x % 10);
        x /= 10;
    }
    reverse(all(res));
    return res;
}

int getval(string tmp)
{
    int res = 0;
    for(auto it : tmp) res = res * 10 + it - '0';
    return res;
}

string add(string &a, string &b)
{
    string res = "";
    int i = sz(a) - 1;
    int j = sz(b) - 1;
    int carry = 0;
    while(i >= 0 || j >= 0) {
        int x, y;
        if(i >= 0) x = a[i] - '0';
        else x = 0;
        if(j >= 0) y = b[j] - '0';
        else y = 0;

        int tmp = (x + y + carry) % 10;
        carry = (x + y + carry) / 10;
        res += char('0' + tmp);
        --i;
        --j;
    }

    if(carry) res += char('1');
    reverse(all(res));
    return res;
}

string sub(string &a, string &b)
{
    string res = "";
    int i = sz(a) - 1;
    int j = sz(b) - 1;
    int carry = 0;
    while(i >= 0 || j >= 0) {
        int x, y;
        if(i >= 0) x = a[i] - '0';
        else x = 0;
        if(j >= 0) y = b[j] - '0';
        else y = 0;

        x -= carry;
        if(x < y) {
            x += 10;
            carry = 1;
        }
        else carry = 0;
        res += char('0' + (x - y));
        --i;
        --j;
    }

    while(res.back() == '0') res.pop_back();
    if(sz(res) == 0) res = "0";

    reverse(all(res));
    return res;
}

int cmp(string &a, string &b)
{
    if(sz(a) == sz(b)) {
        for(int i = 0; i < sz(a); i++) {
            if(a[i] == b[i]) continue;
            if(a[i] < b[i]) return -1;
            return 1;
        }

        return 0;
    }
    if(sz(a) < sz(b)) return -1;
    return 1;

}

void encode(int n, int a[])
{
    string x = "";
    for(int i = 0; i < n; i++) {
        string tmp = getstring(a[i]);
        if(i) {
            while(sz(tmp) < 3) tmp = '0' + tmp;
        }
        x += tmp;
    }

    vector<vector<string>> choose;
    choose.resize(507, vector<string>(300));

    for(int i = 0; i <= 500; i++) {
        choose[i][0] = "1";
        for(int j = 1; j <= min(256, i); j++) {
            choose[i][j] = add(choose[i - 1][j], choose[i - 1][j - 1]);
        }
    }

    int k = 1;
    while(cmp(choose[256 + k][256], x) == -1) ++k;

    int sta = 0;
    for(int i = 1; i <= k; i++) {
        for(int j = sta; j <= 255; j++) {
            string way = choose[256 - j + (k - i)][256 - j];
            int tmp = cmp(x, way);
            if(tmp <= 0) {
                send(j);
                sta = j;
                break;
            }
            else {
                x = sub(x, way);
            }
        }
    }
}
#include "decoder.h"
#include "decoderlib.h"

string getstring(int x)
{
    string res = "";
    while(x) {
        res += char('0' + x % 10);
        x /= 10;
    }
    reverse(all(res));
    return res;
}

int getval(string tmp)
{
    int res = 0;
    for(auto it : tmp) res = res * 10 + it - '0';
    return res;
}

string add(string &a, string &b)
{
    string res = "";
    int i = sz(a) - 1;
    int j = sz(b) - 1;
    int carry = 0;
    while(i >= 0 || j >= 0) {
        int x, y;
        if(i >= 0) x = a[i] - '0';
        else x = 0;
        if(j >= 0) y = b[j] - '0';
        else y = 0;

        int tmp = (x + y + carry) % 10;
        carry = (x + y + carry) / 10;
        res += char('0' + tmp);
        --i;
        --j;
    }

    if(carry) res += char('1');
    reverse(all(res));
    return res;
}

string sub(string &a, string &b)
{
    string res = "";
    int i = sz(a) - 1;
    int j = sz(b) - 1;
    int carry = 0;
    while(i >= 0 || j >= 0) {
        int x, y;
        if(i >= 0) x = a[i] - '0';
        else x = 0;
        if(j >= 0) y = b[j] - '0';
        else y = 0;

        x -= carry;
        if(x < y) {
            x += 10;
            carry = 1;
        }
        else carry = 0;
        res += char('0' + (x - y));
        --i;
        --j;
    }

    while(res.back() == '0') res.pop_back();
    if(sz(res) == 0) res = "0";

    reverse(all(res));
    return res;
}

int cmp(string &a, string &b)
{
    if(sz(a) == sz(b)) {
        for(int i = 0; i < sz(a); i++) {
            if(a[i] == b[i]) continue;
            if(a[i] < b[i]) return -1;
            return 1;
        }

        return 0;
    }
    if(sz(a) < sz(b)) return -1;
    return 1;

}

void decode(int n, int l, int a[])
{   
    vector<int> v;
    for(int i = 0; i < l; i++) v.pb(a[i]);
    sort(all(v));
    
    vector<vector<string>> choose;
    choose.resize(507, vector<string>(300));

    for(int i = 0; i <= 500; i++) {
        choose[i][0] = "1";
        for(int j = 1; j <= min(256, i); j++) {
            choose[i][j] = add(choose[i - 1][j], choose[i - 1][j - 1]);
        }
    }

    string res = "1";

    int sta = 0;
    for(int i = 1; i <= sz(v); i++) {
        int x = v[i - 1];
        for(int j = sta; j < x; j++) {
            res = add(res, choose[(256 - j) + (sz(v) - i)][(256 - j)]);
        }
        sta = x;
    }

    vector<int> ans;
    for(int i = 0; i < n; i++) {
        string tmp = "";
        if(i) {
            for(int j = 0; j < 3; j++) {
                tmp += res.back();
                res.pop_back();
            }
            reverse(all(tmp));
            ans.pb(getval(tmp));
        }
    }
    ans.pb(getval(res));
    reverse(all(ans));
    for(auto it : ans) output(it);
}

Compilation message (stderr)

encoder.cpp:4:1: error: 'string' does not name a type
    4 | string getstring(int x)
      | ^~~~~~
encoder.cpp:15:12: error: 'string' was not declared in this scope
   15 | int getval(string tmp)
      |            ^~~~~~
encoder.cpp:22:1: error: 'string' does not name a type
   22 | string add(string &a, string &b)
      | ^~~~~~
encoder.cpp:47:1: error: 'string' does not name a type
   47 | string sub(string &a, string &b)
      | ^~~~~~
encoder.cpp:78:9: error: 'string' was not declared in this scope
   78 | int cmp(string &a, string &b)
      |         ^~~~~~
encoder.cpp:78:17: error: 'a' was not declared in this scope
   78 | int cmp(string &a, string &b)
      |                 ^
encoder.cpp:78:20: error: 'string' was not declared in this scope
   78 | int cmp(string &a, string &b)
      |                    ^~~~~~
encoder.cpp:78:28: error: 'b' was not declared in this scope
   78 | int cmp(string &a, string &b)
      |                            ^
encoder.cpp:78:29: error: expression list treated as compound expression in initializer [-fpermissive]
   78 | int cmp(string &a, string &b)
      |                             ^
encoder.cpp: In function 'void encode(int, int*)':
encoder.cpp:96:5: error: 'string' was not declared in this scope
   96 |     string x = "";
      |     ^~~~~~
encoder.cpp:98:15: error: expected ';' before 'tmp'
   98 |         string tmp = getstring(a[i]);
      |               ^~~~
      |               ;
encoder.cpp:100:22: error: 'tmp' was not declared in this scope; did you mean 'cmp'?
  100 |             while(sz(tmp) < 3) tmp = '0' + tmp;
      |                      ^~~
      |                      cmp
encoder.cpp:100:19: error: 'sz' was not declared in this scope
  100 |             while(sz(tmp) < 3) tmp = '0' + tmp;
      |                   ^~
encoder.cpp:102:9: error: 'x' was not declared in this scope
  102 |         x += tmp;
      |         ^
encoder.cpp:102:14: error: 'tmp' was not declared in this scope; did you mean 'cmp'?
  102 |         x += tmp;
      |              ^~~
      |              cmp
encoder.cpp:105:5: error: 'vector' was not declared in this scope
  105 |     vector<vector<string>> choose;
      |     ^~~~~~
encoder.cpp:105:28: error: 'choose' was not declared in this scope
  105 |     vector<vector<string>> choose;
      |                            ^~~~~~
encoder.cpp:110:29: error: 'min' was not declared in this scope
  110 |         for(int j = 1; j <= min(256, i); j++) {
      |                             ^~~
encoder.cpp:111:28: error: 'add' was not declared in this scope
  111 |             choose[i][j] = add(choose[i - 1][j], choose[i - 1][j - 1]);
      |                            ^~~
encoder.cpp:116:37: error: 'x' was not declared in this scope
  116 |     while(cmp(choose[256 + k][256], x) == -1) ++k;
      |                                     ^
encoder.cpp:116:38: error: 'cmp' cannot be used as a function
  116 |     while(cmp(choose[256 + k][256], x) == -1) ++k;
      |                                      ^
encoder.cpp:121:19: error: expected ';' before 'way'
  121 |             string way = choose[256 - j + (k - i)][256 - j];
      |                   ^~~~
      |                   ;
encoder.cpp:122:27: error: 'x' was not declared in this scope
  122 |             int tmp = cmp(x, way);
      |                           ^
encoder.cpp:122:30: error: 'way' was not declared in this scope
  122 |             int tmp = cmp(x, way);
      |                              ^~~
encoder.cpp:122:33: error: 'cmp' cannot be used as a function
  122 |             int tmp = cmp(x, way);
      |                                 ^
encoder.cpp:129:21: error: 'sub' was not declared in this scope
  129 |                 x = sub(x, way);
      |                     ^~~

decoder.cpp:4:1: error: 'string' does not name a type
    4 | string getstring(int x)
      | ^~~~~~
decoder.cpp:15:12: error: 'string' was not declared in this scope
   15 | int getval(string tmp)
      |            ^~~~~~
decoder.cpp:22:1: error: 'string' does not name a type
   22 | string add(string &a, string &b)
      | ^~~~~~
decoder.cpp:47:1: error: 'string' does not name a type
   47 | string sub(string &a, string &b)
      | ^~~~~~
decoder.cpp:78:9: error: 'string' was not declared in this scope
   78 | int cmp(string &a, string &b)
      |         ^~~~~~
decoder.cpp:78:17: error: 'a' was not declared in this scope
   78 | int cmp(string &a, string &b)
      |                 ^
decoder.cpp:78:20: error: 'string' was not declared in this scope
   78 | int cmp(string &a, string &b)
      |                    ^~~~~~
decoder.cpp:78:28: error: 'b' was not declared in this scope
   78 | int cmp(string &a, string &b)
      |                            ^
decoder.cpp:78:29: error: expression list treated as compound expression in initializer [-fpermissive]
   78 | int cmp(string &a, string &b)
      |                             ^
decoder.cpp: In function 'void decode(int, int, int*)':
decoder.cpp:96:5: error: 'vector' was not declared in this scope
   96 |     vector<int> v;
      |     ^~~~~~
decoder.cpp:96:12: error: expected primary-expression before 'int'
   96 |     vector<int> v;
      |            ^~~
decoder.cpp:97:32: error: 'v' was not declared in this scope
   97 |     for(int i = 0; i < l; i++) v.pb(a[i]);
      |                                ^
decoder.cpp:98:14: error: 'v' was not declared in this scope
   98 |     sort(all(v));
      |              ^
decoder.cpp:98:10: error: 'all' was not declared in this scope
   98 |     sort(all(v));
      |          ^~~
decoder.cpp:98:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   98 |     sort(all(v));
      |     ^~~~
      |     short
decoder.cpp:100:19: error: 'string' was not declared in this scope
  100 |     vector<vector<string>> choose;
      |                   ^~~~~~
decoder.cpp:100:28: error: 'choose' was not declared in this scope
  100 |     vector<vector<string>> choose;
      |                            ^~~~~~
decoder.cpp:105:29: error: 'min' was not declared in this scope
  105 |         for(int j = 1; j <= min(256, i); j++) {
      |                             ^~~
decoder.cpp:106:28: error: 'add' was not declared in this scope
  106 |             choose[i][j] = add(choose[i - 1][j], choose[i - 1][j - 1]);
      |                            ^~~
decoder.cpp:110:11: error: expected ';' before 'res'
  110 |     string res = "1";
      |           ^~~~
      |           ;
decoder.cpp:113:25: error: 'sz' was not declared in this scope
  113 |     for(int i = 1; i <= sz(v); i++) {
      |                         ^~
decoder.cpp:116:13: error: 'res' was not declared in this scope
  116 |             res = add(res, choose[(256 - j) + (sz(v) - i)][(256 - j)]);
      |             ^~~
decoder.cpp:116:19: error: 'add' was not declared in this scope
  116 |             res = add(res, choose[(256 - j) + (sz(v) - i)][(256 - j)]);
      |                   ^~~
decoder.cpp:121:12: error: expected primary-expression before 'int'
  121 |     vector<int> ans;
      |            ^~~
decoder.cpp:123:15: error: expected ';' before 'tmp'
  123 |         string tmp = "";
      |               ^~~~
      |               ;
decoder.cpp:126:17: error: 'tmp' was not declared in this scope; did you mean 'cmp'?
  126 |                 tmp += res.back();
      |                 ^~~
      |                 cmp
decoder.cpp:126:24: error: 'res' was not declared in this scope
  126 |                 tmp += res.back();
      |                        ^~~
decoder.cpp:129:25: error: 'tmp' was not declared in this scope; did you mean 'cmp'?
  129 |             reverse(all(tmp));
      |                         ^~~
      |                         cmp
decoder.cpp:129:13: error: 'reverse' was not declared in this scope
  129 |             reverse(all(tmp));
      |             ^~~~~~~
decoder.cpp:130:13: error: 'ans' was not declared in this scope
  130 |             ans.pb(getval(tmp));
      |             ^~~
decoder.cpp:130:30: error: 'getval' cannot be used as a function
  130 |             ans.pb(getval(tmp));
      |                              ^
decoder.cpp:133:5: error: 'ans' was not declared in this scope
  133 |     ans.pb(getval(res));
      |     ^~~
decoder.cpp:133:19: error: 'res' was not declared in this scope
  133 |     ans.pb(getval(res));
      |                   ^~~
decoder.cpp:133:22: error: 'getval' cannot be used as a function
  133 |     ans.pb(getval(res));
      |                      ^
decoder.cpp:134:5: error: 'reverse' was not declared in this scope
  134 |     reverse(all(ans));
      |     ^~~~~~~