답안 #876689

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
876689 2023-11-22T08:32:26 Z atarii Slagalica (COCI19_slagalica2) C++17
20 / 70
1000 ms 11172 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);
    }
}

void brute(int id)
{
    //cout << id - 1 << " " << state[id - 1] << "\n";
    if(id > n)
    {
//        foru(i, 1, n, 1) cout << val[i] << " ";
//        cout << "\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 == 3) cout << cur << " ";
        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] << " ";
}

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:150:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  150 |         freopen(file".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
slagalica.cpp:151:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  151 |         freopen(file".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 10712 KB Output is correct
2 Correct 15 ms 10300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 10136 KB Output is correct
2 Correct 13 ms 10076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 6868 KB Output is correct
2 Execution timed out 1038 ms 6540 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 5760 KB Output is correct
2 Correct 19 ms 9940 KB Output is correct
3 Execution timed out 1069 ms 7012 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1016 ms 9684 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 10192 KB Output is correct
2 Correct 12 ms 6360 KB Output is correct
3 Execution timed out 1071 ms 10340 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1074 ms 9448 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1010 ms 11092 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1034 ms 9308 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1032 ms 11172 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1028 ms 9308 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1049 ms 9564 KB Time limit exceeded
2 Halted 0 ms 0 KB -