-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2002.cpp
executable file
·113 lines (86 loc) · 2.16 KB
/
2002.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
#include <iostream>
int n, size = 0, times = 0;
long long benefit = 0;
struct Stall
{
int points;
bool double_sale;
explicit Stall(int pts = 0, bool ds = false);
};
Stall::Stall(int pts, bool ds) : points(pts), double_sale(ds) {}
Stall* plan[2000010];
void push(Stall* new_item)
{
size++;
plan[size] = new_item;
for (int i = size; i > 1; i = i / 2)
{
if (plan[i]->points < plan[i/2]->points)
std::swap(plan[i], plan[i/2]);
else if (plan[i]->points == plan[i/2]->points && plan[i]->double_sale && !plan[i/2]->double_sale)
std::swap(plan[i], plan[i/2]);
else
return;
}
}
void rept(Stall* new_item)
{
delete plan[1];
plan[1] = new_item;
for (int i = 1; i <= size/2;)
{
int target = 2*i;
if (target != size)
{
if(plan[target]->points > plan[target+1]->points)
target++;
else if (plan[target]->points == plan[target+1]->points && !plan[target]->double_sale && plan[target+1]->double_sale)
target++;
}
if (plan[target]->points < plan[i]->points)
{
std::swap(plan[target], plan[i]);
i = target;
}
else if (plan[target]->points == plan[i]->points && plan[target]->double_sale && !plan[i]->double_sale)
{
std::swap(plan[target], plan[i]);
i = target;
}
else
return;
}
}
int main()
{
std::cin>>n;
int st;
for (int i = 0; i < n; ++i)
{
std::cin>>st;
if (!size)
{
plan[1] = new Stall(st);
size++;
}
else if (st <= plan[1]->points)
{
push(new Stall(st,false));
}
else
{
benefit += st - plan[1]->points;
if (plan[1]->double_sale)
{
rept(new Stall(plan[1]->points, false));
push(new Stall(st,true));
}
else
{
times+=2;
rept(new Stall(st,true));
}
}
}
std::cout<<benefit<<' '<<times<<std::endl;
}