답안 #876733

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
876733 2023-11-22T09:11:29 Z atarii Slagalica (COCI19_slagalica2) C++17
40 / 70
19 ms 10456 KB
#include<bits/stdc++.h>
using namespace std;

#define ull unsigned long long
#define ll long long
#define foru(i, a, b, k) for(int i = a; i <= b; i += k)
#define umap unordered_map
#define pii pair<int, int>
#define ford(i, a, b, k) for(int i = a; i >= b; i -= k)
#define vint vector <int>
#define all(a) (a).begin(), (a).end()
#define fi first
#define se second
#define pb push_back
#define sz(s) (int)s.size()
#define ctn continue
#define ld long double

template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
template<typename T> ll power(T a, const T b) { ll res = 1, x = a, y = b; while(y){if(y & 1)res *= x; x = x * x; y>>=1;}; return res; }
template<typename T> ll modpower(T a, T b, const T &m) { ll res = 1, x = a, y = b; x %= m; while(y){if(y & 1){res *= x; res %= m;}; x = x * x; x %= m; y >>= 1; } return res % m; }
template<typename T> T __lcm(T &a, T &b) { return a / __gcd(a, b) * b; }

template <typename T> bool getbit(T val, int i) { return (val >> i) & 1; }
template <typename T> int cntbit(T val)        { int cnt = 0; for( ; val; val -= val & (-val)) ++cnt; return cnt; }
template <typename T> T offbit(T val, int i) { return val & (~(T(1) << i)); }
template <typename T> T onbit(T val, int i) { return val | (T(1) << i); }

