HDU5360 Hiking

1.题目描述:点击打开链接

2.解题思路:本题利用优先队列解决,然而在这次多校的比赛时候并没有往这个角度考虑,最终要么WA要么TLE==。其实本题的贪心策略很好想:如果把题目中的人数看做数轴上的点的话,那么应该按照从0依次递增的顺序来选择区间,把符合条件的区间预先存起来,然后按照他们的终点由小到大排序,第一个就是我们要的区间,如果遇到第一个区间的终点也小于当前值,那么也将他放入ans数组,只不过被邀请的人数不变而已。显然,看到这种思路就应当往优先队列的角度考虑。这里应当首先对区间按照起点大小排序(若相同则按照终点大小排序)。然后起点小的先进入优先队列,出队列时,终点小的先出队列即可。

3。代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<functional>
using namespace std;

#define me(s) memset(s,0,sizeof(s))
#define pb push_back
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair <int, int> P;

const int N = 100000 + 10;
struct Node
{
	int id;
	int l, r;
	bool operator<(const Node&rhs)const
	{
		return r>rhs.r;
	}
}a[N];//保存所有的区间
int n;

bool cmp(const Node&a,const Node&b)
{
    return a.l<b.l||(a.l==b.l&&a.r<b.r);
}
int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d", &n);
		for (int i = 0; i<n; i++)scanf("%d", &a[i].l);
		for (int i = 0; i<n; i++)
		{
			scanf("%d", &a[i].r); a[i].id = i + 1;
		}
		priority_queue<Node, vector<Node> >q;
		vector<int>ans;
		sort(a, a + n,cmp);//首先按照起点由小到大排序
		int t = 0, cur = 0;/t表示邀请的人数,cur表示当前区间的下标
		for (;; t++)
		{
			while (cur < n&&a[cur].l <= t)//如果当前区间的起点<=当前值,入队列
			{
				q.push(a[cur]); cur++;
			}
			while (!q.empty() && q.top().r < t)//如果队首元素的终点<当前值,说明不能邀请,由于他们不影响被邀请人数t,因此全部出队列
			{
				ans.push_back(q.top().id);
				q.pop();
			}
			if (q.size() == 0)break;//如果发现队列为空,停止
			else//仍然有区间,说明是合法的区间,只将队首元素出队列
			{
				ans.push_back(q.top().id);
				q.pop();
			}
		}
		printf("%d\n", t);

		while (cur < n)//由于邀请人数<起点,这些区间没有进入优先队列,把他们放在最后邀请,同理,他们也不影响邀请人数t
		{
			ans.push_back(a[cur].id);
			cur++;
		}
		for (int i = 0; i < ans.size(); i++)
			printf("%d%c", ans[i], " \n"[i == ans.size() - 1]);
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-26 06:11:11

HDU5360 Hiking的相关文章

hdu5360 Hiking(水题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 724    Accepted Submission(s): 384Special Judge Problem Description There are 

HDU5360——优先队列——Hiking

Problem Description There are n soda conveniently labeled by 1,2,…,n. beta, their best friends, wants to invite some soda to go hiking. The i-th soda will go hiking if the total number of soda that go hiking except him is no less than li and no large

hdu5360||多校联合第6场1008 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=5360 Problem Description There are n soda conveniently labeled by 1,2,-,n. beta, their best friends, wants to invite some soda to go hiking. The i-th soda will go hiking if the total number of soda that go hi

【HDOJ】2425 Hiking Trip

优先级队列+BFS. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 25 8 9 typedef struct node_st { 10 int x, y, t; 11 node_st() {} 12 node_st(int xx, int yy, int tt)

hdu 2425 Hiking Trip

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2425 Hiking Trip Description Hiking in the mountains is seldom an easy task for most people, as it is extremely easy to get lost during the trip. Recently Green has decided to go on a hiking trip. Unfort

HDU 5360 Hiking(优先队列)

Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 492    Accepted Submission(s): 263 Special Judge Problem Description There are  soda conveniently labeled by . beta, their best friends,

hdu 5360 Hiking(优先队列+贪心)

题目:http://acm.hdu.edu.cn/showproblem.php? pid=5360 题意:beta有n个朋友,beta要邀请他的朋友go hiking,已知每一个朋友的理想人数[L,R](现有L~R个人准备去,那么这个朋友就去). 求最多有多少人去. 及beta邀请朋友的顺序. 分析:每次邀请人的最优解就是:选会去的人里面R最小的那个人. 代码实现的话,cur代表已经准备go hiking的人数,每次将全部L<=cur的人放进优先队列,选出R最小的那个. 假设队列为空,那么剩下

HDU 5360——Hiking——————【贪心+优先队列】

Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 118    Accepted Submission(s): 69Special Judge Problem Description There are n soda conveniently labeled by 1,2,…,n. beta, their best fri

HDU 5360 Hiking (贪心+优先队列)

本文纯属原创,转载注明出处:http://blog.csdn.net/zip_fan.谢谢. 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5360 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Special Judge Problem Description There are n soda conveniently la