Submission #563562

# Submission time Handle Problem Language Result Execution time Memory
563562 2022-05-17T13:39:27 Z farhan132 Split the sequence (APIO14_sequence) C++17
100 / 100
841 ms 107132 KB
/***Farhan132***/
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC optimize("fast-math")
 
#include <bits/stdc++.h>
 
 
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef double dd;
typedef vector<ll> vll;
typedef pair<ll , ll> ii;
typedef vector< ii > vii;
typedef pair < pair < ll , ll > , pair < ll , ll > > cm; 
typedef tuple < ll,  ll, ll > tp;
 
#define ff first
#define ss second
#define pb push_back
#define in insert
#define f0(b) for(int i=0;i<(b);i++)
#define f00(b) for(int j=0;j<(b);j++)
#define f1(b) for(int i=1;i<=(b);i++)
#define f11(b) for(int j=1;j<=(b);j++)
#define f2(a,b) for(int i=(a);i<=(b);i++)
#define f22(a,b) for(int j=(a);j<=(b);j++)
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld", &a , &b)
#define pf(a) printf("%lld\n",a)
#define pff(a,b) printf("%lld %lld\n", a , b)
#define bug printf("**!\n")
#define mem(a , b) memset(a, b ,sizeof(a))
#define front_zero(n) __builtin_clzll(n)
#define back_zero(n) __builtin_ctzll(n)
#define total_one(n) __builtin_popcount(n)
#define clean fflush(stdout)
 
//const ll mod =  (ll) 998244353;
const ll mod =  (ll) 1e9 + 7;
const ll maxn = (ll)1e8 + 5;
const int nnn = 1048590;
const int inf = numeric_limits<int>::max()-1;
//const ll INF = numeric_limits<ll>::max()-1;
const ll INF = (ll)1e18;
 
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll dxx[]={0,1,0,-1,1,1,-1,-1};
ll dyy[]={1,0,-1,0,1,-1,1,-1};
 
bool USACO = 0;
 
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
 
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll N = 1e5 + 5;

struct cht{
  
  vector < ll > m, b, idx;
  ll sz = 0, ptr = 0;

  bool bad(ll f1, ll f2, ll f3){
    return __int128((b[f1] - b[f3]) * (m[f2] - m[f1])) <= 
           __int128((b[f1] - b[f2]) * (m[f3] - m[f1]));
  }

  void add(ll _m, ll _b, ll _i){
    if(sz && m.back() == _m){
      if(b.back() > _b) return;
      m.pop_back();
      b.pop_back();
      idx.pop_back();
      sz--;
    }
    m.pb(_m);
    b.pb(_b);
    idx.pb(_i);
    sz++;

    while(sz >= 3 && bad(sz - 3, sz - 2, sz - 1)){
      m.erase(m.end()-2);
      b.erase(b.end()-2);
      idx.erase(idx.end()-2);
      sz--;
    }
  }
  ll f(ll x, ll i){
    return m[i] * x + b[i];
  }
  ii query(ll x){
    
    ptr = min(ptr, sz-1);
    while(ptr + 1 < sz && f(x, ptr) <= f(x, ptr + 1)) ptr++;
    
    return {f(x, ptr), idx[ptr]};
  }
};
int par[204][N];
bool ok[204][N];
ll dp[2][N];
ll a[N] , sum[N];

ll s(ll i,ll j){
  return sum[j] - sum[i - 1];
}

