forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkt_auction.cpp
130 lines (116 loc) · 3.94 KB
/
tkt_auction.cpp
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
/*
Author: @Suvraneel
Suvraneel Bhuin
Problem Statement:
* There are n concert tickets available, each with a certain price. Then, m customers arrive, one after another.
* Each customer announces the maximum price he or she is willing to pay for a ticket (like an auction)
* They will get a ticket with the nearest possible price such that it does not exceed the maximum price.
Input:
* The first line contains integers n and m: the number of tickets and the number of customers.
* The next line contains n integers h1,h2,…,hn: the price of each ticket.
* The last line contains m integers t1,t2,…,tm: the maximum price for each customer.
Output:
For each customer, the price that they'll pay for their ticket. Same ticket can't be purchased again.
If a customer cannot get any ticket, print −1.
Constraints:
* 1 ≤ n,m ≤2⋅105
* 1 ≤ hi,ti ≤109
*/
#include <iostream>
//For cin and cout
using namespace std;
//Swap function --> simply exchanges the values of inputs
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int partition(int array[], int lo, int hi)
{
int i = (lo - 1);
// Select the pivot element
int pivot = array[hi];
//Traverses the array - whenever an element < pivot occurs, swap it with (i+1)th element
//This leads to elements < pivot on the left & elements > pivot on the right wrt the pivot
for (int j = lo; j < hi; j++) {
if (array[j] <= pivot) {
i++;
swap(&array[i], &array[j]);
}
}
//Place the pivot element at i+1 (between smaller and larger elements)
swap(&array[i + 1], &array[hi]);
//Returns the pivot position
return (i + 1);
}
void quickSort(int array[], int lo, int hi) {
//If starting index crosses end index, then there're no elements to sort further, thus return
if( lo >= hi ){
return;
}
// Select pivot position and put all the elements smaller
// than pivot on left and greater than pivot on right
int pivot = partition(array, lo, hi);
// Sort the elements on the left of pivot
quickSort(array, lo, pivot - 1);
// Sort the elements on the right of pivot
quickSort(array, pivot + 1, hi);
}
//Main driver program
int main() {
//tkt = no. of tickets; cust = no. of customers
int tkt, cust, i, j;
//implies initially no customer had any ticket
int sold = -1;
cin >> tkt >> cust;
int price[tkt], bid[cust];
for (j = 0; j < tkt; j++)
{
//accepts price of each ticket
cin >> price[j];
}
for (i = 0; i < cust; i++)
{
//accepts maximum bid by each customer
cin >> bid[i];
}
//calls sorting algorithm on ticket price array
quickSort(price, 0, tkt-1);
//loop for considering each customer
for ( i = 0; i < cust; i++)
{
//loop for examining each ticket starting from expensive so as to confirm maximum bid
for ( j = tkt-1; j >= 0; j--)
{
//for each ticket sold
if (bid[i]>=price[j])
{
//track successful bids
sold = price[j];
//set the price as max integer possible so that noone else can buy the same ticket
price[j] = INT_MAX;
break;
}
}
//output successful bids for each customer
cout << sold << endl;
//(-1) indicates the customer was unable to acquire a ticket
//reset the 'sold' parameter for the next ticket
sold = -1;
}
return 0;
}
/*
Time complexity: Sorting = O(n) where n = no. of tickets
Auction = O(mn) where m = no. of customer
Space Complexity: O(n)
Sample Input:
5 3
5 3 7 8 5
4 8 3
Standard Output:
3
8
-1
*/