# 103. 电影

unordered_map内部是实现了一个哈希表，查找效率为O(1)，效率比map要快

unordered_map和map的区别：https://www.cnblogs.com/strawqqhat/p/10602515.html

#include <bits/stdc++.h>
using namespace std;

#if(__cplusplus == 201103L)
#include <unordered_map>
#include <unordered_set>
#else
#include <tr1/unordered_map>
#include <tr1/unordered_set>
namespace std
{
using std::tr1::unordered_map;
using std::tr1::unordered_set;
}
#endif

const int N = 2e5 + 5;

unordered_map<int,int> ma;

typedef struct{
int id, lan, sub;
}movie;

movie arr[N];

bool cmp(movie a, movie b){
if (a.lan != b.lan)
return a.lan > b.lan;
else
return a.sub > b. sub;
}

int main(void)
{
int n, m;
int a, b, c;
scanf("%d", &n);
for (int i = 1; i <= n; i++){
scanf("%d", &a);
ma[a]++;
}
scanf("%d", &m);
for (int i = 1; i <= m; i++){
scanf("%d", &b);
arr[i].lan = ma[b];
arr[i].id = i;
}
for (int i = 1; i <= m; i++){
scanf("%d", &c);
arr[i].sub = ma[c];
}
sort(arr + 1, arr + n + 1, cmp);
printf("%d\n", arr[1].id);
return 0;
}


# 104. 货仓选址

#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5;

int arr[N];

int main(void)
{
int n, ans = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &arr[i]);
sort(arr + 1, arr + n + 1);
for (int i = 1; i <= n / 2; i++){
ans += arr[n - i + 1] - arr[i];
}
printf("%d\n", ans);
return 0;
}


# 106. 动态中位数

#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;

priority_queue<int> max_que;
priority_queue<int, vector<int>, greater<int> > min_que;

int main(void)
{
int p, id, m, t, cnt;
cin >> p;
while (p--){
cnt = 0;
cin >> id >> m;
cout << id << ' ' << (m + 1) / 2 << endl;
for (int i = 1; i <= m; i++){
cin >> t;
//判定应该放入大根堆还是小根堆
if (i == 1 || t >= min_que.top())
min_que.push(t);
else if (t < min_que.top())
max_que.push(t);
//判断两堆之间的数目是否合理
int d = max_que.size() - min_que.size();
if (d > 0){
t = max_que.top();
max_que.pop();
min_que.push(t);
}
else if (d < -1){
t = min_que.top();
min_que.pop();
max_que.push(t);
}
//输出中位数
if (i % 2 == 1){
cout << min_que.top() << ' ';
cnt++;
if (cnt % 10 == 0)
cout << endl;
}
}
cout << endl;
//清空
while (max_que.size() > 0){
max_que.pop();
}
while (min_que.size() > 0){
min_que.pop();
}
}
return 0;
}


# 107. 超快速排序

#include <iostream>
using namespace std;
const int N = 5e5 + 5;
typedef long long ll;

ll a[N], b[N], cnt;

void merge(int l, int mid, int r)
{
if (r - l < 1)
return;
merge(l, (l + mid) / 2, mid);
merge(mid + 1, (mid + 1 + r) / 2, r);
int i = l, j = mid + 1;
for (int k = l; k <= r; k++)
if (j > r || i <= mid && a[i] < a[j])
b[k] = a[i++];
else
b[k] = a[j++], cnt += mid - i + 1;
for (int k = l; k <= r; k++)
a[k] = b[k];
}

int main(void)
{
int n;
while (cin >> n && n != 0){
cnt = 0;
for (int i = 1; i <= n; i++)
cin >> a[i];
merge(1, (1 + n) / 2, n);
cout << cnt << endl;
}
return 0;
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客