zoukankan      html  css  js  c++  java
  • 【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('
    ')
    #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();
    }
    
  • 相关阅读:
    Microsoft Security Essentials
    android studio 引用远程仓库下载慢(JCenter下载慢)的办法
    Android studio Unable to start the daemon process
    Android studio 怎么使用已经下载好的Android SDK ?
    解决Unknown error: Unable to build: the file dx.jar was not loaded from the SDK folder!
    U3d中实现A*寻路,附源文件
    [运维-服务器 – 1A] – nginx.conf(转)
    unity客户端与c++服务器之间的简单通讯_1
    [Java Web – Maven – 1A]maven 3.3.3 for windows 配置(转)
    [Android-2A] -仿IOS微信滑动删除_SwipeListview左滑删除例子
  • 原文地址:https://www.cnblogs.com/ivorysi/p/9207001.html
Copyright © 2011-2022 走看看