请写出实现代码
时间: 2023-11-12 11:06:05 浏览: 20
```c++
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int N = 100010, M = 1000010;
struct Person {
int sex, time, wait;
bool operator < (const Person& other) const {
return time < other.time;
}
};
int n, m;
int len[N];
Person p[M];
queue<Person> male, female;
double solve(queue<Person>& q) {
double res = 0;
int cnt = 0;
while (q.size()) {
auto cur = q.front(); q.pop();
res += cur.wait;
cnt++;
}
return cnt ? res / cnt : 0;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> len[i];
cin >> m;
for (int i = 0; i < m; i++) cin >> p[i].sex >> p[i].time;
sort(p, p + m);
int cur = 0;
for (int i = 1; i <= n; i++) {
int t = len[i];
while (cur < m && p[cur].time < t) {
auto& person = p[cur];
person.wait = person.time - len[i - 1];
if (person.sex == 1) {
male.push(person);
} else {
female.push(person);
}
cur++;
}
vector<Person> v;
int cnt = min(male.size(), female.size());
for (int j = 0; j < cnt; j++) {
auto m = male.front(); male.pop();
auto f = female.front(); female.pop();
m.wait = f.wait = t - max(m.time, f.time);
v.push_back(m);
v.push_back(f);
}
for (auto person : v) {
if (cur < m && person.time == p[cur].time) {
person.wait = 0;
if (p[cur].sex == 1) {
male.push(p[cur]);
} else {
female.push(p[cur]);
}
cur++;
} else if (person.sex == 1) {
male.push(person);
} else {
female.push(person);
}
}
}
while (male.size()) {
auto cur = male.front(); male.pop();
cur.wait = -cur.time;
p[cur.time].wait = cur.wait;
}
while (female.size()) {
auto cur = female.front(); female.pop();
cur.wait = -cur.time;
p[cur.time].wait = cur.wait;
}
printf("%.2lf %.2lf\n", solve(male), solve(female));
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)