hdu4864

题意:

有n个机器,m个任务,n,m<=100000,每个机器都有工作时间的最大限制xi(0<xi<1440)和完成的最大难度yi(0<=yi<=100),每个任务也有所需要的时间和难度,只要机器的时间大于等于任务,难度大于等于任务,该任务就可以被机器完成,每完成一个任务就可以得到500*xi+2*yi的money,问最多能有多少个任务被完成,并且保证完成任务数量最多的情况下,所得到的money最多是多少?

思路:

由于money是500*x+2*y,而y最大是100,则只要保证任务的x最大,那么得到的钱一定是最多的。

将任务按照时间x从大到小排序,然后依次用任务查找机器,时间x从大到小保证了得到的钱是最多的,然后每个任务找出大于该任务难度y且与难度y最接近的机器完成该任务。现在有个问题需要证明一下,每次找难度最接近的机器,那么时间x只是大于任务x即可,时间是否物尽其用了呢?是否有浪费呢?不会,因为任务的x是从大到小排序遍历,如果任务找到了一个大于自身时间x的机器,就用,即使有别的x更大的机器没有被用,但是可以留下来给别的任务用。

比赛的时候代码WA,虽然也是贪心,但是用的机器查找任务,导致x做到了物尽其用,y并没有做到,so WA~

不要迷迷糊糊的写,要严谨的证明全对再下手写~

 /*===============================================================
* Copyright (C) 2014 All rights reserved.
*
* File Name: hdu4864.cpp
* Author:sunshine
* Created Time: 2014年07月23日
*
================================================================*/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm> using namespace std; #define N 100005 struct node{
int x,y;
bool operator < (const node &n1) const{
if(x != n1.x) return x > n1.x;
return y > n1.y;
}
}task[N]; int main(){
int n,m;
int x,y;
while(cin >> n >> m){ multiset<int> S[];
long long res = ;
int cnt = ; for(int i = ;i < n;i ++){
scanf("%d%d", &x, &y);
S[y].insert(x);
} for(int i = ;i < m;i ++){
scanf("%d%d", &task[i].x, &task[i].y);
} sort(task,task+m); for(int i = ;i < m ;i ++){
x = task[i].x;
y = task[i].y; for(int j = y;j <= ;j ++){
if(S[j].empty()) {
continue;
} multiset<int>::iterator it = S[j].lower_bound(x); if(it == S[j].end() || *it < x){
continue;
}else{
cnt ++;
res += * x + * y;
S[j].erase(it);
break;
}
}
}
printf("%d %I64d\n",cnt,res);
}
return ;
}

hdu4268

题意:

Alice有n个纸片,Bob有n个纸片,n<=100000每个纸片有长度和宽度,已知这2*n个纸片的长和宽,求Alice最多能覆盖Bob多少张纸片,每张纸片只能被用一次。

思路:

将这2*n张纸片按照长x从大到小排序,y从大到小排序,如果x、y相等,则Alice的纸片排在前面,用Bob去查找Alice,即Alice所有的纸片插入到set中,但是并不是一下子全部插入到set中,而是边插入set处理边求解Bob的第i张纸片能否被覆盖,这样能够保证之前插入的所有纸片的长x都大于等于自身的x,每次遇到Alice的纸片则插入到set中,每次遇到Bob的纸片则在set中查找刚好大于等于y的第一个Alice的纸片,然后把它删除,依次。

 /*===============================================================
* Copyright (C) 2014 All rights reserved.
*
* File Name: hdu4268.cpp
* Author:sunshine
* Created Time: 2014年07月23日
*
================================================================*/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm> using namespace std; #define N 100010 struct node{
int x,y;
bool id;
}p[*N]; int cmp(node n1,node n2){
if(n1.x == n2.x && n2.y == n2.y) return n1.id > n2.id;
if(n1.x != n2.x) return n1.x > n2.x;
return n1.y > n2.y;
} int main(){
int cas;
int n;
int x,y;
scanf("%d", &cas);
while(cas --){
scanf("%d", &n);
for(int i = ;i < n;i ++){
scanf("%d%d", &p[i].x, &p[i].y);
p[i].id = ;
} for(int i = n;i < * n;i ++){
scanf("%d%d", &p[i].x, &p[i].y);
p[i].id = ;
} sort(p,p + * n,cmp);
multiset<int>S;
int cnt = ;
for(int i = ;i < * n;i ++){
if(p[i].id){
S.insert(p[i].y);
}else{
y = p[i].y;
multiset<int>::iterator it = S.lower_bound(y); if(it == S.end() || *it < y){
continue;
}else{
S.erase(it);
cnt ++;
}
}
}
printf("%d\n",cnt);
}
return ;
}

