QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#112165#6568. Space Alignmentmaomao90#AC ✓2ms3520kbC++173.2kb2023-06-10 13:53:552023-06-10 13:53:57

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-10 13:53:57]
  • 评测
  • 测评结果:AC
  • 用时:2ms
  • 内存:3520kb
  • [2023-06-10 13:53:55]
  • 提交

answer


// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 105;

int n;
string s[MAXN];
int vsp[MAXN], vtb[MAXN];

bool pos;
int ans;

void apply(int sp, int tb, int k) {
    if (vsp[k] == -1) {
        vsp[k] = sp;
        vtb[k] = tb;
        return;
    }
    if (vsp[k] == sp && vtb[k] == tb) {
        return;
    }
    // sp + tb * x = vsp[k] + vtb[k] * x
    int num = sp - vsp[k], denom = vtb[k] - tb;
    cerr << ' ' << num << ' ' << denom << ' ' << sp << ' ' << tb << ' ' << k << '\n';
    if (denom == 0 || num == 0 || num % denom != 0) {
        pos = 0;
    } else if (ans == -1) {
        ans = num / denom;
    } else if (ans != num / denom) {
        pos = 0;
    }
}

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> n;
    REP (i, 0, n) {
        cin >> s[i];
    }
    REP (i, 0, n + 1) {
        vsp[i] = vtb[i] = -1;
    }
    int psm = 0;
    pos = 1;
    ans = -1;
    REP (i, 0, n) {
        int sp = 0, tb = 0;
        REP (j, 0, SZ(s[i]) - 1) {
            if (s[i][j] == 's') {
                sp++;
            } else {
                tb++;
            }
        }
        cerr << i << ' ' << sp << ' ' << tb << '\n';
        if (s[i][SZ(s[i]) - 1] == '{') {
            apply(sp, tb, psm++);
        } else {
            apply(sp, tb, --psm);
        }
        if (!pos) {
            cout << -1 << '\n';
            return 0;
        }
    }
    if (!(vsp[0] == 0 && vtb[0] == 0)) {
        cout << -1 << '\n';
        return 0;
    }
    REP (i, 1, n + 1) {
        if (vsp[i] == -1) {
            continue;
        }
        REP (j, i + 1, n + 1) {
            if (vsp[j] == -1) {
                continue;
            }
            int lsp = vsp[i] * j, ltb = vtb[i] * j,
                rsp = vsp[j] * i, rtb = vtb[j] * i;
            if (lsp == rsp && ltb == rtb) {
                continue;
            }
            // rsp + rtb * x = lsp + ltb * x
            int num = rsp - lsp, denom = ltb - rtb;
            if (denom == 0 || num == 0 || num % denom != 0) {
                pos = 0;
            } else if (ans == -1) {
                ans = num / denom;
            } else if (ans != num / denom) {
                pos = 0;
            }
            if (!pos) {
                cout << -1 << '\n';
                return 0;
            }
        }
    }
    cout << max(ans, 1) << '\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3428kb

input:

10
{
ss{
sts{
tt}
t}
t{
ss}
}
{
}

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3416kb

input:

2
{
}

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3520kb

input:

4
{
ss{
ss}
}

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3336kb

input:

4
{
tt{
tt}
}

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 2ms
memory: 3328kb

input:

4
{
ss{
s}
}

output:

-1

result:

ok single line: '-1'

Test #6:

score: 0
Accepted
time: 2ms
memory: 3428kb

input:

4
{
tt{
t}
}

output:

-1

result:

ok single line: '-1'

Test #7:

score: 0
Accepted
time: 2ms
memory: 3420kb

input:

4
{
tt{
s}
}

output:

-1

result:

ok single line: '-1'

Test #8:

score: 0
Accepted
time: 1ms
memory: 3480kb

input:

4
{
tt{
sss}
}

output:

-1

result:

ok single line: '-1'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3424kb

input:

4
{
tt{
ssss}
}

output:

2

result:

ok single line: '2'

Test #10:

score: 0
Accepted
time: 2ms
memory: 3416kb

input:

6
{
}
{
tt{
ssss}
}

output:

2

result:

ok single line: '2'

Test #11:

score: 0
Accepted
time: 0ms
memory: 3288kb

input:

100
{
}
{
}
{
t{
ssssssssssssssssssssssssssssssssssss}
t{
t}
t{
tssssssssssssssssssssssssssssssssssss{
tssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss{
tsssssssssssssssssssssssssssssssssssst}
ttssssssssssssssssssssssssssssssssssss{
ssssssssssssssssssssssssssssssssssssssssss...

output:

36

result:

ok single line: '36'

Test #12:

score: 0
Accepted
time: 2ms
memory: 3460kb

input:

100
{
t{
tssssssssssssssssssss{
ttssssssssssssssssssss{
tsssssssssssssssssssstt{
sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssstt{
ttsssssssssssssssssssstssssssssssssssssssssssssssssssssssssssss{
sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssstsssssssss...

output:

20

result:

ok single line: '20'

Test #13:

score: 0
Accepted
time: 2ms
memory: 3492kb

input:

4
{
t{
sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss...

output:

999

result:

ok single line: '999'