이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 int inf = 0x3f3f3f3f ;
const int MAXN = 12 ;
using namespace std ;
int n , k ;
ll dp[ (1<<MAXN) ] , pref[MAXN] , ans = 0 ;
ll get(int i , int j) { return pref[j] - pref[i-1] ; }
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 ;
}
int main()
{
scanf("%d%d", &n , &k ) ;
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 = get(esq+1, i)*get(i+1,dir) ;
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") ;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:53: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: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] ) ;
~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |