hdu4864 hdu4268 贪心 lower_bound
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的更多相关文章
- [HDU4864]Task (贪心)
此图和上一篇博客的图一起看有奇效 题意 https://vjudge.net/problem/HDU-4864 思路 贪心 代码 by lyd 我实在是敲不来 #include <iostrea ...
- hdu4268贪心
题意: 两个人有一些图片,矩形的,问a最多能够覆盖b多少张图片.. 思路: 明显是贪心,但是有一点很疑惑,如果以别人为主,每次都用自己最小的切能覆盖敌人的方法就wa,而以自己为 ...
- hdu4864 Task贪心好题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4864 题目大意: 有n个机器,m个任务.每个机器至多能完成一个任务.对于每个机器,有一个最大运行时间 ...
- POJ1723,1050,HDU4864题解(贪心)
POJ1723 Soldiers 思维题. 考虑y坐标,简单的货舱选址问题,选择中位数即可. 再考虑x坐标,由于直接研究布置方法非常困难,可以倒着想:不管如何移动,最后的坐标总是相邻的,且根据贪心的思 ...
- 贪心+容器 hdu4268
Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...
- hdu4864 贪心
题意: 给你n太机器,m个任务,每个任务和机器都有两个权值x,y,每个机器只能被一个任务使用,条件是机器的两个权值分别比任务的大于等于,每个任务获得的价值是x*500+y*2,问你最多能 ...
- HDU4268 Alice and Bob(贪心+multiset)
Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...
- HDU4864:Task(贪心)
题意: 给出n个机器和m个任务,对于一天来说,每个机器有最大工作时间xi,可接受最大等级yi,每个任务有一个工作时间xi,一个等级yi,可获价值为500*xi+2*yi,任务需要在一台机器一天内完成, ...
- hdu 4268 贪心+set lower_bound用法
http://acm.hdu.edu.cn/showproblem.php?pid=4268 A想用手里的牌尽量多地覆盖掉B手中的牌.. 牌有h和w 问A手中的牌最多能覆盖B多少张牌 iterator ...
随机推荐
- 【转】linux线程模型
一.定义 关于进程.轻量级进程.线程.用户线程.内核线程的定义,这个很容易找到,但是看完之后你可以说你懂了,但实际上你真的明白了么? 在现代操作系统中,进程支持多线程.进程是资源管理的最小单元:而线程 ...
- ASP.NET服务器控件对应的HTML标签
了解ASP.NET的控件最终解析成什么HTML代码,有助于我们对ASP.NET更深的了解,在使用JS交互时也知道如何操作. ASP.NET 服务器控件渲染到客户端之后对应的HTML标签讲解. labe ...
- [转] TreeList 当前节点图标和背景色设置
高原之上原文TreeList 选中节点时图标状态和背景色 // 给TreeList加SelectImage this.treelArea.SelectImageList = imglCustom; / ...
- codeforces 681D Gifts by the List dfs+构造
题意:给你一个森林,表示其祖先关系(自己也是自己的祖先),每个人有一个礼物(要送给这个人的固定的一个祖先) 让你构造一个序列,使得的对于每个人,这个序列中第一个出现的他的祖先,是他要送礼物的的那个祖先 ...
- XSLT 调用java
XSLT调用JS http://www.ibm.com/developerworks/cn/xml/tips/x-tipxsltjs/index.htmlXSLT调用JAVA http://unm ...
- private
成员变量私有化的好处在于可以强制加强面向对象和封装的概念,一个面向对象的系统更加关注行为,而不是数据,所以应该通过发送消息来获得数据,也应该实习细节的封装
- ping命令的几个简单使用
发觉linux下的ping命令花样还挺多的,下面是几个例子 1.ping www.baidu.com,最粗糙的用法,此时主机将不停地向目的地址发送ICMP echo request数据包,直至你按下C ...
- JavaEE5 Tutorial_JavaBean,JSTL
<jsp:useBean id="beanName" class="fully_qualified_classname" scope="scop ...
- es 的集群状态
es的集群状态一共有三种 : green yellow red 状态是基于 碎片的 等级进行划分的 .
- 由浅入深探究mysql索引结构原理、性能分析与优化
摘要: 第一部分:基础知识 第二部分:MYISAM和INNODB索引结构 1.简单介绍B-tree B+ tree树 2.MyisAM索引结构 3.Annode索引结构 4.MyisAM索引与Inno ...