2023-08-10: There are m items in the scenic area, that is, the item array is int[][] game, which is a m*2 two-dimensional array. The i-th item in the scenic area has the following two parameters: game[i ] = { Ki, Bi } Ki must be negative,

2023-08-10: There are m items in the scenic area, that is, the item array is int[][] game, which is a two-dimensional array of m*2

The i-th item of the scenic spot has the following two parameters:

game[i] = { Key, Bi }

Ki must be negative and Bi must be positive

for example :

Ki = -2, Bi = 10

If only 1 person buys a ticket, the price of a single ticket is: Ki * 1 + Bi = 8

So it costs 8 yuan for one person to play the project

If there are 2 people buying tickets, the price of a single ticket is: Ki * 2 + Bi = 6

So it costs 6 * 2 = 12 yuan for these 2 people to play the project

If there are 5 people buying tickets, the price of a single ticket is: Ki * 2 + Bi = 0

So it costs 0 * 5 = 0 yuan for these 5 people to play the project

If more people buy tickets, they all think it costs 0 yuan (because you are so fucking crazy for letting the project pay back)

So it can be considered that if there are x people buying tickets, the price of a single ticket is: Ki * x + Bi

The total cost of x individuals playing this project is: max { (Ki * x + Bi) * x , 0 }

You are the leader, there are n people in the unit, and each person can choose at most 1 item to play, or you can choose no item

All employees will submit their choices tomorrow night, and then you will spend money and buy tickets uniformly according to the above rules

But now, you want to know how much money you need to prepare to deal with various possible situations,

Support various possible expenses and return the safest amount of money.

Data volume description:

1 <= N、M、Bi <= 10^5,

-(10^5) <= Ki < 0。

From Zuo Chengyun .

Answer 2023-08-10:

Step description:

1. Create a priority queue (heap) h for storing game items. We use the GameHeap type to define the priority queue and implement the Len, Less, Swap, Push and Pop methods.

2. Traverse each item g, create Game structure game with Ki and Bi as parameters during the traversal process, and add it to the priority queue h.

3. Initialize the result variable ans to 0 to record the total cost.

4. Iterate n times, indicating that there are n people performing the operation of selecting game items.

4.1. Check the Earn value of the first item in the current priority queue h (the price of a single ticket multiplied by the number of people). If the Earn value is less than or equal to 0, that is, the item is no longer cost-effective, jump out of the loop.

4.2. Pop an item from the priority queue h and assign it to the variable cur.

4.3. Add the Earn value of the current project to the result variable ans.

4.4. Increase the number of cur.People in the current project.

4.5. Add the updated item cur back to the priority queue h.

5. Return the result variable ans, which is the safest amount prepared.

Total time complexity: O(nlog(m)), where n is the number of people and m is the number of items. Traversing n times, popping the maximum value from the priority queue each time, the time complexity is log(m).

Total space complexity: O(m), the size of the priority queue h depends on the number of items m.

The complete code of go is as follows:

package main

import (
	"container/heap"
	"fmt"
)

type Game struct {
    
    
	Ki     int
	Bi     int
	People int
}

type GameHeap []Game

func (h GameHeap) Len() int            {
    
     return len(h) }
func (h GameHeap) Less(i, j int) bool  {
    
     return h[i].Earn() > h[j].Earn() }
func (h GameHeap) Swap(i, j int)       {
    
     h[i], h[j] = h[j], h[i] }
func (h *GameHeap) Push(x interface{
    
    }) {
    
     *h = append(*h, x.(Game)) }
func (h *GameHeap) Pop() interface{
    
    } {
    
    
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func (g Game) Earn() int {
    
    
	return (2*g.People+1)*g.Ki + g.Bi
}

func EnoughMoney(n int, games [][]int) int {
    
    
	h := &GameHeap{
    
    }
	heap.Init(h)

	for _, g := range games {
    
    
		game := Game{
    
    Ki: g[0], Bi: g[1]}
		heap.Push(h, game)
	}

	ans := 0
	for i := 0; i < n; i++ {
    
    
		if (*h)[0].Earn() <= 0 {
    
    
			break
		}
		cur := heap.Pop(h).(Game)
		ans += cur.Earn()
		cur.People++
		heap.Push(h, cur)
	}

	return ans
}

func main() {
    
    
	games := [][]int{
    
    {
    
    -2, 10}, {
    
    -1, 5}, {
    
    -3, 15}}
	n := 5
	result := EnoughMoney(n, games)
	fmt.Println(result)
}

insert image description here

The complete c++ code is as follows:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct Game {
    
    
    int Ki;
    int Bi;
    int people;

    Game(int k, int b) {
    
    
        Ki = k;
        Bi = b;
        people = 0;
    }

    int earn() const {
    
    
        return (2 * people + 1) * Ki + Bi;
    }
};

struct CompareGame {
    
    
    bool operator()(const Game& a, const Game& b) {
    
    
        return a.earn() < b.earn();
    }
};

int enoughMoney(int n, vector<vector<int>>& games) {
    
    
    priority_queue<Game, vector<Game>, CompareGame> heap;

    for (auto& g : games) {
    
    
        heap.push(Game(g[0], g[1]));
    }

    int ans = 0;

    while (n > 0 && heap.top().earn() > 0) {
    
    
        Game cur = heap.top();
        heap.pop();

        ans += cur.earn();
        cur.people++;
        heap.push(cur);

        n--;
    }

    return ans;
}

int main() {
    
    
    vector<vector<int>> games = {
    
     {
    
    -2, 10}, {
    
    -1, 5}, {
    
    -3, 15} };
    int n = 5;

    int result = enoughMoney(n, games);
    cout << "Amount needed: " << result << endl;

    return 0;
}

insert image description here

The complete code of C language is as follows:

#include <stdio.h>
#include <stdlib.h>

struct Game {
    
    
    int Ki;
    int Bi;
    int people;
};

typedef struct Game Game;

int cmp(const void* a, const void* b) {
    
    
    Game* gameA = (Game*)a;
    Game* gameB = (Game*)b;
    return (2 * gameB->people + 1) * gameB->Ki + gameB->Bi - (2 * gameA->people + 1) * gameA->Ki - gameA->Bi;
}

int enoughMoney(int n, int games[][2], int m) {
    
    
    Game* heap = (Game*)malloc(m * sizeof(Game));
    for (int i = 0; i < m; i++) {
    
    
        heap[i].Ki = games[i][0];
        heap[i].Bi = games[i][1];
        heap[i].people = 0;
    }

    qsort(heap, m, sizeof(Game), cmp);

    int ans = 0;

    for (int i = 0; i < n; i++) {
    
    
        if ((2 * heap[0].people + 1) * heap[0].Ki + heap[0].Bi <= 0) {
    
    
            break;
        }
        ans += (2 * heap[0].people + 1) * heap[0].Ki + heap[0].Bi;
        heap[0].people++;
        qsort(heap, m, sizeof(Game), cmp);
    }

    free(heap);

    return ans;
}

int main() {
    
    
    int games[][2] = {
    
     {
    
    -2, 10}, {
    
    -1, 5}, {
    
    -3, 15} };
    int n = 5;
    int m = sizeof(games) / sizeof(games[0]);

    int result = enoughMoney(n, games, m);
    printf("Total money needed: %d\n", result);

    return 0;
}

insert image description here

Guess you like

Origin blog.csdn.net/weixin_48502062/article/details/132217878