Submission #91294

# Submission time Handle Problem Language Result Execution time Memory
91294 2018-12-27T03:59:23 Z YottaByte K blocks (IZhO14_blocks) C++14
0 / 100
681 ms 263168 KB
#include <stdio.h>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 1e5;

#define pb push_back
#define ll long long
#define fr first
#define sc second
#define mk make_pair

ll sum, ans = 1e9;
int n, a[N + 1], k, t[N * 4];
vector < pair < int, int > > lr[101];

void bld(int v = 1, int l = 1, int r = n)
{
	if(l == r)
	{
		t[v] = a[l];
		return;
	}
	else
	{
		int mid = l + r >> 1;
		bld(v + v, l, mid);
		bld(v + v + 1, mid + 1, r);
	}
	t[v] = max(t[v + v], t[v + v + 1]);
}

void brute(int l, int r, int q)
{
	if(q == 1)
	{
		lr[k - q].pb( mk( l, n ) );
		return;
	}
	if(l == n) return;
	else
	{
		for(int i = r; i <= n - q + 1; i++)
		{
			lr[k - q].pb( mk( l, i ) );
			brute(i + 1, i + 1, q - 1);
		}
	}
}

int getmx(int ql, int qr, int v = 1, int l = 1, int r = n)
{
	if(l > qr || ql > r) return 0;
	if(ql <= l && r <= qr)
		return t[v];
	int mid = l + r >> 1;
	return max(getmx(ql, qr, v + v, l, mid), getmx(ql, qr, v + v + 1, mid + 1, r));
}

void calc(int cur, int q, ll psum = 0)
{
	if(q == 0)
	{
		//printf("%d ", cur);
		//printf("%d\n", psum);
		ans = min(ans, psum);
		return;
	}
	else
	{
		for(int i = 0; i < lr[cur].size(); i++)
		{
			calc(cur + 1, q - 1, psum + getmx(lr[cur][i].fr, lr[cur][i].sc)); 
		}
	}
}

main()
{
	scanf("%d %d", &n, &k);
	for(int i = 1; i <= n; i++)
		scanf("%d", &a[i]), sum += a[i];
	sort(a + 1, a + 1 + n);
	
	bld();
	
	brute(1, 1, k);
	
	calc(0, k);
	
	printf("%lld", ans);
}
/**

5 2
1 2 3 4 5

**/

Compilation message

blocks.cpp: In function 'void bld(int, int, int)':
blocks.cpp:28:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int mid = l + r >> 1;
             ~~^~~
blocks.cpp: In function 'int getmx(int, int, int, int, int)':
blocks.cpp:58:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid = l + r >> 1;
            ~~^~~
blocks.cpp: In function 'void calc(int, int, long long int)':
blocks.cpp:73:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < lr[cur].size(); i++)
                  ~~^~~~~~~~~~~~~~~~
blocks.cpp: At global scope:
blocks.cpp:80:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main()
      ^
blocks.cpp: In function 'int main()':
blocks.cpp:82:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~~
blocks.cpp:84:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]), sum += a[i];
   ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 1 ms 372 KB Output is correct
3 Correct 1 ms 452 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 1 ms 524 KB Output is correct
6 Correct 1 ms 540 KB Output is correct
7 Incorrect 1 ms 560 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 624 KB Output is correct
2 Correct 2 ms 624 KB Output is correct
3 Correct 2 ms 624 KB Output is correct
4 Correct 2 ms 624 KB Output is correct
5 Correct 2 ms 624 KB Output is correct
6 Correct 2 ms 624 KB Output is correct
7 Incorrect 2 ms 624 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 681 ms 263168 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 651 ms 263168 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -