【51nod】1164 最高的奖励 V2

题解

一道比较神奇的二分图匹配

既然有n个元素,那么能匹配n个位置,我们把这n个位置找出来,是每个区间从左端点开始找到一个没有被匹配到的位置作为该点(我们忽略右端点)

然后我们从价值大到小,然后从左端点的位置开始匹配,如果这个点没有被匹配,就匹配这个点
否则如果这个点已经匹配的区间右端点大于该点的右端点,就用matk[y]去匹配y + 1,否则就用x取匹配y + 1

代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <cmath>
#include <bitset>
#include <queue>
#define enter putchar('\n')
#define space putchar(' ')
//#define ivorysi
#define pb push_back
#define mo 974711
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define MAXN 500005
#define eps 1e-12
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;char c = getchar();T f = 1;
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	c = getchar();
    }
    while(c >= '0' && c <= '9') {
	res = res * 10 - '0' + c;
	c = getchar();
    }
    res = res * f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) out(x / 10);
    putchar('0' + x % 10);
}
int N,pos[5005];
int matk[5005];
struct Seg {
    int s,t;int64 w;
}S[5005];
bool cmpw(Seg a,Seg b) {
    return a.w > b.w;
}
bool cmps(Seg a,Seg b) {
    return a.s < b.s;
}
bool match(int x,int y) {
    
    if(y > N || pos[y] > S[x].t) return 0;
    if(!matk[y]) {matk[y] = x;return 1;}
    if(S[matk[y]].t <= S[x].t) {
	if(match(x,y + 1)) {
	    matk[y] = x;
	    return 1;
	}
    }
    else {
	if(match(matk[y],y + 1)) {
	    matk[y] = x;
	    return 1;
	}
    }
    return 0;
}
void Solve() {
    read(N);
    int s,t;int64 w;
    for(int i = 1 ; i <= N ; ++i) {
	read(s);read(t);read(w);
	S[i] = (Seg){s,t,w};
    }
    sort(S + 1,S + N + 1,cmps);
    for(int i = 1 ; i <= N ; ++i) {
	if(pos[i - 1] < S[i].s) pos[i] = S[i].s;
	else pos[i] = pos[i - 1] + 1;
    }
    sort(S + 1,S + N + 1,cmpw);
    int64 ans = 0;
    for(int i = 1 ; i <= N ; ++i) {
	int t = lower_bound(pos + 1,pos + N + 1,S[i].s) - pos;
	if(match(i,t)) ans += S[i].w;
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}
posted @ 2018-06-21 08:33  sigongzi  阅读(235)  评论(0编辑  收藏  举报