题解

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

既然有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. 服务器安全策略之《通过IP安全策略阻止某个IP访问的设置方法》

    现在我们在布署好了一个网站,发布到外网后就意味着将会接受来自四面八方的黑客攻击,这个情况很常见,我们的网站基本上每天都要接受成千上万次的攻击,有SQL注入的.有代码注入的.有CC攻击等等...而我作为 ...

  2. Spring 中出现相同名称的 bean 的处理机制

    小总结: 如果启用组件扫描,bean名称不同时,Spring将尝试创建一个bean,即使该类的bean已经在spring-config.xml中定义了. 但是,如果在spring配置文件中定义的bea ...

  3. uploadify IE11 不兼容问题(不显示图片)

    1.进入uploadify官网demo  :  http://www.uploadify.com/demos/ 2.  显示   (确认flash为最新版本) 3.更换其它浏览器一切正常 4.原因:I ...

  4. CSS3实战之box-shadow篇

    box-shadow属性包含6个参数值:阴影类型.X轴位移.Y轴位移.阴影大小.阴影扩展和阴影颜色.这6个参数值可以有选择地省略. 现在我们用一个img元素来举栗子 我们先来写最简单的box-shad ...

  5. SQL Server 数据库备份失败解决方法

    问题:System.Data.SqlClient.SqlError: 无法使用备份文件 'D:\20160512.bak',因为原先格式化该文件时所用扇区大小为 512,而目前所在设备的扇区大小为 4 ...

  6. TED_Topic5:How virtual reality can create the ultimate empathy machine

    By Chris Milk # Background about our speaker Working at the frontiers of interactive technology, Chr ...

  7. 20155217 2016-2017-2 《Java程序设计》第4周学习总结

    20155217 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 在java中,继承时使用extends关键字,private成员也会被继承,只不过子 ...

  8. c++的类型转换(转)

    类型转换机制可以分为:隐式类型转换 和 显示类型转换(强制类型转换) C中的类型转换: 事情要从头说起,这个头就是C语言.我们已经习惯了使用C-like类型转换,因为它强大而且简单. 主要有以下两种形 ...

  9. opencv 摄像头

    VideoCapture cap(); if(!cap.isOpened()) ; Mat frame, edges; namedWindow(); for(;;) { cap >> fr ...

  10. Vue项目按需打包Lodash

    使用的是 webpack 模板 1. 首先安装 npm install lodash --save npm install lodash-webpack-plugin babel-plugin-lod ...