File tree 5 files changed +185
-4
lines changed
5 files changed +185
-4
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version" : " 0.2.0" ,
6
+ "configurations" : [
7
+ {
8
+ "name" : " g++ - Build and debug active file" ,
9
+ "type" : " cppdbg" ,
10
+ "request" : " launch" ,
11
+ "program" : " ${fileDirname}/${fileBasenameNoExtension}" ,
12
+ "args" : [],
13
+ "stopAtEntry" : false ,
14
+ "cwd" : " ${workspaceFolder}" ,
15
+ "environment" : [],
16
+ "externalConsole" : false ,
17
+ "MIMode" : " gdb" ,
18
+ "setupCommands" : [
19
+ {
20
+ "description" : " Enable pretty-printing for gdb" ,
21
+ "text" : " -enable-pretty-printing" ,
22
+ "ignoreFailures" : true
23
+ }
24
+ ],
25
+ "preLaunchTask" : " C/C++: g++ build active file" ,
26
+ "miDebuggerPath" : " /usr/bin/gdb"
27
+ }
28
+ ]
29
+ }
Original file line number Diff line number Diff line change 1
1
{
2
2
"files.associations" : {
3
- "cmath" : " cpp"
3
+ "cmath" : " cpp" ,
4
+ "iosfwd" : " cpp"
4
5
}
5
6
}
Original file line number Diff line number Diff line change
1
+ {
2
+ "tasks" : [
3
+ {
4
+ "type" : " shell" ,
5
+ "label" : " C/C++: g++ build active file" ,
6
+ "command" : " /usr/bin/g++" ,
7
+ "args" : [
8
+ " -g" ,
9
+ " ${file}" ,
10
+ " -o" ,
11
+ " ${fileDirname}/${fileBasenameNoExtension}"
12
+ ],
13
+ "options" : {
14
+ "cwd" : " ${workspaceFolder}"
15
+ },
16
+ "problemMatcher" : [
17
+ " $gcc"
18
+ ],
19
+ "group" : {
20
+ "kind" : " build" ,
21
+ "isDefault" : true
22
+ }
23
+ }
24
+ ],
25
+ "version" : " 2.0.0"
26
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ typedef long long int lli;
6
+ typedef vector<lli> vlli;
7
+ typedef tuple<lli, lli> ii;
8
+ typedef tuple<lli, lli, lli> iii;
9
+
10
+ void task ()
11
+ {
12
+
13
+ lli lenNumbers, lenWindow;
14
+ cin >> lenNumbers >> lenWindow;
15
+
16
+ vector<lli> res;
17
+
18
+ vector<lli> numbers;
19
+
20
+ multiset<lli> minHeap;
21
+ multiset<lli, greater<lli>> maxHeap;
22
+
23
+ lli number;
24
+ lli currentMedian = 0 ;
25
+
26
+ for (lli i = 0 ; i < lenNumbers; i++)
27
+ {
28
+ cin >> number;
29
+ numbers.emplace_back (number);
30
+
31
+ if (i >= lenWindow)
32
+ {
33
+ res.emplace_back (currentMedian);
34
+ lli toBeRemoved = numbers[i - lenWindow];
35
+ if (toBeRemoved <= currentMedian)
36
+ {
37
+ maxHeap.erase (maxHeap.find (toBeRemoved));
38
+ }
39
+ else
40
+ {
41
+ minHeap.erase (minHeap.find (toBeRemoved));
42
+ }
43
+ }
44
+
45
+ maxHeap.insert (number);
46
+ minHeap.insert (*maxHeap.begin ());
47
+ maxHeap.erase (maxHeap.begin ());
48
+ if (maxHeap.size () < minHeap.size ())
49
+ {
50
+ maxHeap.insert (*minHeap.begin ());
51
+ minHeap.erase (minHeap.begin ());
52
+ }
53
+ currentMedian = *maxHeap.begin ();
54
+ }
55
+
56
+ res.push_back (currentMedian);
57
+
58
+ for (auto r : res)
59
+ {
60
+ cout << r << ' ' ;
61
+ }
62
+
63
+ cout << ' \n ' ;
64
+ }
65
+
66
+ int main ()
67
+ {
68
+
69
+ ios::sync_with_stdio (false );
70
+ cin.tie (NULL );
71
+
72
+ task ();
73
+
74
+ return 0 ;
75
+ }
Original file line number Diff line number Diff line change @@ -84,10 +84,60 @@ void task()
84
84
ans.emplace_back (currentMedian);
85
85
}
86
86
87
- for (auto num: ans) {
88
- cout<<num<<' \n ' ;
87
+ for (auto num : ans)
88
+ {
89
+ cout << num << ' \n ' ;
90
+ }
91
+ }
92
+
93
+ void task2 ()
94
+ {
95
+
96
+ lli lenStream;
97
+ cin >> lenStream;
98
+ priority_queue<lli> maxHeap;
99
+ priority_queue<lli, vector<lli>, greater<lli>> minHeap;
100
+
101
+ lli currentMedian = 0 ;
102
+
103
+ lli number;
104
+
105
+ vector<lli> ans;
106
+
107
+ while (lenStream--)
108
+ {
109
+
110
+ cin >> number;
111
+
112
+ maxHeap.push (number);
113
+
114
+ minHeap.push (maxHeap.top ());
115
+ maxHeap.pop ();
116
+
117
+ if (maxHeap.size () < minHeap.size ())
118
+ {
119
+ maxHeap.push (minHeap.top ());
120
+ minHeap.pop ();
121
+ }
122
+
123
+ if (maxHeap.size () == minHeap.size ())
124
+ {
125
+ currentMedian = (lli)floor ((maxHeap.top () + minHeap.top ()) / 2.0 );
126
+ }
127
+ else
128
+ {
129
+ currentMedian = maxHeap.top ();
130
+ }
131
+
132
+ ans.push_back (currentMedian);
133
+ }
134
+
135
+ for (auto a : ans)
136
+ {
137
+ cout << a << ' \t ' ;
89
138
}
90
139
140
+ cout << ' \n ' ;
91
141
}
92
142
93
143
int main ()
@@ -96,7 +146,7 @@ int main()
96
146
ios::sync_with_stdio (0 );
97
147
cin.tie (0 );
98
148
99
- task ();
149
+ task2 ();
100
150
101
151
return 0 ;
102
152
}
You can’t perform that action at this time.
0 commit comments