hdu4864 hdu4268 贪心 lower_bound的更多相关文章

  1. [HDU4864]Task (贪心)

    此图和上一篇博客的图一起看有奇效 题意 https://vjudge.net/problem/HDU-4864 思路 贪心 代码 by lyd 我实在是敲不来 #include <iostrea ...

  2. hdu4268贪心

    题意:       两个人有一些图片,矩形的,问a最多能够覆盖b多少张图片.. 思路:       明显是贪心,但是有一点很疑惑,如果以别人为主,每次都用自己最小的切能覆盖敌人的方法就wa,而以自己为 ...

  3. hdu4864 Task贪心好题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4864 题目大意: 有n个机器,m个任务.每个机器至多能完成一个任务.对于每个机器,有一个最大运行时间 ...

  4. POJ1723,1050,HDU4864题解(贪心)

    POJ1723 Soldiers 思维题. 考虑y坐标,简单的货舱选址问题,选择中位数即可. 再考虑x坐标,由于直接研究布置方法非常困难,可以倒着想:不管如何移动,最后的坐标总是相邻的,且根据贪心的思 ...

  5. 贪心+容器 hdu4268

    Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...

  6. hdu4864 贪心

    题意:        给你n太机器,m个任务,每个任务和机器都有两个权值x,y,每个机器只能被一个任务使用,条件是机器的两个权值分别比任务的大于等于,每个任务获得的价值是x*500+y*2,问你最多能 ...

  7. HDU4268 Alice and Bob(贪心+multiset)

    Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...

  8. HDU4864:Task(贪心)

    题意: 给出n个机器和m个任务,对于一天来说,每个机器有最大工作时间xi,可接受最大等级yi,每个任务有一个工作时间xi,一个等级yi,可获价值为500*xi+2*yi,任务需要在一台机器一天内完成, ...

  9. hdu 4268 贪心+set lower_bound用法

    http://acm.hdu.edu.cn/showproblem.php?pid=4268 A想用手里的牌尽量多地覆盖掉B手中的牌.. 牌有h和w 问A手中的牌最多能覆盖B多少张牌 iterator ...

随机推荐

  1. CoreData 基本操作方法封装

    转:http://blog.csdn.net/marujunyy/article/details/18500523 为了方便使用CoreData 封装了几个扩展类,使用方法和类文件如下: //首先需要 ...

  2. MySQL5.6 replication architecture --原图来自姜承尧

  3. PLSQL Developer报“动态执行表不可访问,本会话的自动统计被禁止”的解决方案

    现象与提示: 第一次用PLSQL Developer连接数据库,若用sys用户登录并操作则正常,若用普通用户比如haishu登录并创建一个表则报错"动态执行表不可访问,本会话的自动统计被禁止 ...

  4. 【LeetCode 221】Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  5. Tableau学习笔记之五

    计算用户自定义字段,虽然在Tableau软件中已经加入了很多的数值操作运算,比如平均值,最大值等,但是可以自定义自己需要的数值操作运算. 数值操作可以有以下:预定义函数,百分比,总计,分级等等 1.直 ...

  6. Cocos2d-x使用iOS游戏内付费IAP(C++篇)

    本文章转载 http://www.ityran.com/archives/5515.非本人原创! 前期准备 设备与账号 在开始编码之前我们需要准备测试环境. IAP只能真机测试,准备一台iOS设备是必 ...

  7. Java Spring boot 系列目录

    Spring boot 介绍 Spring boot 介绍 Spring boot 介绍 Spring boot 介绍 Spring boot 介绍 Spring boot 介绍 Spring boo ...

  8. dbms_file_transfer使用简介

    dbms_file_transfer这个包可以在两个位置传输文件,分别可以有以下位置: a 从一个asm diskgroup传输到另外一个asm diskgroup b 从一个asm diskgrou ...

  9. Backbone.js developer 武汉 年薪8w-10w

    1.   精通Backbone.js 2.   熟练Ajax.NoSQL.RESTful APIs 3.   了解Pusher.com和 Parse.com 4.   具有良好的沟通能力,学习能力,敬 ...

  10. HDU 5607 graph(DP+矩阵乘法)

    [题目链接] http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=663&pid=1002 [题意] 给定一个有向 ...