-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_woSTL.cpp
More file actions
139 lines (120 loc) · 3.09 KB
/
sort_woSTL.cpp
File metadata and controls
139 lines (120 loc) · 3.09 KB
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Person
{
public:
int index;
string name;
int age;
int money;
int member;
string join_date;
int taken;
Person(int index, string name, int age, int money, int member, string join_date, int taken)
: index(index), name(name), age(age), money(money), member(member), join_date(join_date), taken(taken) {}
};
void print_list(const vector<Person> &list);
void quicksort(vector<Person> &list, int low, int high, bool (*comp)(const Person &, const Person &));
int partition(vector<Person> &list, int low, int high, bool (*comp)(const Person &, const Person &));
bool compare(const Person &a, const Person &b)
{
if (a.member != b.member)
return a.member > b.member;
if (a.join_date != b.join_date)
return a.join_date < b.join_date;
if (a.name != b.name)
return a.name < b.name;
return a.index < b.index;
}
bool compare2(const Person &a, const Person &b)
{
if (a.money != b.money)
return a.money > b.money;
return a.index < b.index;
}
int main()
{
int num_data, num_days;
cin >> num_data >> num_days;
cin.ignore();
vector<Person> list;
int idx = 0;
while (num_data--)
{
string name, date;
int age, money, member;
getline(cin, name);
cin >> age >> money >> member >> date;
cin.ignore();
list.push_back(Person(idx++, name, age, money, member, date, 0));
}
for (int i = 0; i < num_days; i++)
{
int p, x, k;
cin >> p >> x >> k;
while (p--)
{
cin.ignore();
string name, date;
int age, money, member;
getline(cin, name);
cin >> age >> money >> member >> date;
list.push_back(Person(idx++, name, age, money, member, date, 0));
}
cout << "DAY #" << i + 1 << '\n';
quicksort(list, 0, list.size() - 1, compare);
int income = 0;
while (x-- && !list.empty())
{
Person one = list.front();
income += one.money;
cout << one.name << ' ' << one.age << '\n';
list.erase(list.begin());
}
quicksort(list, 0, list.size() - 1, compare2);
int len = list.size();
if (len > k)
{
list.erase(list.begin(), list.begin() + k);
}
else
{
list.clear();
}
income = (income / 10) * 9;
cout << "INCOME TODAY: " << income << '\n';
}
}
void quicksort(vector<Person> &list, int low, int high, bool (*comp)(const Person &, const Person &))
{
if (low < high)
{
int pi = partition(list, low, high, comp);
quicksort(list, low, pi - 1, comp);
quicksort(list, pi + 1, high, comp);
}
}
int partition(vector<Person> &list, int low, int high, bool (*comp)(const Person &, const Person &))
{
Person pivot = list[high];
int i = low - 1;
for (int j = low; j < high; j++)
{
if (comp(list[j], pivot))
{
i++;
swap(list[i], list[j]);
}
}
swap(list[i + 1], list[high]);
return i + 1;
}
void print_list(const vector<Person> &list)
{
for (const Person &p : list)
{
cout << p.index << '\t' << p.name << '\t' << p.age << '\t' << p.money << '\t' << p.member << '\t' << p.join_date << '\t' << p.taken << '\n';
}
}