Submission #743687

# Submission time Handle Problem Language Result Execution time Memory
743687 2023-05-17T17:29:00 Z MohamedAhmed04 Akcija (COCI21_akcija) C++14
110 / 110
1613 ms 32296 KB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 2004 ;

int arr[MAX] ;
int n , k ;

vector< pair<int , int> >vp ;
int w[MAX] , d[MAX] ;

int tree[4 * MAX] , lazy[4 * MAX] ;

void prop(int node , int l , int r)
{
	tree[node] += lazy[node] ;
	if(l != r)
	{
		lazy[node << 1] += lazy[node] ;
		lazy[node << 1 | 1] += lazy[node] ;
	}
	lazy[node] = 0 ;
}

void update(int node , int l , int r , int from , int to , int val)
{
	prop(node , l , r) ;
	if(from > r || to < l)
		return ;
	if(l >= from && r <= to)
	{
		lazy[node] += val ;
		prop(node , l , r) ;
		return ;
	}
	int mid = (l + r) >> 1 ;
	update(node << 1 , l , mid , from , to , val) ;
	update(node << 1 | 1 , mid+1 , r , from , to , val) ;
	tree[node] = min(tree[node << 1] , tree[node << 1 | 1]) ;
}

int FindIdx(int node , int l , int r)
{
	if(tree[node] != 0)
		return 0 ;
	if(l == r)
		return l ;
	int mid = (l + r) >> 1 ;
	prop(node << 1 , l , mid) ;
	prop(node << 1 | 1 , mid+1 , r) ;
	if(!tree[node << 1 | 1])
		return FindIdx(node << 1 | 1 , mid+1 , r) ;
	else
		return FindIdx(node << 1 , l , mid) ;
}

int state[MAX][MAX] ; //0 -> must be out, 1 -> must be in, 2 -> out and can be in, 3 -> in and can be out
int suff[MAX] ;
priority_queue< array<long long , 5> >q ;