inline int readInt()       {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();int n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline ll readLong()       {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();ll  n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline string readString() {char c;while(c=getchar(),c==' '||c=='\n'||c=='\t');string s({c});while(c=getchar(),c!=EOF&&c!=' '&&c!='\n'&&c!='\t')s+=c;return s;}

template <typename _Tp> void write_unsign(const _Tp &__n) { if (__n > 9) { write_unsign(__n / 10); } putchar(__n % 10 + '0'); }
void write(const int       &__n) { if (__n < 0) { putchar('-'); write_unsign(-__n); } else { write_unsign(__n); } }
void write(const long long &__n) { if (__n < 0) { putchar('-'); write_unsign(-__n); } else { write_unsign(__n); } }
void write(const unsigned long long &__n) { if (__n < 0) { putchar('-'); write_unsign(-__n); } else { write_unsign(__n); } }
void write(const char      &__c) {                                                                putchar(__c);   }
void write(const string    &__s) { for (auto &__c : __s) {                                         putchar(__c); } }
template <typename _Tp, typename... _Ts> void write(const _Tp &__x, const _Ts &...__y) { write(__x), write(__y...); }

const  ll  MOD = 998244353;
const  ll  LIM = 1e6 + 10;
const int    N = 1e5 + 5;
const int  INF = 0x3f3f3f3f;
const  ll LINF = 1e17 + 100;
const int  LOG = 19;
const ll  base = 31;
const int offset = 1e3 + 1;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

inline int mul(int a, int b) { a %= MOD, b %= MOD; return 1LL * a * b % MOD; }
inline int sub(int a, int b) { a -= b; if(a < 0) a += MOD; return a; }
inline int add(int a, int b) { a += b; if(a >= MOD) a -= MOD; return a; }

int n;
int state[N], val[N];
int res[N];
bool match[10][10];
vector <int> cnt[10];

void read()
{
    n = readInt();
    foru(i, 1, n, 1)
    {
        int x, a;
        x = readInt();
        a = readInt();
        cnt[x].pb(a);
    }
}

namespace subtask3
{
    void brute(int id)
    {
        if(id > n)
        {
            bool change = false;
            foru(i, 1, n, 1) if(res[i] != val[i])
            {
                change = res[i] > val[i];
                break;
            }
            if(change)
                foru(i, 1, n, 1) res[i] = val[i];
            return;
        }

        foru(cur, 1, 8, 1) if(!cnt[cur].empty())
        {
            if(id < n && (cur == 7 || cur == 8)) continue;
            if(match[state[id - 1]][cur])
            {
                state[id] = cur, val[id] = cnt[cur].back();
                cnt[cur].pop_back();

                brute(id + 1);
                cnt[cur].push_back(val[id]);
            }
        }
    }

    void solve()
    {
        match[1][3] = match[1][4] = match[1][8] = 1;
        match[2][1] = match[2][2] = match[2][7] = 1;
        match[3][3] = match[3][4] = match[3][8] = 1;
        match[4][1] = match[4][2] = match[4][7] = 1;
        match[5][3] = match[5][4] = match[5][8] = 1;
        match[6][1] = match[6][2] = match[6][7] = 1;


        foru(i, 1, n, 1) res[i] = INF;
        foru(i, 1, 8, 1) sort(all(cnt[i]), greater <int> ());


        if(!cnt[5].empty())
        {
            state[1] = 5;
            val[1] = cnt[5].back();
            cnt[5].pop_back();
        }
        else
        {
            state[1] = 6;
            val[1] = cnt[6].back();
            cnt[6].pop_back();
        }

        brute(2);

        if(res[1] == INF) cout << - 1;
        else foru(i, 1, n, 1) cout << res[i] << " ";
    }
}

namespace subtask4
{
    vector <int> p;
    vector <int> cur[10];
    int l, r;

    void calc(vector <int> &p)
    {
        if(!match[l][p.front()] || !match[p.back()][r]) return;
        foru(i, 1, 8, 1) cur[i] = cnt[i];

        int ptr = 1;

        for(auto &x : p)
        {
            while(!cur[x].empty())
            {
                int c = cur[x].back();
                cur[x].pop_back();

                if(!match[state[ptr]][x]) return;
                state[++ptr] = x;
                val[ptr] = c;
            }
        }

        if(ptr < n - 1) return;

        bool change = false;
        foru(i, 1, n, 1) if(res[i] != val[i])
        {
            change = res[i] > val[i];
            break;
        }

        if(change)
            foru(i, 1, n, 1) res[i] = val[i];
    }

    void solve()
    {
        state[0] = 0;
        foru(i, 1, 8, 1) match[0][i] = 1;

        match[1][3] = match[1][4] = match[1][8] = 1;
        match[2][1] = match[2][2] = match[2][7] = 1;
        match[3][3] = match[3][4] = match[3][8] = 1;
        match[4][1] = match[4][2] = match[4][7] = 1;
        match[5][3] = match[5][4] = match[5][8] = 1;
        match[6][1] = match[6][2] = match[6][7] = 1;

        foru(i, 1, n, 1) res[i] = INF;
        foru(i, 1, 8, 1) sort(all(cnt[i]), greater <int> ());

        foru(i, 5, 6, 1) if(!cnt[i].empty())
            l = i;
        foru(i, 7, 8, 1) if(!cnt[i].empty())
            r = i;

        val[1] = cnt[l].back();
        state[1] = l;

        val[n] = cnt[r].back();
        state[n] = r;

        p.clear();
        foru(i, 1, 4, 1) if(!cnt[i].empty())
            p.pb(i);

        do
        {
            calc(p);
        } while(next_permutation(all(p)));

        if(res[1] == INF) cout << - 1;
        else foru(i, 1, n, 1) cout << res[i] << " ";
    }
}


void solve()
{
    if(n <= 15 || (cnt[2].empty() && cnt[3].empty())) return subtask3::solve();
    if(sz(cnt[1]) + sz(cnt[4]) <= 1) return subtask4::solve();
    cout << - 1;
}


signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    #define file "puzzle"
    if(fopen(file".inp", "r"))
    {
        freopen(file".inp", "r", stdin);
        freopen(file".out", "w", stdout);
    }

    read();
    solve();
    return 0;
}

Compilation message

slagalica.cpp: In function 'int main()':
slagalica.cpp:236:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  236 |         freopen(file".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
slagalica.cpp:237:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  237 |         freopen(file".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 10456 KB Output is correct
2 Correct 15 ms 10052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 9892 KB Output is correct
2 Correct 14 ms 9432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 3016 KB Output is correct
2 Correct 15 ms 3812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 2680 KB Output is correct
2 Correct 14 ms 3800 KB Output is correct
3 Correct 16 ms 4108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 3800 KB Output is correct
2 Correct 10 ms 2988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 3792 KB Output is correct
2 Correct 11 ms 2520 KB Output is correct
3 Correct 17 ms 4120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1880 KB Output is correct
2 Incorrect 4 ms 2080 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 1684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 1684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 1624 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 1812 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 1844 KB Output isn't correct
2 Halted 0 ms 0 KB -