Big Surf & Leopard intelligence learning Pa - priority queue using -priority_queue of (heap sort)

No matter how high the mountain, climb, always climb;
the road may be long, go on, will be able to reach.

Use the priority queue priority_queue

First describes the following priority_queue this thing.
This thing has three parameters
First, the first argument is like a normal stack and queue, this is a type of this itself.
The second vector is inherited this thing, usually vector <>
The third function is ordered, there Greater () less change () here is the type member of the type ordered
For chestnut
Big Surf intelligence
Title Description

CCTV Xiaowei enroll Intellect surfing program. The Challenge attracted many participants, the moderator in recognition of everyone's courage to reward each participant m yuan. Do not be too happy! Because these are not necessarily paying the money it is yours? ! Then the host announced the rules of the game:
First of all, the game is divided into n time periods (n≤500), it gives a lot of games, each game must be completed (1≤ti≤ before the specified period ti n). If a game can not be completed before the prescribed time limit, the incentive fees from m-ary deduct part of the money wi, wi is a natural number, different game deduct money is not the same. Of course, every game itself is very simple, to ensure that each participant can be completed within a period of time, but must start from the whole period. Moderator just want to quiz each participant how to arrange the order of their own organization to do the game. As a participant, Xiaowei wanted to win, of course, want to win the most money! Note: The game will not allow participants to lose money!

Entry

The first acts of m, represents the beginning of a reward to each participant's money;
the second row n, n expressed little game;
the third line has the number n, respectively, the provisions of game 1 to n of deadlines;
4 row has the number n, each represent 1 to n charge game can not be completed before the deadline.

Export

Xiaowei was able to win the most money.

Sample Input

10000
7
4 2 4 3 1 4 6
70 60 50 40 30 20 10

Sample Output

9950

This problem is a greedy thinking of the title, greed idea is to find the optimal solution to meet the current time, meaning that if you are not satisfied with this fill, then it freed up a time for him (After the sort time will be controlled within a difference of 1) So we pop the top of the head element, so put this one time vacated.
Time to AC

#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<utility>
#include<stdio.h>
#include<vector>
#include <stack>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<map>
#pragma warning(disable:4244)
#define PI 3.1415926536
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
inline ll read() {
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
inline void out(ll a)
{
	if (a < 0)putchar('-'), a = -a;
	if (a >= 10)out(a / 10);
	putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod; n >>= 1;
	}
	return res;
}
#define read read()
struct node {//存储
	ll t, val;
}save[1000000];
bool cmp(node a, node b)//让时间小的放在最前面这样的话入队列时可以控制时间差在1之内
{                       //当前的时间要么等于限定时间,要么在限定时间之后。
	return a.t < b.t;   //那些有可能叠加时间的导致当前去掉一个pop不能满足的
}                       //都会在每一次的pop中去除。因为不可能出现当前的时间会比以用时间小的
int main()
{
	ll tot = read;//记录总共的money
	ll num = read;
	ll sum = 0;
	for (ll i = 0; i < num; i++)save[i].t = read;
	for (ll i = 0; i < num; i++)
	{
		save[i].val = read;
		sum += save[i].val;//记录当前的可扣的所有的money
	}
	sort(save, save + num, cmp);
	priority_queue<ll, vector<ll>, greater<ll>>q;
	for (ll i = 0; i < num; i++)
	{
		q.push(save[i].val);//入队列
		if (q.size() > save[i].t)q.pop();//剔除队列
	}
	while (!q.empty()) {
		sum -= q.top();//减去那些可以不扣的money,剩下的就是必须扣的钱了
		q.pop();
	}
	cout << tot - sum << endl;
	return 0;
}

Example II
Leopard learn Pa
Entry

Each subject are consuming one unit of time to learn, time to start from zero, there are 1 billion per unit time. At any time, any one can homework (numbers 1 ~ n) to learn.
For the i homework, I have a deadline Di, if completion of this course, will be able to acquire knowledge Pi.
In the given homework and deadlines, knowledge that can be obtained is the maximum number is it?

Export

The first line, an integer n, denotes the number of homework
next n lines of two integers, and Di Pi

Sample Input

3
2 10
1 5
1 7

Sample Output
17

analysis
This question is very clear thinking, and surfing Intellect even more straightforward, like this directly to how much value you can get
the code below
Time to AC

#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<utility>
#include<stdio.h>
#include<vector>
#include <stack>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<map>
#pragma warning(disable:4244)
#define PI 3.1415926536
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
inline ll read() {
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
inline void out(ll a)
{
	if (a < 0)putchar('-'), a = -a;
	if (a >= 10)out(a / 10);
	putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod; n >>= 1;
	}
	return res;
}
#define read read()
struct node {
	int val, t;
}save[100001];
bool cmp(node a, node b)
{
	return a.t < b.t;
}
int main()
{
	priority_queue<int, vector<int>, greater<int>>q;
	int n = read;
	for (int i = 0; i < n; i++)
	{
		save[i].t = read;
		save[i].val = read;
	}
	sort(save, save + n, cmp);
	for (int i = 0; i < n; i++)
	{
		q.push(save[i].val);
		if (save[i].t < q.size())q.pop();
	}
	ll ans = 0;
	while (!q.empty())
	{
		ans += q.top();
		q.pop();
	}
	cout << ans << endl;
	return 0;
}

By- wheel month

Published 32 original articles · won praise 11 · views 1180

Guess you like

Origin blog.csdn.net/qq_35339563/article/details/104872057