-
Notifications
You must be signed in to change notification settings - Fork 12
/
Day-1_InterquartileRange.cpp
73 lines (53 loc) · 1.14 KB
/
Day-1_InterquartileRange.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
double quad(vector<int> a)
{
double q; int n=a.size();
if(n%2==0)
q = (double)(a[n/2-1] + a[n/2])/2;
else
q = (double)a[n/2];
return q;
}
int main()
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n; cin>>n; vector<int> a(n), f(n);
for(auto i=0; i<n; i++)
cin>>a[i];
int sum=0;
for(auto i=0; i<n; i++)
{
cin>>f[i];
sum += f[i];
}
vector<int> s(sum);
int idx=0;
for(auto i=0; i<n; i++)
{
for(auto j=0; j<f[i]; j++)
{
s[idx] = a[i];
idx++;
}
}
sort(s.begin(), s.end());
vector<int> l(s.begin(), s.begin()+sum/2);
vector<int> h;
if(sum%2==0)
{
h.insert(h.end(), s.begin()+sum/2, s.end());
}
else
{
h.insert(h.end(), s.begin()+sum/2+1, s.end());
}
double Q1 = quad(l);
double Q3 = quad(h);
printf("%.1f", (Q3-Q1));
return 0;
}