Cattle passenger OJ tokitsukaze and Soldier (PQ)

Topic links:
I am a child portal Zeishuai

Subject to the effect:
in a game, tokitsukaze need to select some of the soldiers in the n soldiers make up a group to play copy.
Combat power of the i-th soldier to v [i], combat power groups are fighting force of all the soldiers in the regiment and.
But these soldiers have a special request: If you choose the i-th soldiers, the soldiers hope the number of groups of no more than s [i]. (If you do not choose the i-th soldier, do not have this limitation.)
Tokitsukaze wondered combat power groups up to much.

answer:

Need to use the priority queue !!! Σ (゚Д゚Techno) Techno
First, tell us about what is the priority queue (¯)¯) ↗

The so-called priority queue, that queue elements are sorted according to certain rules queue.

Then introduce the basic method used (the structure of a queue element) D ('ω `*)

#include<queue>//头文件还是这个( • ̀ω•́ )✧

//定义优先队列
priority_queue<结构体名> 队列名;//单词有点长,别拼错啦(ノ゚▽゚)ノ
//如:
struct node
{
	int x,y;
	bool operator < (const node &a) const
	{//必须要重载这个小于!!!
		return x<a.x;
	}
}

//优先队列的操作:
q.top();//返回队头元素
q.pop();//出队队头元素
q.push();//入队
q.empty();//判断队列是否为空,空则返回真,非空则返回假
q.size();//返回队列中元素的个数

//接下来就可畅快的使用优先队列啦!

For the next chestnut ヾ (◍ ° ∇ ° ◍) Techno ゙

#include<iostream>
#include<queue>
using namespace std;
struct node
{
	int x,y;
	bool operator < (const node &a) const
	{
		return x<a.x;
		//这里默认是降序排序
		//如果想要升序排序,改为return x>a.x;  即可!
	}
};
priority_queue<node> q;
int main()
{
	int n;
	node t;
	cin>>n;//n组数据
	for(int i=0;i<n;i++)
	{
		cin>>t.x>>t.y;
		q.push(t);
	}
	while(!q.empty())
	{
		t=q.top();
		cout<<t.x<<" "<<t.y<<endl;
		q.pop();
	}
	return 0;
}

测试 环节 ヽ (° ∀ °) method (° ∀ °) Bruno ヽ (° ∀ °) method (° ∀ °) Bruno ヽ (° ∀ °) method (° ∀ °) Bruno

输入:
3
1 2
100 2
3 5
输出:
100 2
3 5
1 2

Formal entry into this question of explanations: (✪ω✪)
combat power of each soldier is v, we do not want to exceed the length of the ranks of the s.
(This problem is sorted in descending order of priority queue! Have questions, please turn to find the basic usage (✪ω✪))

First, the core must be according to each soldier s to operate, because each soldier s may be different, it may be a big a little, so we when input is complete before descending order of all the soldiers of s .

We then descending order of soldiers added to the priority queue of this structure , if the queue length is less than or equal once soldiers to be currently enqueued s, we only need a corresponding number of force elements can be!

At the same time in every time element into the team, plus the sum value of the combat power of the group of soldiers, a team of combat power when the sum minus the value of the group of soldiers , and for each cycle ends, use ans = max (ans, sum) to compare the sum sum value before the modification and the modified value, which is great for what!

Code: (^ _-) ☆

#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#define maxn 100055
#define ll long long int
using namespace std;
struct node
{//士兵的结构体
	int v,s;
	bool operator < (const node &a) const
	{//优先队列的重载
		return v>a.v;
	}
};
node p[maxn];
priority_queue<node> q;
bool cmp(node a,node b)
{//排序s的cmp
	return a.s>b.s;
}
int main()
{
	ll n,v,s,ans,sum;
	while(scanf("%lld",&n)!=EOF)
	{
		ans=0;sum=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&v,&s);
			p[i].v=v;
			p[i].s=s;
		}
		sort(p,p+n,cmp);//首先对s进行排序
		for(int i=0;i<n;i++)
		{
			if(q.size()<p[i].s)
			{//如果队列的长度并未达到当前入队士兵的s
				q.push(p[i]);
				sum+=p[i].v;
			}
			else if(q.size()==p[i].s)
			{//如果队列的长度和当前入队士兵的s相等
			//那么需要队列出队一个士兵,然后让当前士兵入列
				node t=q.top();
				sum-=t.v;
				q.pop();
				q.push(p[i]);
				sum+=p[i].v;
			}
			else if(q.size()>p[i].s)
			{//如果队列的长度大于当前入队士兵的s
			//则需要一直出队,直到队列元素为s-1个
				while(q.size()>=p[i].s)
				{
					node t=q.top();
					sum-=t.v;
					q.pop();
				}
				q.push(p[i]);
				sum+=p[i].v;
			}
			ans=max(ans,sum);
		}
		printf("%lld\n",ans);
	return 0;
}
Published 67 original articles · won praise 42 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_26235879/article/details/100564928