void Insert_States(int cur , int prv , int idx1 , int idx2)
{
	memset(tree , 0 , sizeof(tree)) ;
	memset(lazy , 0 , sizeof(lazy)) ;
	memset(suff , -1 , sizeof(suff)) ;
	for(int i = 1 ; i <= n ; ++i)
	{
		state[cur][i] = state[prv][i] ;
		if(i < idx1 && state[cur][i] == 3)
			state[cur][i] = 1 ;
	}
	int sz = 0 ;
	long long sum = 0 ;
	state[cur][idx1] = 0 , state[cur][idx2] = 3 ;
	for(int i = 1 ; i <= n ; ++i)
	{
		update(1 , 1 , n , i , n , 1) ;
		if(state[cur][i] == 1 || state[cur][i] == 3)
			++sz , sum += w[i] , update(1 , 1 , n , d[i] , n , -1) ;
		if(state[cur][i] == 2)
		{
			if(suff[d[i]] == -1)
				suff[d[i]] = i ;
			else if(w[i] < w[suff[d[i]]])
				suff[d[i]] = i ;
		}
	}
	for(int i = n-1 ; i >= 1 ; --i)
	{
		if(suff[i+1] == -1)
			continue ;
		if(suff[i] == -1 || w[suff[i+1]] < w[suff[i]])
			suff[i] = suff[i+1] ;
	}
	for(int i = 1 ; i <= n ; ++i)
	{
		if(state[cur][i] != 3)
			continue ;
		update(1 , 1 , n , d[i] , n , 1) ;
		int idx = FindIdx(1 , 1 , n) + 1 ;
		if(suff[idx] != -1)
		{
			idx = suff[idx] ;
			q.push({sz , -1 * (sum - w[i] + w[idx]) , cur , i , idx}) ;
		}
		else
			q.push({sz-1 , -1 * (sum - w[i]) , cur , i , 0}) ;
		update(1 , 1 , n , d[i] , n , -1) ;
	}
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>k ;
	for(int i = 1 ; i <= n ; ++i)
	{
		int w , d ;
		cin>>w>>d ;
		vp.emplace_back(w , d) ;
	}
	sort(vp.begin() , vp.end()) ;
	for(int i = 1 ; i <= n ; ++i)
		w[i] = vp[i-1].first , d[i] = vp[i-1].second ;
	for(int i = 1 ; i <= n ; ++i)
		update(1 , 1 , n , i , n , 1) ;
	int sz = 0 ;
	long long sum = 0 ;
	for(int i = 1 ; i <= n ; ++i)
	{
		state[1][i] = 2 ;
		update(1 , 1 , n , d[i] , n , -1) ;
		if(tree[1] >= 0)
			state[1][i] = 3 , ++sz , sum += w[i] ;
		else
			update(1 , 1 , n , d[i] , n , 1) ;
	}
	q.push({sz , -1 * sum , 1 , 0 , 0}) ;
	for(int i = 1 ; i <= k ; ++i)
	{
		array<long long , 5>ans = q.top() ;
		q.pop() ;
		cout<<ans[0]<<" "<<-1ll * ans[1]<<"\n" ;
		Insert_States(i , ans[2] , ans[3] , ans[4]) ;
	}
	return 0 ;
}		
# Verdict Execution time Memory Grader output
1 Correct 3 ms 596 KB Output is correct
2 Correct 5 ms 600 KB Output is correct
3 Correct 3 ms 604 KB Output is correct
4 Correct 5 ms 596 KB Output is correct
5 Correct 4 ms 600 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 472 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 596 KB Output is correct
2 Correct 5 ms 600 KB Output is correct
3 Correct 3 ms 604 KB Output is correct
4 Correct 5 ms 596 KB Output is correct
5 Correct 4 ms 600 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 472 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 3 ms 632 KB Output is correct
15 Correct 4 ms 596 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 3 ms 500 KB Output is correct
18 Correct 3 ms 596 KB Output is correct
19 Correct 2 ms 476 KB Output is correct
20 Correct 3 ms 468 KB Output is correct
21 Correct 4 ms 476 KB Output is correct
22 Correct 4 ms 376 KB Output is correct
23 Correct 3 ms 500 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 604 KB Output is correct
2 Correct 4 ms 596 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
4 Correct 3 ms 724 KB Output is correct
5 Correct 4 ms 736 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 3 ms 436 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 5 ms 596 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 8628 KB Output is correct
2 Correct 12 ms 8344 KB Output is correct
3 Correct 13 ms 8464 KB Output is correct
4 Correct 12 ms 8404 KB Output is correct
5 Correct 17 ms 8904 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 12 ms 8524 KB Output is correct
8 Correct 10 ms 7892 KB Output is correct
9 Correct 13 ms 8524 KB Output is correct
10 Correct 1 ms 592 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 336 KB Output is correct
13 Correct 11 ms 8028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 848 KB Output is correct
2 Correct 3 ms 852 KB Output is correct
3 Correct 3 ms 852 KB Output is correct
4 Correct 4 ms 852 KB Output is correct
5 Correct 5 ms 976 KB Output is correct
6 Correct 2 ms 716 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 2 ms 724 KB Output is correct
9 Correct 2 ms 852 KB Output is correct
10 Correct 3 ms 852 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 3 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 596 KB Output is correct
2 Correct 5 ms 600 KB Output is correct
3 Correct 3 ms 604 KB Output is correct
4 Correct 5 ms 596 KB Output is correct
5 Correct 4 ms 600 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 472 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 3 ms 632 KB Output is correct
15 Correct 4 ms 596 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 3 ms 500 KB Output is correct
18 Correct 3 ms 596 KB Output is correct
19 Correct 2 ms 476 KB Output is correct
20 Correct 3 ms 468 KB Output is correct
21 Correct 4 ms 476 KB Output is correct
22 Correct 4 ms 376 KB Output is correct
23 Correct 3 ms 500 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 4 ms 604 KB Output is correct
28 Correct 4 ms 596 KB Output is correct
29 Correct 3 ms 596 KB Output is correct
30 Correct 3 ms 724 KB Output is correct
31 Correct 4 ms 736 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 3 ms 468 KB Output is correct
34 Correct 3 ms 436 KB Output is correct
35 Correct 3 ms 468 KB Output is correct
36 Correct 5 ms 596 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 14 ms 8628 KB Output is correct
41 Correct 12 ms 8344 KB Output is correct
42 Correct 13 ms 8464 KB Output is correct
43 Correct 12 ms 8404 KB Output is correct
44 Correct 17 ms 8904 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 12 ms 8524 KB Output is correct
47 Correct 10 ms 7892 KB Output is correct
48 Correct 13 ms 8524 KB Output is correct
49 Correct 1 ms 592 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 0 ms 336 KB Output is correct
52 Correct 11 ms 8028 KB Output is correct
53 Correct 3 ms 848 KB Output is correct
54 Correct 3 ms 852 KB Output is correct
55 Correct 3 ms 852 KB Output is correct
56 Correct 4 ms 852 KB Output is correct
57 Correct 5 ms 976 KB Output is correct
58 Correct 2 ms 716 KB Output is correct
59 Correct 2 ms 852 KB Output is correct
60 Correct 2 ms 724 KB Output is correct
61 Correct 2 ms 852 KB Output is correct
62 Correct 3 ms 852 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
64 Correct 1 ms 340 KB Output is correct
65 Correct 3 ms 852 KB Output is correct
66 Correct 1207 ms 16292 KB Output is correct
67 Correct 1284 ms 15652 KB Output is correct
68 Correct 1218 ms 17792 KB Output is correct
69 Correct 1258 ms 15860 KB Output is correct
70 Correct 1613 ms 24768 KB Output is correct
71 Correct 577 ms 14672 KB Output is correct
72 Correct 671 ms 15940 KB Output is correct
73 Correct 592 ms 14644 KB Output is correct
74 Correct 611 ms 16324 KB Output is correct
75 Correct 1035 ms 32296 KB Output is correct
76 Correct 1 ms 340 KB Output is correct
77 Correct 1 ms 340 KB Output is correct
78 Correct 28 ms 9116 KB Output is correct