Submission #916741

# Submission time Handle Problem Language Result Execution time Memory
916741 2024-01-26T12:44:09 Z sleepntsheep Match (CEOI16_match) C++17
0 / 100
2 ms 604 KB
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
#include <complex>

using u32 = unsigned;
using i32 = int;
using i64 = long long;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;

using namespace std;
using pt = complex<f80>;
#define ALL(x) begin(x), end(x)
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
#define N 100005

void setio(string s)
{
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

int n;
string s;
deque<int> q[300];
int vis[N];
int h[N<<1], ap[N];

int qry(int l, int r)
{
    int z = 1e9;
    for(l+=n,r+=n+1;l<r;l/=2,r/=2)
    {
        if(l&1)z=min(z,h[l++]);
        if(r&1)z=min(h[--r],z);
    }
    return z;
}

int main()
{
    setio("match");
    ShinLena;
    cin >> s;
    n = s.size();
    for (int i = 0; i < n; ++i) q[s[i]].push_back(i);
    auto t = s;
    for (int i = 0; i < n; ++i)
    {
        if (vis[i]) continue;
        auto x = s[i];
        auto l = i, r = q[x].back();
        q[x].pop_back();
        t[l] = '('; t[r] = ')';
        vis[r] = 1;
    }
    for (int i = 0; i < n; ++i)
        h[i+n] = h[i+n-1] + (t[i] == '(' ? 1 : -1);
    for (int i = n; i--;) h[i] = min(h[i<<1], h[i<<1|1]);

    for (int i = 0; i < n; ++i)
    {
        if (ap[h[i+n]])
        {
            int y = ap[h[i+n]] - 1;
            if (s[y] != s[i] or qry(y+1, i-1) < 0)
            {
                cout << -1;
                return 0;
            }
        }
        if(i)
        {
            ap[0] = 1;
        }
        else
        {
            ap[h[i+n-1]] = i+1;
        }
    }

    cout << t;
    return 0;
}


Compilation message

match.cpp: In function 'int main()':
match.cpp:57:39: warning: array subscript has type 'char' [-Wchar-subscripts]
   57 |     for (int i = 0; i < n; ++i) q[s[i]].push_back(i);
      |                                       ^
match.cpp:63:27: warning: array subscript has type 'char' [-Wchar-subscripts]
   63 |         auto l = i, r = q[x].back();
      |                           ^
match.cpp:64:11: warning: array subscript has type 'char' [-Wchar-subscripts]
   64 |         q[x].pop_back();
      |           ^
match.cpp: In function 'void setio(std::string)':
match.cpp:30:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
match.cpp:31:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -