Submission #981639

#TimeUsernameProblemLanguageResultExecution timeMemory
981639crafticatPermutation (APIO22_perm)C++17
91.33 / 100
2 ms428 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int,int>; std::vector<int> construct_permutation(long long k) { k--; int ma = 1e9; int mi = 0; vector<int> data; while (k > 0) { if (k % 2 == 1) { data.push_back(ma--); k = (k - 1) / 2; } else { data.push_back(mi++); k--; } } std::reverse(data.begin(), data.end()); vector<pii> d2(data.size()); for (int i = 0; i < data.size(); ++i) { d2[i] = {data[i],i}; } std::sort(d2.begin(), d2.end()); vector<int> ans(data.size()); int iter = 0; for (auto [val, ind] : d2) { ans[ind] = iter++; } return ans; } #if DEBUG 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() { 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

Compilation message (stderr)

perm.cpp: In function 'std::vector<int> construct_permutation(long long int)':
perm.cpp:24:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for (int i = 0; i < data.size(); ++i) {
      |                     ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...