void solve(){

  mem(dp, 0);
  mem(par, -1);
  ll n, k; cin >> n >> k;

  sum[0] = 0;

  for(ll i = 1; i <= n; i++){
    cin >> a[i];
    sum[i] = sum[i - 1] + a[i];
  }
  ll row = 0;
  mem(ok, 0);

  for(ll c = 1; c <= k; c++){
    cht T;
    T.add(0, 0, -1);
    row ^= 1;
    for(ll i = 1; i < n; i++){
      
      auto [v, j] = T.query(s(1, i));
      dp[row][i] = s(1, i) * s(1, n) - s(1, i) * s(1, i) + v;
      T.add(s(1, i) , -(s(1, i)* s(1, n)) + dp[row^1][i], i);
      if(dp[row][i - 1] > dp[row][i]){
        dp[row][i] = dp[row][i-1];
        ok[c][i] = 0;
        if(ok[c][i-1] == 1) par[c][i] = i-1;
        else par[c][i] = par[c][i-1];
      }else par[c][i] = j, ok[c][i] = 1;
    }
  }

  cout << dp[row][n-1] << '\n';
  ll cur = n-1;
  for(ll c = k; c >= 1; c--){
    if(ok[c][cur] == 0) cur = par[c][cur];
    cout << cur << ' '; cur = par[c][cur];
  }


  return;
}
 
int main() {
    
    
    #ifdef LOCAL
        freopen("in", "r", stdin);
        freopen("out", "w", stdout);
        auto start_time = clock();
    #else 
       ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    #endif
 
    //pre(N);
 
    ll T = 1, CT = 0;  //cin >> T; 
 
    while(T--){
        solve();
    }
    #ifdef LOCAL
        auto end_time = clock();
        cerr<< "Execution time: "<<(end_time - start_time)*(int)1e3/CLOCKS_PER_SEC<<" ms\n";
    #endif
 
    return 0;
}

Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:175:15: warning: unused variable 'CT' [-Wunused-variable]
  175 |     ll T = 1, CT = 0;  //cin >> T;
      |               ^~
# Verdict Execution time Memory Grader output
1 Correct 39 ms 101688 KB contestant found the optimal answer: 108 == 108
2 Correct 40 ms 101652 KB contestant found the optimal answer: 999 == 999
3 Correct 40 ms 101668 KB contestant found the optimal answer: 0 == 0
4 Correct 39 ms 101664 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 40 ms 101656 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 39 ms 101576 KB contestant found the optimal answer: 1 == 1
7 Correct 38 ms 101636 KB contestant found the optimal answer: 1 == 1
8 Correct 45 ms 101668 KB contestant found the optimal answer: 1 == 1
9 Correct 40 ms 101576 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 40 ms 101592 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 40 ms 101692 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 41 ms 101580 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 39 ms 101716 KB contestant found the optimal answer: 140072 == 140072
14 Correct 40 ms 101588 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 38 ms 101600 KB contestant found the optimal answer: 805 == 805
16 Correct 39 ms 101612 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 39 ms 101628 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 40 ms 101656 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 40 ms 101624 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 44 ms 101684 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 40 ms 101568 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 40 ms 101628 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 40 ms 101576 KB contestant found the optimal answer: 933702 == 933702
7 Correct 38 ms 101580 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 39 ms 101580 KB contestant found the optimal answer: 687136 == 687136
9 Correct 41 ms 101604 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 39 ms 101600 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 39 ms 101716 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 39 ms 101656 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 40 ms 101696 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 43 ms 101744 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 40 ms 101644 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 40 ms 101580 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 40 ms 101588 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 38 ms 101668 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 40 ms 101600 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 40 ms 101632 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 38 ms 101688 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 40 ms 101644 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 47 ms 101708 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 39 ms 101740 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 44 ms 101736 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 42 ms 101624 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 45 ms 101624 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 44 ms 101724 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 40 ms 101660 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 41 ms 101708 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 42 ms 102064 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 40 ms 101836 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 98 ms 102656 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 40 ms 102012 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 80 ms 102124 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 83 ms 102068 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 95 ms 102604 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 78 ms 102268 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 84 ms 102264 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 94 ms 102256 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 55 ms 105188 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 54 ms 105020 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 638 ms 107132 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 60 ms 106280 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 841 ms 106604 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 514 ms 105204 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 632 ms 107112 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 466 ms 106788 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 513 ms 106776 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 626 ms 106880 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845