1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdio.h>
//#include <cstdlib>
#include <vector>

using namespace std;

struct rucksack
{
	long int capacity;
	vector <long int> items;
	long int empty_space;
};

void quickSort(long int arr[], long int left, long int right)
{
	long int i = left, j = right;
	long int tmp;
	long int pivot = arr[(left + right) / 2];

	/* partition */
	while (i <= j) {
		while (arr[i] < pivot)
			i++;
		while (arr[j] > pivot)
			j--;
		if (i <= j) {
			tmp = arr[i];
			arr[i] = arr[j];
			arr[j] = tmp;
			i++;
			j--;
		}
	}
	if (left < j)
		quickSort(arr, left, j);
	if (i < right)
		quickSort(arr, i, right);
}

void quickSortRucksacks(rucksack arr[], long int left, long int right)
{
	long int i = left, j = right;
	rucksack tmp;
	long int pivot = arr[(left + right) / 2].capacity;

	/* partition */
	while (i <= j) {
		while (arr[i].capacity < pivot)
			i++;
		while (arr[j].capacity > pivot)
			j--;
		if (i <= j) {
			tmp = arr[i];
			arr[i] = arr[j];
			arr[j] = tmp;
			i++;
			j--;
		}
	}
	if (left < j)
		quickSortRucksacks(arr, left, j);
	if (i < right)
		quickSortRucksacks(arr, i, right);
}

int main()
{	
	long int items_amount = 0;
	long int rucksacks_amount = 0;

	//FILE * pFile;

	//pFile = fopen ("./pak_tes/pak_tes2.in","r");

	scanf ("%ld",&items_amount); 
	scanf ("%ld",&rucksacks_amount); 
	//fscanf (pFile,"%ld",&items_amount); 
	//fscanf (pFile,"%ld",&rucksacks_amount); 
	long int summary_weight = 0;
	long int summary_space = 0;

	long int *items = new long int[items_amount];
	rucksack *rucksacks = new rucksack[rucksacks_amount];

	for(int how_many_iterator = 0; how_many_iterator < items_amount; how_many_iterator++)
	{				
		scanf ("%ld",&(items[how_many_iterator]));		
		//fscanf (pFile,"%ld",&(items[how_many_iterator]));

		summary_weight += items[how_many_iterator];
	}
	for(int how_many_iterator = 0; how_many_iterator < rucksacks_amount; how_many_iterator++)
	{				
		scanf ("%ld",&(rucksacks[how_many_iterator].capacity));	
		//fscanf (pFile,"%ld",&(rucksacks[how_many_iterator].capacity));

		rucksacks[how_many_iterator].empty_space = rucksacks[how_many_iterator].capacity;
		summary_space += rucksacks[how_many_iterator].capacity;
	}

	if(summary_weight > summary_space)
	{
		printf("NIE\n");
		//system("pause");
		return 0;
	}
	//bubble sort by damage
	// szybsze sortowanie?    !!!!!!!!!!!!!!!!!
	//sort items

	quickSort(items, 0, items_amount - 1);
	/*
	for(int i = 0; i < items_amount; i++)
	{
		for (int j = 0; j < items_amount-1; j++)
		{
			if (items[j] < items[j+1])
			{
				long int temp = items[j];
				items[j] = items[j+1];
				items[j+1] = temp;
			}
		}
	}
	*/
	//sort rucksacks
	quickSortRucksacks(rucksacks, 0, rucksacks_amount - 1);
	/*
	for(int i = 0; i < rucksacks_amount; i++)
	{
		for (int j = 0; j < rucksacks_amount-1; j++)
		{
			if (rucksacks[j].capacity < rucksacks[j+1].capacity)
			{
				rucksack temp = rucksacks[j];
				rucksacks[j] = rucksacks[j+1];
				rucksacks[j+1] = temp;
			}
		}
	}
	*/
	
	vector <long int> items_list;
	for(int i = 0; i < items_amount; i++)
	{
		items_list.push_back(items[i]);
	}

	// NO I MOZNA POPRAWIC SKRAJNE PRZYPADKI -> POBAWIC SIE W ZAMIANE PRZEDMIOTOW GDY SA ULOZONE ZEBY EFEKTYWNIEJ WYKORZYSTAC MIEJSCE
	long int rucksacks_iterator = 0;
	long int items_iterator = 0;
	rucksack *current_rucksack;
	while(!items_list.empty())
	{
		if(rucksacks_iterator == rucksacks_amount)
		{
			printf("NIE");
			//system("pause");
			return 0;
		}
		current_rucksack = &rucksacks[rucksacks_iterator];
		items_iterator = 0;
		while(current_rucksack->empty_space != 0 && items_iterator != items_list.size())
		{
			long int current_item_weight = items_list.at(items_iterator);
			if(current_rucksack->empty_space >= current_item_weight)
			{				
				//pack
				current_rucksack->items.push_back(current_item_weight);
				current_rucksack->empty_space -= current_item_weight; 
				items_list.erase(items_list.begin() + items_iterator);
			}
			else
			{
				items_iterator++;				
			}
			
		}
		rucksacks_iterator++;
	}

	//fclose (pFile);
	printf("%d\n", rucksacks_iterator);

	delete[] items;
	delete[] rucksacks;
	//system("pause");
	return 0;
}