제출 #727098

#제출 시각아이디문제언어결과실행 시간메모리
727098marvinthang순열 (APIO22_perm)C++17
100 / 100
2 ms340 KiB
/************************************* * author: marvinthang * * created: 20.04.2023 09:23:44 * *************************************/ #include <bits/stdc++.h> #include "perm.h" using namespace std; #define fi first #define se second #define left ___left #define right ___right #define TIME (1.0 * clock() / CLOCKS_PER_SEC) #define MASK(i) (1LL << (i)) #define BIT(x, i) ((x) >> (i) & 1) #define __builtin_popcount __builtin_popcountll #define ALL(v) (v).begin(), (v).end() #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i) #define REPD(i, n) for (int i = (n); i--; ) #define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; ) #define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u) #define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u) #ifdef LOCAL #include "debug.h" #else #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } #define DB(...) 23 #define db(...) 23 #define debug(...) 23 #endif template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; } template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; } template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; } template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); } template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; } // end of template vector<int> construct_permutation(long long k) { vector <int> left, right; int cur = 0; int l = __lg(k); vector <int> pos; int rem = __builtin_popcount(k) - 1; REP(i, l) { if (BIT(k, i)) { if (rem > 3 && i + 1 < l && BIT(k, i + 1)) { rem -= 2; pos.push_back(right.size()); right.push_back(cur++); left.push_back(cur++); ++i; } else { right.push_back(cur++); --rem; } } left.push_back(cur++); } reverse(ALL(pos)); reverse(ALL(right)); for (int x: pos) { x = (int) right.size() - x - 1; rotate(right.begin() + x - 2, right.begin() + x, right.begin() + x + 1); } vector <int> res(left); res.insert(res.end(), ALL(right)); return res; } #ifdef LOCAL #include "perm.h" #include <cstdio> #include <vector> #include <cassert> #include <algorithm> #include <stdlib.h> using namespace std; static long long MX=1e18; static bool check_permutation(vector<int> v) { sort(v.begin(),v.end()); for(int i=0;i<v.size();i++) if(v[i]!=i) return 0; return 1; } long long count_increasing(const vector<int>& v) { vector<long long> dp(v.size() + 1, 0); dp[0] = 1; for (int x : v) { for (int i = 0; i <= x; i++) { dp[x+1] += dp[i]; dp[x+1] = min(dp[x+1],MX+1); } } long long result = 0; for (int i = 0; i <= (int)v.size(); i++){ result += dp[i]; result = min(result,MX+1); } return result; } int main() { file("perm"); int t; assert(1 == scanf("%d", &t)); while(t--) { long long k; assert(1 == scanf("%lld",&k)); vector<int> ret=construct_permutation(k); if(!check_permutation(ret)) { printf("WA: Returned array is not a permutation\n"); exit(0); } long long inc=count_increasing(ret); if(inc!=k) { if(inc==MX+1) printf("WA: Expected %lld increasing subsequences, found more than %lld\n",k, MX); else printf("WA: Expected %lld increasing subsequences, found %lld\n",k,inc); exit(0); } printf("%d\n",(int)ret.size()); // for(int i=0;i<ret.size();i++) // { // printf("%d",ret[i]); // if(i+1==ret.size()) // printf("\n"); // else // printf(" "); // } } return 0; } #endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...