贪心总结

之前的总结

区间相关问题

NOIP2018提高组金牌训练营——贪心算法专题

 

#10005. 「一本通 1.1 练习 1」数列极差

小的先处理,最后肯定最大

大的先处理,最后肯定最小

两个优先队列维护即可

#include<cstdio>
#include<queue>
#include<functional>
#define REP(i, a, b) for(register int i = (a); i < (b); i++)
#define _for(i, a, b) for(register int i = (a); i <= (b); i++)
using namespace std;

int main()
{
	int n;
	while(scanf("%d", &n) && n)
	{
		priority_queue<int, vector<int>, greater<int> > qmax; //小的先处理 
		priority_queue<int> qmin; //大的先处理 
		REP(i, 0, n)
		{
			int x; scanf("%d", &x);
			qmax.push(x); qmin.push(x);
		}
		REP(i, 1, n)
		{
			int a = qmax.top(); qmax.pop();
			int b = qmax.top(); qmax.pop();
			qmax.push(a * b + 1);
		}
		REP(i, 1, n)
		{
			int a = qmin.top(); qmin.pop();
			int b = qmin.top(); qmin.pop();
			qmin.push(a * b + 1);
		}
		printf("%d\n", qmax.top() - qmin.top());
	}
	return 0;
}

 

#10006. 「一本通 1.1 练习 2」数列分段

每一段尽量大就好了,不想敲了。

 

#10007. 「一本通 1.1 练习 3」线段

按照右端点排序,尽量放。

#include<cstdio>
#include<queue>
#include<algorithm>
#define REP(i, a, b) for(register int i = (a); i < (b); i++)
#define _for(i, a, b) for(register int i = (a); i <= (b); i++)
using namespace std;

const int MAXN = 1e6 + 10;
struct node
{ 
	int l, r;
	void read() { scanf("%d%d", &l, &r); }
	bool operator < (const node& rhs) const
	{
		return r < rhs.r;
	}
}a[MAXN];

int main()
{
	int n; scanf("%d", &n);
	REP(i, 0, n) a[i].read();
	sort(a, a + n);
	
	int ans = 1, k = a[0].r;
	REP(i, 1, n)
		if(a[i].l >= k)
		{
			ans++;
			k = a[i].r;
		}
	printf("%d\n", ans);
	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值