This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define lp(i,a,b) for(int i=a;i<b;i++)
#define pii pair<int,int>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
const ll inf = 1e15 ;
const int MAXN = 1010 ;
const int MAXK = 1010 ;
const int MAXNparcial = 15 ;
using namespace std ;
int n , k ;
bool isOn(int m, int bit) { return ( (m&(1<<bit)) != 0 ) ; }
int countBits(int m)
{
int tot = 0 ;
lp(i,1,n+1)
if( isOn(m,i) ) tot ++ ;
return tot ;
}
int findDir(int m,int bit)
{
lp(i,bit+1,n+1)
if(isOn(m,i)) return i ;
return n ;
}
int findEsq(int m, int bit)
{
for(int i = bit-1 ; i > 0 ; i-- )
if( isOn(m, i) ) return i ;
return 0 ;
}
void parcial()
{
ll dp[ (1<<MAXNparcial) ] , pref[MAXNparcial] , ans = 0 ;
lp(i,1,n+1) scanf("%lld", &pref[i] ) ;
lp(i,1,n+1) pref[i] += pref[i-1] ;
for(int m = 0 ; m < (1<<(n+1)) ; m ++ )
{
if( countBits(m) >= k || isOn(m,0)) continue ;
lp(i,1,n+1)
if( !isOn(m,i) )
{
int esq = findEsq( m , i ) ;
int dir = findDir( m , i ) ;
ll ganho = (pref[i]-pref[esq])*(pref[dir]-pref[i]) ;
dp[ (m|(1<<i) ) ] = max(dp[ (m|(1<<i) ) ] , dp[m] + ganho ) ;
}
}
lp(m,0,(1<<(n+1)))
if( countBits(m) == k && !isOn(m,0) )
ans = max(ans, dp[m]) ;
printf("%lld\n" , ans ) ;
lp(m,0,(1<<(n+1))-1 )
if( countBits(m) == k && !isOn(m,0) && dp[m] == ans )
{
lp(i,1,n+1)
if(isOn(m,i)) printf("%d " , i ) ;
break ;
}
printf("\n") ;
}
ll pref[MAXN] , dp[MAXN][MAXK] ;
int cut[MAXN][MAXK] ;
vector<int> ans ;
ll get(int i , int j) { return pref[j] - pref[i-1] ; }
void buildAns(int ini, int c)
{
if(c==0) return ;
ans.pb( cut[ini][c] ) ;
buildAns(cut[ini][c]+1,c-1) ;
}
int main()
{
scanf("%d%d", &n , &k ) ;
if(n<=10) {parcial() ; return 0 ;}
lp(i,1,n+1) scanf("%lld", &pref[i] ) ;
lp(i,1,n+1) pref[i] += pref[i-1] ;
for(int c = 1 ; c <= k ; c++)
{
for(int i = n ; i > 0 ; i-- )
{
if( n - i < c ) { dp[i][c] = -inf ; continue ; }
for(int j = i ; j < n ; j++ )
{
ll ganho = dp[j+1][c-1] + get(i,j)*get(j+1,n) ;
if(ganho > dp[i][c])
cut[i][c] = j , dp[i][c] = ganho ;
}
}
}
buildAns(1,k) ;
printf("%lld\n" , dp[1][k]);
lp(i,0,k) printf("%d ", ans[i]) ;
printf("\n");
}
Compilation message (stderr)
sequence.cpp: In function 'void parcial()':
sequence.cpp:54:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
lp(i,1,n+1) scanf("%lld", &pref[i] ) ;
~~~~~^~~~~~~~~~~~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:110:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n , &k ) ;
~~~~~^~~~~~~~~~~~~~~~~~
sequence.cpp:114:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
lp(i,1,n+1) scanf("%lld", &pref[i] ) ;
~~~~~^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |