这题就是个贪心题
通过对价值排序,然后从高到低,每次找一个尽量小的能放得下的位置放置就好了
我是用了multiset和lowerbound来查找
时间是nlogn的
#include <bits/stdc++.h>
#include <set>
#include <algorithm>
using namespace std;
struct Node {
int v, w, index;
Node() {}
Node(int vv, int ww, int ii): v(vv), w(ww), index(ii) {}
bool operator<(const Node &other) const {
if (v > other.v) return true;
if (v < other.v) return false;
return w < other.w;
}
};
struct NNode {
int w, index;
NNode() {}
NNode(int ww, int ii): w(ww), index(ii) {}
bool operator<(const NNode &other) const {
return w < other.w;
}
};
const int maxn = 10010;
multiset<NNode> s;
int f[maxn];
Node a[maxn];
int n, m, x;
int main() {
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
s.insert(NNode(x, i));
}
for (int i = 1; i <= m; i++) {
scanf("%d %d", &a[i].v, &a[i].w);
a[i].index = i;
}
sort(a+1, a+1+m);
for (int i = 1; i <= m; i++) {
multiset<NNode>::iterator k = s.lower_bound(NNode(a[i].w, 1));
if (k != s.end()) {
f[(*k).index] = a[i].index;
s.erase(k);
}
}
for (int i = 1; i < n; i++) printf("%d ", f[i]);
printf("%d\n", f[n]);
}