Exploitation of A-question ljw in Wu Li school competition (thinking + map application)

Posted by Perry Mason on Thu, 03 Feb 2022 00:32:28 +0100

Wu Li school competition question A

Exploitation of ljw (thinking + map application)

Niuke link

Meaning:

Given a [] and b [] two arrays with the same length of n, after a series of operations,
send p = ∑ i = 1 n m a x ( ( a i − b i ) , 0 ) p=\sum_{i=1}^nmax((a_i-b_i),0) p = ∑ i=1n max((ai − bi), 0) minimum. (0 < i <= n)

There are two operations:
1: Select a value x, and the values equal to X in all b arrays will be doubled
if (b[i] == x) b[i] * =2
2: Select a value x, and all values equal to X in the b array are added with the previous value
if (b[i] == x) b[i] += b[i - 1], note that b[0] = 0

There are 10 operation opportunities in total. The same x cannot be selected for different operations (i.e. if it is read in operation 1 x 0 x_0 x0, read in is not allowed in operation 2 x 0 x_0 x0​)

The same X can be selected for the same operation, but no matter how many times x is selected, it will be executed only once

Pay attention to the output format (copy directly if you don't want to write):

Output 10 lines with two numbers in each line. The two values are separated by spaces. The first number is 1 or 2. 1 means to release the "I love ljw" skill, 2 means to release the "ljw love me" skill, and the second number means the non negative integer selected by the skill. The 10 line output is output in the order of the second number from small to large. If there are multiple qualified results, please output the scheme with the minimum sum of 10 non negative integers. If there are multiple schemes with the same sum of 10 non negative integers but different skills, output the scheme with the minimum sum of 10 skills (i.e. the sum of the first number).

Idea:

(x is the selected value)
Maintain two map s and record how much the value of the whole formula can be reduced by selecting operation 1 (or 2) when x == b[i].

map[key] = value means that when key is selected as x, the value of the whole formula can be reduced by value.

Then merge and sort mp1 and mp2, take out the first 10 key s and labels, and then output them (if there are less than 10, just output several lines 1 and 0 first)

If the values of map are equal, the value of mp1 will be in front of that of mp2.

Note: the ten keys taken out cannot have the same value (that is, the same key exists in mp1 and mp2, and only the key in front of it is taken)

Details:

Traverse a[i]. When a[i] > b [i], let the two map s perform the following operations:

mp1[b[i]] += min(a[i] - b[i], b[i]);//It was originally damaged by a[i] - b[i]; When a [i] > 2 * b[i], reduce b[i] points of damage, otherwise reduce a[i] - b[i] points of damage (i.e. no injury)
mp2[b[i]] += min(a[i] - b[i], b[i - 1]);//Similarly

Note that the data is large (1e9), so you can change the VIS array to map < int, int > vis and record whether this value has been selected

Full code:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef double dd;
typedef pair<int, int> pii;
typedef pair<dd, dd> pdd;
const int MAXN = 200010;
const int inf = 1e9 + 7;
int n, cnt;
int a[MAXN], b[MAXN];
map<int, int>vis; //Open the array result seven or eight times and choose to open it into map

struct TY {
	int num, val, id;//num: operation 1 or 2, val: how much to reduce the whole formula, id: the value of the selected x
	bool operator<(const TY& a) {
		if (val == a.val) return num < a.num;
		return val > a.val;
	}
}e[MAXN << 1], g[MAXN << 1];

map<int, int>mp1, mp2;

bool cmp(TY a, TY b)
{
	return a.id < b.id;
}

int main()
{
	//freopen("D:\\C\\acm\\2021\\ljwzbj0.in", "r", stdin);
	//input
	int n;
	scanf("%d", &n);
	for (int i = 1;i <= n;i++) scanf("%d", &a[i]);
	for (int i = 1;i <= n;i++) scanf("%d", &b[i]);
	//Traversal, operation
	for (int i = 1;i <= n;i++)
	{
		if (a[i] <= b[i]) continue;
		mp1[b[i]] += min(a[i] - b[i], b[i]);
		mp2[b[i]] += min(a[i] - b[i], b[i - 1]);
	}
	//Merge two map s
	int cnt = 0;
	for (auto& p : mp1) e[++cnt] = { 1,p.second,p.first };
	for (auto& p : mp2) e[++cnt] = { 2,p.second,p.first };
	sort(e + 1, e + 1 + cnt);
	//Remove the repeatedly selected values and g[] save the final answer
	int cnt2 = 0;
	for (int i = 1;i <= cnt;i++)
	{
		if (vis[e[i].id] == 1) continue;
		g[++cnt2] = e[i];
		vis[e[i].id] = 1;
	}
	//If it is less than 10, several 1s and 0s will be output
	if (cnt2 < 10)
	{
		sort(g + 1, g + 1 + cnt2, cmp);//Sort according to the output format
		for (int i = 1;i + cnt2 <= 10;i++)
		{
			printf("1 0\n");
		}
		for (int i = 1;i <= cnt2;i++)
		{
			printf("%d %d\n", g[i].num, g[i].id);
		}
	}
	//More than 10 outputs, top ten
	else
	{
		sort(g + 1, g + 11, cmp);
		for (int i = 1;i <= 10;i++)
		{
			printf("%d %d\n", g[i].num, g[i].id);
		}
	}
}

Summary:

During the competition, I saw that the problem was long and complex. My teammates also said it was a cancer problem, so I didn't pay too much attention. When I looked at the problem after the game, it was not difficult to think. The main complexity was to understand the meaning of the problem and output the format.

After the code is finished, the RE sends seven or eight times. Finally, it is found that at the beginning, VIS is opened into an array vis[200010], which is changed into a map.

On the whole, it's a pity that the game didn't come out. It seems that reading comprehension should be strengthened

Topics: map