Description
共有 人报名了招聘,第 个人的工作经验为 ,薪水为 。要招聘 个人,并将这些人分为 组,每组两个人。一个人为组长,而另一个人为组员。对于每个组,要求组长的工作经验不小于组员的工作经验。
每个应聘者会有一个意愿 ,表示他应聘的岗位。若 ,则应聘的是组长;若 ,则他应聘的是组员;若 ,则他既可以当组长又可以当组员。
求在不改变意愿的情况下,是否可以招聘满足条件的 个人。如果可以,还要求招聘的人薪水和的最小值。
。
Solution
尽量让组员在组长的前面,所以先根据经验排序,如果经验相同,让志愿为组员的在前面,都可以在中间,志愿为组长的在后面。
令 为排序后前 个人,有 个组长和 个组员的最小代价。对于每个人,根据他的志愿,如果是组长就分进一个只有组员的组,如果是组员就新开一个组,两个都可以就都试一试。
在分好组及其之前,因为排序了,所以不可能有组长的人数比组员多。
发现三维 dp 会 MLE,用滚动数组。还要开 long long。
这样写复杂度为 ,会 TLE。可以特判 的无解情况。复杂度 。
Code
#include <bits/stdc++.h>
using namespace std;
#define re register
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> P;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int read() {
int x = 0, f = 0; char ch = 0;
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
int n, k;
struct person{
int xp, mon, want;
}a[N];
bool cmp(person a, person b){
if (a.xp != b.xp) return a.xp < b.xp;
return a.want < b.want;
}
ll f[2][5005][5005];
int main(){
n = read(), k = read();
if (2 * k > n){
puts("-1"); return 0;
}
for (int i = 1; i <= n; i++){
a[i].xp = read(), a[i].mon = read(), a[i].want = read();
if (a[i].want == 1) a[i].want = 3;
else if (a[i].want == 2) a[i].want = 1;
else a[i].want = 2;
}
sort(a + 1, a + n + 1, cmp);
memset(f, 0x7f, sizeof(f));
f[0][0][0] = 0;
int now = 0;
for (int i = 1; i <= n; i++){
now ^= 1;
for (int j = 0; j <= k; j++)
for (int x = 0; x <= j; x++){
f[now][j][x] = f[now ^ 1][j][x];
if (j && a[i].want != 3) f[now][j][x] = min(f[now][j][x], f[now ^ 1][j - 1][x] + a[i].mon);
if (x && a[i].want != 1) f[now][j][x] = min(f[now][j][x], f[now ^ 1][j][x - 1] + a[i].mon);
}
}
if (f[n & 1][k][k] == 9187201950435737471) puts("-1");
else printf("%lld\n", f[n & 1][k][k]);
return 0;
}