Submission #793494

#TimeUsernameProblemLanguageResultExecution timeMemory
793494nnhzzzBali Sculptures (APIO15_sculpture)C++14
100 / 100
54 ms3552 KiB
// cre: Nguyen Ngoc Hung - Train VOI 2024 :>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <unordered_set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <cmath>
#include <array>
#include <cassert>
#include <random>

using namespace std;

#define    SORT_UNIQUE(v)  sort(ALL(v)),(v).resize(unique(ALL(v))-(v).begin())
#define        __nnhzzz__  signed main()
#define          BIT(i,j)  ((i>>j)&1LL)
#define           MASK(i)  (1LL<<i)
#define           RALL(x)  (x).rbegin(),(x).rend()
#define            ALL(x)  (x).begin(),(x).end()
#define             SZ(x)  (int)(x).size()
#define                fi  first
#define                se  second
#define               INF  0x3f3f3f3f
#define                ll  long long
#define               ull  unsigned long long
#define                ld  long double
#define                vi  vector<int>
#define               vvi  vector<vi>
#define              vvvi  vector<vvi>
#define             vvvvi  vector<vvvi>
#define               pii  pair<int,int>
#define              vpii  vector<pii>
#define      RESET(x,val)  memset((x),(val),sizeof(x))
#define REPDIS(i,be,en,j)  for(int i = (be); i<=(en); i+=j)
#define     REPD(i,be,en)  for(int i = (be); i>=(en); i--)
#define      REP(i,be,en)  for(int i = (be); i<=(en); i++)
#define               int  ll
//-------------------------------------------------------------//
const int oo = 1e12,LOG = 20,MAXN = 2e3+7,N = 105;
const int MOD = 1e9+7,MOD1 = 1e9+207,MOD2 = 1e9+407,MOD3 = 998244353;
//-------------------------------------------------------------//
template<typename T1, typename T2> bool mini(T1 &a, T2 b){if(a>b){a=b;return true;}return false;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b){if(a<b){a=b;return true;}return false;}
template<typename T> T gcd(T a, T b) { while(b) { a %= b; swap(a,b); } return a; }
template<typename T> T lcm(T a, T b) { return a/gcd(a,b)*b; }

/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Nguyen Ngoc Hung - nnhzzz
    Training for VOI24 gold medal
----------------------------------------------------------------
*/

int a[MAXN],s[MAXN];
int n,l,r;

// 100

void sub1(){
    int res = oo;
    REP(state,0,MASK(n)-1){
        int cnt = __builtin_popcount(state)+1;
        if(cnt<l || cnt>r || BIT(state,(n-1))){
            continue;
        }
        int pre = 0,ans = 0;
        REP(i,1,n){
            if(BIT(state,(i-1))){
                ans |= s[i]-s[pre];
                pre = i;
            }
        }
        if(pre!=n){
            ans |= s[n]-s[pre];
        }
        mini(res,ans);
    }
    cout << res;
}

int dp[53][23][515];

void sub2(){
    dp[0][0][0] = 1;
    REP(gr,0,r-1){
        REP(i,0,n){
            REP(now,0,512){
                if(dp[i][gr][now]==0){
                    continue;
                }
                REP(j,i+1,n){
                    dp[j][gr+1][now|(s[j]-s[i])] = 1;
                }
            }
        }
    }
    int res = oo;
    REP(gr,l,r){
        REP(now,0,512){
            if(dp[n][gr][now]==1){
                mini(res,now);
            }
        }
    }
    cout << res;
}

int dp1[MAXN],dp2[N][N];

bool check1(int x){
    REP(i,1,n){
        dp1[i] = oo;
    }
    dp1[0] = 0;
    REP(i,0,n){
        if(dp1[i]==oo){
            continue;
        }
        REP(j,i+1,n){
            if(((s[j]-s[i])|x)==x){
                mini(dp1[j],dp1[i]+1);
            }
        }
    }
    return (dp1[n]<=r);
}

bool check2(int x){
    REP(i,0,n){
        REP(j,0,n){
            dp2[i][j] = 0;
        }
    }
    dp2[0][0] = 1;
    REP(i,0,n-1){
        REP(j,0,i){
            if(dp2[i][j]==0){
                continue;
            }
            REP(k,i+1,n){
                if(((s[k]-s[i])|x)==x){
                    dp2[k][j+1] = 1;
                }
            }
        }
    }
    REP(i,l,r){
        if(dp2[n][i]==1){
            return true;
        }
    }
    return false;
}

void sub(){
    int state = 0,res = 0;
    REPD(i,40,0){
        if(l==1){
            res = res+1LL*MASK(i)*(!check1(res+MASK(i)-1));
        }else{
            res = res+1LL*MASK(i)*(!check2(res+MASK(i)-1));
        }
    }
    cout << res;
}

void solve(){
    cin >> n >> l >> r;
    int ma = 0;
    REP(i,1,n){
        cin >> a[i];
        s[i] = s[i-1]+a[i];
        maxi(ma,a[i]);
    }
    // sub(); return ;
    if(n<=20){
        sub1(); return ;
    }
    if(n<=50 && r<=20 && ma<=10){
        sub2(); return ;
    }
    sub();
}

__nnhzzz__{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #define name "test"
    if(fopen(name".inp","r")){
        freopen(name".inp","r",stdin);
        freopen(name".out","w",stdout);
    }
    #define name1 "nnhzzz"
    if(fopen(name1".inp","r")){
        freopen(name1".inp","r",stdin);
        freopen(name1".out","w",stdout);
    }

    int test = 1;

    while(test--){
      solve();
    }
    cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n";
    return 0;
}

Compilation message (stderr)

sculpture.cpp: In function 'void sub()':
sculpture.cpp:189:9: warning: unused variable 'state' [-Wunused-variable]
  189 |     int state = 0,res = 0;
      |         ^~~~~
sculpture.cpp: In function 'int main()':
sculpture.cpp:223:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  223 |         freopen(name".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sculpture.cpp:224:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  224 |         freopen(name".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
sculpture.cpp:228:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  228 |         freopen(name1".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
sculpture.cpp:229:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  229 |         freopen(name1".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...