题解

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

既然有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();
}

【51nod】1164 最高的奖励 V2的更多相关文章

  1. 51nod 1163 最高的奖励(贪心+优先队列)

    题目链接:51nod 1163 最高的奖励 看着这题我立马就想到昨天也做了一道贪心加优先队列的题了奥. 按任务最晚结束时间从小到大排序,依次选择任务,如果该任务最晚结束时间比当前时间点晚,则将该任务的 ...

  2. 51nod 1163 最高的奖励

    链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1163 1163 最高的奖励  基准时间限制:1 秒 空间限制:13 ...

  3. 51Nod 1108 距离之和最小 V2 1096 距离之和最小 中位数性质

    1108 距离之和最小 V2基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注三维空间上有N个点, 求一个点使它到这N个点的曼哈顿距离之和最小,输出这个最小 ...

  4. 51NOD 1185 威佐夫游戏 V2(威佐夫博弈)

    1185 威佐夫游戏 V2  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取 ...

  5. 51Nod 1067:Bash游戏 V2(巴什博弈)

    1067 Bash游戏 V2  基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次只能拿1,3,4 ...

  6. 【51Nod 1190】最小公倍数之和 V2

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1190 \[ \begin{aligned} &\sum_{i=a ...

  7. 51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)

    接上一篇,那个递推式显然可以用矩阵快速幂优化...自己随便YY了下就出来了,学了一下怎么用LaTeX画公式,LaTeX真是个好东西!嘿嘿嘿 如上图.(刚画错了一发...已更新 然后就可以过V2了 or ...

  8. 水题:51Nod 1163-最高的奖励

    最高的奖励 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 Description 有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励.在结束时间之前完成该任 ...

  9. 51nod - 1188 - 最大公约数之和 V2 - 数论

    https://www.51nod.com/Challenge/Problem.html#!#problemId=1188 求\(\sum\limits_{i=1}^{n-1}\sum\limits_ ...

随机推荐

  1. SQL Server 属性不匹配。存在属性(Directory, Archive),包括属性(0),不包括属性(Archive, Compressed, Encrypted)

    问题:安装SQL SERVER 2008报错 “存在属性(Directory, Archive),包括属性(0),不包括属性(Archive, Compressed, Encrypted)” 解决办法 ...

  2. 使用 Collections 实现排序 sort方法 对List 实体类实现Comparable类 示例

    package com.test.jj; import java.util.ArrayList; import java.util.Collections; public class Test { A ...

  3. STL-set and multiset

    ! ! ! ! set 中的元素总是保持单调递增. set<int> a; set的插入 set没有尾部插入函数push_back(),元素的插入一般使用insert进行动态检索插入. a ...

  4. Django-数据库增查

    1/ python manage.py shell ---------一般用于调试操作 2/ 建表--定义类 #产品表 class ProductModel(models.Model): #通过类属性 ...

  5. 双击CAD对象,显示自定义对话框实现方法

    class TlsApplication : IExtensionApplication { void IExtensionApplication.Initialize() { TTest.Start ...

  6. 状压dp(B - 炮兵阵地 POJ - 1185 )

    题目链接:https://cn.vjudge.net/contest/276236#problem/B 题目大意:略  具体思路:和我的上一篇写状压dp的思路差不多,不过就是这个题相当于上一个题的升级 ...

  7. Computer Vision Resources

    Computer Vision Resources Softwares Topic Resources References Feature Extraction SIFT [1] [Demo pro ...

  8. UNIX环境高级编程 第3章 文件I/O

    前面两章说明了UNIX系统体系和标准及其实现,本章具体讨论UNIX系统I/O实现,包括打开文件.读文件.写文件等. UNIX系统中的大多数文件I/O只需要用到5个函数:open.read.write. ...

  9. word文档下划线无法显示的解决方法

    在编辑文档的时候经常会遇到下划线无法显示的情况,如图: 如果遇到不能在姓名后面加下划线的情况,我们该怎么做? 请看下面的图解: 1.首先点击左上角的office图标 2.点击右下角“word选项” 3 ...

  10. 26 About the go command go命令行

    About the go command  go命令行 Motivation Configuration versus convention Go's conventions Getting star ...