【贪心】hdu6180 Schedule

题意:给你n个任务的开始时间和结束时间,一个机器同时最多执行一个任务,问你最少要几个机器。保证机器最少的前提下,问你每个机器的开动时间(最后一次关闭-第一次开启)之和最少是多少。

把这些线段画在数轴上,最大的重叠数就是最少要几个机器。

开动时间怎么算呢?第i个机器的开动时间其实就是(再也不需要>=i台机器的第一个位置 - 需要>=i台机器的第一个位置)。对每个机器的这个值求和即可。

要先离散化。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
struct Point{
	int p,v;
}t[200005];
bool cmp(const Point &a,const Point &b)
{
	return a.v<b.v;
}
int T,n;
int xs[100005],ys[100005],a[200005],ma[200005],Left[200005],Right[200005];
int sufmax[200005];
int main(){
	//freopen("1010.in","r",stdin);
	scanf("%d",&T);
	for(;T;--T){
		memset(a,0,sizeof(a));
		memset(ma,0,sizeof(ma));
		memset(Left,0x7f,sizeof(Left));
		memset(Right,0,sizeof(Right));
		memset(sufmax,0,sizeof(sufmax));
		int all=0;
		scanf("%d",&n);
		for(int i=1;i<=n;++i){
			++all;
			scanf("%d",&t[all].v);
			t[all].p=all;
			++all;
			scanf("%d",&t[all].v);
			t[all].p=all;
		}
		sort(t+1,t+all+1,cmp);
		int zy=0;
		if(t[1].p%2==1){
			xs[(t[1].p+1)/2]=++zy;
		}
		else{
			ys[t[1].p/2]=++zy;
		}
		ma[zy]=t[1].v;
		for(int i=2;i<=all;++i){
			if(t[i].v!=t[i-1].v){
				++zy;
			}
			if(t[i].p%2==1){
				xs[(t[i].p+1)/2]=zy;
			}
			else{
				ys[t[i].p/2]=zy;
			}
			ma[zy]=t[i].v;
		}
		for(int i=1;i<=n;++i){
			++a[xs[i]];
			--a[ys[i]];
		}
		for(int i=1;i<=zy;++i){
			a[i]+=a[i-1];
		}
		sufmax[zy]=a[zy];
		for(int i=zy-1;i>=1;--i){
			sufmax[i]=max(a[i],sufmax[i+1]);
		}
		int ans=*max_element(a+1,a+zy+1);
		for(int i=1;i<=zy;++i){
			if(Left[a[i]]>2000000000){
				Left[a[i]]=i;
			}
		}
		for(int i=ans-1;i>=1;--i){
			Left[i]=min(Left[i],Left[i+1]);
		}
		for(int i=zy;i>=1;--i){
			if(sufmax[i]!=sufmax[i-1]){
				for(int j=sufmax[i]+1;j<=sufmax[i-1];++j){
					Right[j]=i;
				}
			}
		}
		ll sum=0;
		for(int i=1;i<=ans;++i){
			sum+=(ll)(ma[Right[i]]-ma[Left[i]]);
		}
		printf("%d %lld\n",ans,sum);
	}
	return 0;
}
时间: 2024-08-01 15:53:43

【贪心】hdu6180 Schedule的相关文章

URAL 2026 Dean and Schedule 贪心、双端队列(deque)、队列(queue)

C - Dean and Schedule Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 2026 Description A new academic year approaches, and the dean must make a schedule of classes for first-year students. Ther

2017多校第10场 HDU 6180 Schedule 贪心,multiset

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6180 题意:给了一些任务的开始时间和终止时间,现在让我们安排k台及机器,让这些任务在k太机器上最小,并且使得机器的运行时间的和最小. 解法:按开始工作的时间从小到大排序后,用一个set容器维护一下,每次加入找set里面结束时间小于等于开始时间并且最近的点插入即可,然后如果没有小于开始时间的就重新开一台机器即可,这里可能有重复元素,需要multiset. #include <bits/stdc++.h

poj3190Stall Reservations(贪心+优先队列)

题目链接: 啊哈哈,点我点我 思路: 首先根据挤奶时间的先后顺序排序...然后将第一头牛加入优先队列..然后就是加入优先队列的牛应该根据越早结束挤奶那么优先级更高,如果时间结束点相等,那么开始时间早的优先级高... 然后从前向后枚举.如果碰到有牛的挤奶时间的开始值大于优先队列的首部的结束值,那么说明这两头牛可以一起公用一个挤奶房..然后从优先队列中删除这头牛..那么这个问题就得到解决了... 题目: Language: Default Stall Reservations Time Limit:

hdu 6180 Schedule

Schedule Problem Description There are N schedules, the i-th schedule has start time si and end time ei (1 <= i <= N). There are some machines. Each two overlapping schedules cannot be performed in the same machine. For each machine the working time

POJ 1456 Supermarket 区间问题并查集||贪心

F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1456 Appoint description:  System Crawler  (2015-11-30) Description A supermarket has a set Prod of products on sale. It earns a p

G - 贪心

G - 贪心 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Simon and Garfunkel Corporation (SG Corp.) is a large steel-making company with thousand of customers. Keeping the customer satisfied is one of th

2014百度之星资格赛——Disk Schedule

2014百度之星资格赛--Disk Schedule Problem Description 有很多从磁盘读取数据的需求,包括顺序读取.随机读取.为了提高效率,需要人为安排磁盘读取.然而,在现实中,这种做法很复杂.我们考虑一个相对简单的场景. 磁盘有许多轨道,每个轨道有许多扇区,用于存储数据.当我们想在特定扇区来读取数据时,磁头需要跳转到特定的轨道.具体扇区进行读取操作.为了简单,我们假设磁头可以在某个轨道顺时针或逆时针匀速旋转,旋转一周的时间是360个单位时间.磁头也可以随意移动到某个轨道进行

hdu 3572 Task Schedule(最大流)

hdu 3572 Task Schedule Description Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened factory. Her factory has introduced M new machines in order to process the coming N tasks. For the i-th tas

(贪心 + 并查集优化) poj 1456

Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9452   Accepted: 4067 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an int