Submission #135118

# Submission time Handle Problem Language Result Execution time Memory
135118 2019-07-23T16:18:12 Z CaroLinda Split the sequence (APIO14_sequence) C++14
50 / 100
172 ms 9976 KB
#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

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
1 Correct 2 ms 376 KB contestant found the optimal answer: 108 == 108
2 Correct 3 ms 376 KB contestant found the optimal answer: 999 == 999
3 Correct 2 ms 256 KB contestant found the optimal answer: 0 == 0
4 Correct 2 ms 376 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 2 ms 376 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 2 ms 364 KB contestant found the optimal answer: 1 == 1
7 Correct 2 ms 376 KB contestant found the optimal answer: 1 == 1
8 Correct 2 ms 376 KB contestant found the optimal answer: 1 == 1
9 Correct 2 ms 376 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 2 ms 376 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 2 ms 376 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 2 ms 376 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 2 ms 376 KB contestant found the optimal answer: 140072 == 140072
14 Correct 2 ms 380 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 2 ms 376 KB contestant found the optimal answer: 805 == 805
16 Correct 2 ms 376 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 2 ms 376 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 2 ms 760 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 2 ms 632 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 3 ms 760 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 2 ms 760 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 3 ms 760 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 2 ms 760 KB contestant found the optimal answer: 933702 == 933702
7 Correct 2 ms 760 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 2 ms 760 KB contestant found the optimal answer: 687136 == 687136
9 Correct 2 ms 764 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 2 ms 760 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1912 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 4 ms 1912 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 9 ms 2296 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 4 ms 1912 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 8 ms 2168 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 8 ms 2160 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 8 ms 2296 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 5 ms 2040 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 4 ms 2040 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 5 ms 2044 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8284 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 13 ms 8312 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 168 ms 9848 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 12 ms 8312 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 172 ms 9900 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 150 ms 9688 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 168 ms 9976 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 168 ms 9976 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 44 ms 8568 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 77 ms 8952 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Integer 0 violates the range [1, 9999]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -